Whamcloud - gitweb
LU-50 ldiskfs: pdirops patch for ldiskfs
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4_pdirop-rhel6.patch
1 --- /dev/null   2011-12-14 22:16:16.000000000 +0800
2 +++ linux-2.6.32-131.6.1-pdo/include/linux/htree_lock.h 2011-12-02 17:09:34.000000000 +0800
3 @@ -0,0 +1,187 @@
4 +/*
5 + * include/linux/htree_lock.h
6 + *
7 + * Copyright (c) 2011 Whamcloud, Inc.
8 + *
9 + * Author: Liang Zhen <liang@whamcloud.com>
10 + */
11 +
12 +/*
13 + * htree lock
14 + *
15 + * htree_lock is an advanced lock, it can support five lock modes (concept is
16 + * taken from DLM) and it's a sleeping lock.
17 + *
18 + * most common use case is:
19 + * - create a htree_lock_head for data
20 + * - each thread (contender) creates it's own htree_lock
21 + * - contender needs to call htree_lock(lock_node, mode) to protect data and
22 + *   call htree_unlock to release lock
23 + *
24 + * Also, there is advanced use-case which is more complex, user can have
25 + * PW/PR lock on particular key, it's mostly used while user holding shared
26 + * lock on the htree (CW, CR)
27 + *
28 + * htree_lock(lock_node, HTREE_LOCK_CR); lock the htree with CR
29 + * htree_node_lock(lock_node, HTREE_LOCK_PR, key...); lock @key with PR
30 + * ...
31 + * htree_node_unlock(lock_node);; unlock the key
32 + *
33 + * Another tip is, we can have N-levels of this kind of keys, all we need to
34 + * do is specifying N-levels while creating htree_lock_head, then we can
35 + * lock/unlock a specific level by:
36 + * htree_node_lock(lock_node, mode1, key1, level1...);
37 + * do something;
38 + * htree_node_lock(lock_node, mode1, key2, level2...);
39 + * do something;
40 + * htree_node_unlock(lock_node, level2);
41 + * htree_node_unlock(lock_node, level1);
42 + *
43 + * NB: for multi-level, should be careful about locking order to avoid deadlock
44 + */
45 +
46 +#ifndef _LINUX_HTREE_LOCK_H
47 +#define _LINUX_HTREE_LOCK_H
48 +
49 +#include <linux/list.h>
50 +#include <linux/spinlock.h>
51 +#include <linux/sched.h>
52 +
53 +/*
54 + * Lock Modes
55 + * more details can be found here:
56 + * http://en.wikipedia.org/wiki/Distributed_lock_manager
57 + */
58 +typedef enum {
59 +       HTREE_LOCK_EX   = 0, /* exclusive lock: incompatible with all others */
60 +       HTREE_LOCK_PW,       /* protected write: allows only CR users */
61 +       HTREE_LOCK_PR,       /* protected read: allow PR, CR users */
62 +       HTREE_LOCK_CW,       /* concurrent write: allow CR, CW users */
63 +       HTREE_LOCK_CR,       /* concurrent read: allow all but EX users */
64 +       HTREE_LOCK_MAX,      /* number of lock modes */
65 +} htree_lock_mode_t;
66 +
67 +#define HTREE_LOCK_NL          HTREE_LOCK_MAX
68 +#define HTREE_LOCK_INVAL       0xdead10c
69 +
70 +enum {
71 +       HTREE_HBITS_MIN         = 2,
72 +       HTREE_HBITS_DEF         = 14,
73 +       HTREE_HBITS_MAX         = 32,
74 +};
75 +
76 +enum {
77 +       HTREE_EVENT_DISABLE     = (0),
78 +       HTREE_EVENT_RD          = (1 << HTREE_LOCK_PR),
79 +       HTREE_EVENT_WR          = (1 << HTREE_LOCK_PW),
80 +       HTREE_EVENT_RDWR        = (HTREE_EVENT_RD | HTREE_EVENT_WR),
81 +};
82 +
83 +struct htree_lock;
84 +
85 +typedef void (*htree_event_cb_t)(void *target, void *event);
86 +
87 +struct htree_lock_child {
88 +       struct list_head        lc_list;        /* granted list */
89 +       htree_event_cb_t        lc_callback;    /* event callback */
90 +       unsigned                lc_events;      /* event types */
91 +};
92 +
93 +struct htree_lock_head {
94 +       unsigned long           lh_lock;        /* bits lock */
95 +       /* blocked lock list (htree_lock) */
96 +       struct list_head        lh_blocked_list;
97 +       /* # key levels */
98 +       u16                     lh_depth;
99 +       /* hash bits for key and limit number of locks */
100 +       u16                     lh_hbits;
101 +       /* counters for blocked locks */
102 +       u16                     lh_nblocked[HTREE_LOCK_MAX];
103 +       /* counters for granted locks */
104 +       u16                     lh_ngranted[HTREE_LOCK_MAX];
105 +       /* private data */
106 +       void                    *lh_private;
107 +       /* array of children locks */
108 +       struct htree_lock_child lh_children[0];
109 +};
110 +
111 +/* htree_lock_node_t is child-lock for a specific key (ln_value) */
112 +struct htree_lock_node {
113 +       htree_lock_mode_t       ln_mode;
114 +       /* major hash key */
115 +       u16                     ln_major_key;
116 +       /* minor hash key */
117 +       u16                     ln_minor_key;
118 +       struct list_head        ln_major_list;
119 +       struct list_head        ln_minor_list;
120 +       /* alive list, all locks (granted, blocked, listening) are on it */
121 +       struct list_head        ln_alive_list;
122 +       /* blocked list */
123 +       struct list_head        ln_blocked_list;
124 +       /* granted list */
125 +       struct list_head        ln_granted_list;
126 +       void                    *ln_ev_target;
127 +};
128 +
129 +struct htree_lock {
130 +       struct task_struct      *lk_task;
131 +       struct htree_lock_head  *lk_head;
132 +       void                    *lk_private;
133 +       unsigned                lk_depth;
134 +       htree_lock_mode_t       lk_mode;
135 +       struct list_head        lk_blocked_list;
136 +       struct htree_lock_node  lk_nodes[0];
137 +};
138 +
139 +/* create a lock head, which stands for a resource */
140 +struct htree_lock_head *htree_lock_head_alloc(unsigned depth,
141 +                                             unsigned hbits, unsigned priv);
142 +/* free a lock head */
143 +void htree_lock_head_free(struct htree_lock_head *lhead);
144 +/* register event callback for child lock at level @depth */
145 +void htree_lock_event_attach(struct htree_lock_head *lhead, unsigned depth,
146 +                            unsigned events, htree_event_cb_t callback);
147 +/* create a lock handle, which stands for a thread */
148 +struct htree_lock *htree_lock_alloc(unsigned depth, unsigned pbytes);
149 +/* free a lock handle */
150 +void htree_lock_free(struct htree_lock *lck);
151 +/* lock htree, when @wait is true, 0 is returned if the lock can't
152 + * be granted immediately */
153 +int htree_lock_try(struct htree_lock *lck, struct htree_lock_head *lhead,
154 +                  htree_lock_mode_t mode, int wait);
155 +/* unlock htree */
156 +void htree_unlock(struct htree_lock *lck);
157 +/* unlock and relock htree with @new_mode */
158 +int htree_change_lock_try(struct htree_lock *lck,
159 +                         htree_lock_mode_t new_mode, int wait);
160 +void htree_change_mode(struct htree_lock *lck, htree_lock_mode_t mode);
161 +/* require child lock (key) of htree at level @dep, @event will be sent to all
162 + * listeners on this @key while lock being granted */
163 +int htree_node_lock_try(struct htree_lock *lck, htree_lock_mode_t mode,
164 +                       u32 key, unsigned dep, int wait, void *event);
165 +/* release child lock at level @dep, this lock will listen on it's key
166 + * if @event isn't NULL, event_cb will be called against @lck while granting
167 + * any other lock at level @dep with the same key */
168 +void htree_node_unlock(struct htree_lock *lck, unsigned dep, void *event);
169 +/* stop listening on child lock at level @dep */
170 +void htree_node_stop_listen(struct htree_lock *lck, unsigned dep);
171 +/* for debug */
172 +void htree_lock_stat_print(int depth);
173 +void htree_lock_stat_reset(void);
174 +
175 +#define htree_lock(lck, lh, mode)      htree_lock_try(lck, lh, mode, 1)
176 +#define htree_change_lock(lck, mode)   htree_change_lock_try(lck, mode, 1)
177 +
178 +#define htree_lock_mode(lck)           ((lck)->lk_mode)
179 +
180 +#define htree_node_lock(lck, mode, key, dep)   \
181 +       htree_node_lock_try(lck, mode, key, dep, 1, NULL)
182 +/* this is only safe in thread context of lock owner */
183 +#define htree_node_is_granted(lck, dep)                \
184 +       ((lck)->lk_nodes[dep].ln_mode != HTREE_LOCK_INVAL && \
185 +        (lck)->lk_nodes[dep].ln_mode != HTREE_LOCK_NL)
186 +/* this is only safe in thread context of lock owner */
187 +#define htree_node_is_listening(lck, dep)      \
188 +       ((lck)->lk_nodes[dep].ln_mode == HTREE_LOCK_NL)
189 +
190 +#endif
191 --- /dev/null   2011-12-14 22:16:16.000000000 +0800
192 +++ linux-2.6.32-131.6.1-pdo/fs/ext4/htree_lock.c       2011-12-14 22:56:28.000000000 +0800
193 @@ -0,0 +1,880 @@
194 +/*
195 + * fs/ext4/htree_lock.c
196 + *
197 + * Copyright (c) 2011 Whamcloud, Inc.
198 + *
199 + * Author: Liang Zhen <liang@whamcloud.com>
200 + */
201 +#include <linux/jbd2.h>
202 +#include <linux/hash.h>
203 +#include <linux/module.h>
204 +#include <linux/htree_lock.h>
205 +
206 +enum {
207 +       HTREE_LOCK_BIT_EX       = (1 << HTREE_LOCK_EX),
208 +       HTREE_LOCK_BIT_PW       = (1 << HTREE_LOCK_PW),
209 +       HTREE_LOCK_BIT_PR       = (1 << HTREE_LOCK_PR),
210 +       HTREE_LOCK_BIT_CW       = (1 << HTREE_LOCK_CW),
211 +       HTREE_LOCK_BIT_CR       = (1 << HTREE_LOCK_CR),
212 +};
213 +
214 +enum {
215 +       HTREE_LOCK_COMPAT_EX    = 0,
216 +       HTREE_LOCK_COMPAT_PW    = HTREE_LOCK_COMPAT_EX | HTREE_LOCK_BIT_CR,
217 +       HTREE_LOCK_COMPAT_PR    = HTREE_LOCK_COMPAT_PW | HTREE_LOCK_BIT_PR,
218 +       HTREE_LOCK_COMPAT_CW    = HTREE_LOCK_COMPAT_PW | HTREE_LOCK_BIT_CW,
219 +       HTREE_LOCK_COMPAT_CR    = HTREE_LOCK_COMPAT_CW | HTREE_LOCK_BIT_PR |
220 +                                 HTREE_LOCK_BIT_PW,
221 +};
222 +
223 +static int htree_lock_compat[] = {
224 +       [HTREE_LOCK_EX]         HTREE_LOCK_COMPAT_EX,
225 +       [HTREE_LOCK_PW]         HTREE_LOCK_COMPAT_PW,
226 +       [HTREE_LOCK_PR]         HTREE_LOCK_COMPAT_PR,
227 +       [HTREE_LOCK_CW]         HTREE_LOCK_COMPAT_CW,
228 +       [HTREE_LOCK_CR]         HTREE_LOCK_COMPAT_CR,
229 +};
230 +
231 +/* max allowed htree-lock depth.
232 + * We only need depth=3 for ext4 although user can have higher value. */
233 +#define HTREE_LOCK_DEP_MAX     16
234 +
235 +#ifdef HTREE_LOCK_DEBUG
236 +
237 +static char *hl_name[] = {
238 +       [HTREE_LOCK_EX]         "EX",
239 +       [HTREE_LOCK_PW]         "PW",
240 +       [HTREE_LOCK_PR]         "PR",
241 +       [HTREE_LOCK_CW]         "CW",
242 +       [HTREE_LOCK_CR]         "CR",
243 +};
244 +
245 +/* lock stats */
246 +struct htree_lock_node_stats {
247 +       unsigned long long      blocked[HTREE_LOCK_MAX];
248 +       unsigned long long      granted[HTREE_LOCK_MAX];
249 +       unsigned long long      retried[HTREE_LOCK_MAX];
250 +       unsigned long long      events;
251 +};
252 +
253 +struct htree_lock_stats {
254 +       struct htree_lock_node_stats    nodes[HTREE_LOCK_DEP_MAX];
255 +       unsigned long long      granted[HTREE_LOCK_MAX];
256 +       unsigned long long      blocked[HTREE_LOCK_MAX];
257 +};
258 +
259 +static struct htree_lock_stats hl_stats;
260 +
261 +void htree_lock_stat_reset(void)
262 +{
263 +       memset(&hl_stats, 0, sizeof(hl_stats));
264 +}
265 +
266 +void htree_lock_stat_print(int depth)
267 +{
268 +       int     i;
269 +       int     j;
270 +
271 +       printk(KERN_DEBUG "HTREE LOCK STATS:\n");
272 +       for (i = 0; i < HTREE_LOCK_MAX; i++) {
273 +               printk(KERN_DEBUG "[%s]: G [%10llu], B [%10llu]\n",
274 +                      hl_name[i], hl_stats.granted[i], hl_stats.blocked[i]);
275 +       }
276 +       for (i = 0; i < depth; i++) {
277 +               printk(KERN_DEBUG "HTREE CHILD [%d] STATS:\n", i);
278 +               for (j = 0; j < HTREE_LOCK_MAX; j++) {
279 +                       printk(KERN_DEBUG
280 +                               "[%s]: G [%10llu], B [%10llu], R [%10llu]\n",
281 +                               hl_name[j], hl_stats.nodes[i].granted[j],
282 +                               hl_stats.nodes[i].blocked[j],
283 +                               hl_stats.nodes[i].retried[j]);
284 +               }
285 +       }
286 +}
287 +
288 +#define lk_grant_inc(m)       do { hl_stats.granted[m]++; } while (0)
289 +#define lk_block_inc(m)       do { hl_stats.blocked[m]++; } while (0)
290 +#define ln_grant_inc(d, m)    do { hl_stats.nodes[d].granted[m]++; } while (0)
291 +#define ln_block_inc(d, m)    do { hl_stats.nodes[d].blocked[m]++; } while (0)
292 +#define ln_retry_inc(d, m)    do { hl_stats.nodes[d].retried[m]++; } while (0)
293 +#define ln_event_inc(d)       do { hl_stats.nodes[d].events++; } while (0)
294 +
295 +#else /* !DEBUG */
296 +
297 +void htree_lock_stat_reset(void) {}
298 +void htree_lock_stat_print(int depth) {}
299 +
300 +#define lk_grant_inc(m)              do {} while (0)
301 +#define lk_block_inc(m)              do {} while (0)
302 +#define ln_grant_inc(d, m)    do {} while (0)
303 +#define ln_block_inc(d, m)    do {} while (0)
304 +#define ln_retry_inc(d, m)    do {} while (0)
305 +#define ln_event_inc(d)              do {} while (0)
306 +
307 +#endif /* DEBUG */
308 +
309 +EXPORT_SYMBOL(htree_lock_stat_reset);
310 +EXPORT_SYMBOL(htree_lock_stat_print);
311 +
312 +#define HTREE_DEP_ROOT           (-1)
313 +
314 +#define htree_spin_lock(lhead, dep)                            \
315 +       bit_spin_lock((dep) + 1, &(lhead)->lh_lock)
316 +#define htree_spin_unlock(lhead, dep)                          \
317 +       bit_spin_unlock((dep) + 1, &(lhead)->lh_lock)
318 +
319 +#define htree_key_event_ignore(child, ln)                      \
320 +       (!((child)->lc_events & (1 << (ln)->ln_mode)))
321 +
322 +static int
323 +htree_key_list_empty(struct htree_lock_node *ln)
324 +{
325 +       return list_empty(&ln->ln_major_list) && list_empty(&ln->ln_minor_list);
326 +}
327 +
328 +static void
329 +htree_key_list_del_init(struct htree_lock_node *ln)
330 +{
331 +       struct htree_lock_node *tmp = NULL;
332 +
333 +       if (!list_empty(&ln->ln_minor_list)) {
334 +               tmp = list_entry(ln->ln_minor_list.next,
335 +                                struct htree_lock_node, ln_minor_list);
336 +               list_del_init(&ln->ln_minor_list);
337 +       }
338 +
339 +       if (list_empty(&ln->ln_major_list))
340 +               return;
341 +
342 +       if (tmp == NULL) { /* not on minor key list */
343 +               list_del_init(&ln->ln_major_list);
344 +       } else {
345 +               BUG_ON(!list_empty(&tmp->ln_major_list));
346 +               list_replace_init(&ln->ln_major_list, &tmp->ln_major_list);
347 +       }
348 +}
349 +
350 +static void
351 +htree_key_list_replace_init(struct htree_lock_node *old,
352 +                           struct htree_lock_node *new)
353 +{
354 +       if (!list_empty(&old->ln_major_list))
355 +               list_replace_init(&old->ln_major_list, &new->ln_major_list);
356 +
357 +       if (!list_empty(&old->ln_minor_list))
358 +               list_replace_init(&old->ln_minor_list, &new->ln_minor_list);
359 +}
360 +
361 +static void
362 +htree_key_event_enqueue(struct htree_lock_child *child,
363 +                       struct htree_lock_node *ln, int dep, void *event)
364 +{
365 +       struct htree_lock_node *tmp;
366 +
367 +       /* NB: ALWAYS called holding lhead::lh_lock(dep) */
368 +       BUG_ON(ln->ln_mode == HTREE_LOCK_NL);
369 +       if (event == NULL || htree_key_event_ignore(child, ln))
370 +               return;
371 +
372 +       /* shouldn't be a very long list */
373 +       list_for_each_entry(tmp, &ln->ln_alive_list, ln_alive_list) {
374 +               if (tmp->ln_mode == HTREE_LOCK_NL) {
375 +                       ln_event_inc(dep);
376 +                       if (child->lc_callback != NULL)
377 +                               child->lc_callback(tmp->ln_ev_target, event);
378 +               }
379 +       }
380 +}
381 +
382 +static int
383 +htree_node_lock_enqueue(struct htree_lock *newlk, struct htree_lock *curlk,
384 +                       unsigned dep, int wait, void *event)
385 +{
386 +       struct htree_lock_child *child = &newlk->lk_head->lh_children[dep];
387 +       struct htree_lock_node *newln = &newlk->lk_nodes[dep];
388 +       struct htree_lock_node *curln = &curlk->lk_nodes[dep];
389 +
390 +       /* NB: ALWAYS called holding lhead::lh_lock(dep) */
391 +       /* NB: we only expect PR/PW lock mode at here, only these two modes are
392 +        * allowed for htree_node_lock(asserted in htree_node_lock_internal),
393 +        * NL is only used for listener, user can't directly require NL mode */
394 +       if ((curln->ln_mode == HTREE_LOCK_NL) ||
395 +           (curln->ln_mode != HTREE_LOCK_PW &&
396 +            newln->ln_mode != HTREE_LOCK_PW)) {
397 +               /* no conflict, attach it on granted list of @curlk */
398 +               if (curln->ln_mode != HTREE_LOCK_NL) {
399 +                       list_add(&newln->ln_granted_list,
400 +                                &curln->ln_granted_list);
401 +               } else {
402 +                       /* replace key owner */
403 +                       htree_key_list_replace_init(curln, newln);
404 +               }
405 +
406 +               list_add(&newln->ln_alive_list, &curln->ln_alive_list);
407 +               htree_key_event_enqueue(child, newln, dep, event);
408 +               ln_grant_inc(dep, newln->ln_mode);
409 +               return 1; /* still hold lh_lock */
410 +       }
411 +
412 +       if (!wait) { /* can't grant and don't want to wait */
413 +               ln_retry_inc(dep, newln->ln_mode);
414 +               newln->ln_mode = HTREE_LOCK_INVAL;
415 +               return -1; /* don't wait and just return -1 */
416 +       }
417 +
418 +       newlk->lk_task = current;
419 +       set_current_state(TASK_UNINTERRUPTIBLE);
420 +       /* conflict, attach it on blocked list of curlk */
421 +       list_add_tail(&newln->ln_blocked_list, &curln->ln_blocked_list);
422 +       list_add(&newln->ln_alive_list, &curln->ln_alive_list);
423 +       ln_block_inc(dep, newln->ln_mode);
424 +
425 +       htree_spin_unlock(newlk->lk_head, dep);
426 +       /* wait to be given the lock */
427 +       if (newlk->lk_task != NULL)
428 +               schedule();
429 +       /* granted, no doubt, wake up will set me RUNNING */
430 +       if (event == NULL || htree_key_event_ignore(child, newln))
431 +               return 0; /* granted without lh_lock */
432 +
433 +       htree_spin_lock(newlk->lk_head, dep);
434 +       htree_key_event_enqueue(child, newln, dep, event);
435 +       return 1; /* still hold lh_lock */
436 +}
437 +
438 +/*
439 + * get PR/PW access to particular tree-node according to @dep and @key,
440 + * it will return -1 if @wait is false and can't immediately grant this lock.
441 + * All listeners(HTREE_LOCK_NL) on @dep and with the same @key will get
442 + * @event if it's not NULL.
443 + * NB: ALWAYS called holding lhead::lh_lock
444 + */
445 +static int
446 +htree_node_lock_internal(struct htree_lock_head *lhead, struct htree_lock *lck,
447 +                        htree_lock_mode_t mode, u32 key, unsigned dep,
448 +                        int wait, void *event)
449 +{
450 +       LIST_HEAD               (list);
451 +       struct htree_lock       *tmp;
452 +       struct htree_lock       *tmp2;
453 +       u16                     major;
454 +       u16                     minor;
455 +       u8                      reverse;
456 +       u8                      ma_bits;
457 +       u8                      mi_bits;
458 +
459 +       BUG_ON(mode != HTREE_LOCK_PW && mode != HTREE_LOCK_PR);
460 +       BUG_ON(htree_node_is_granted(lck, dep));
461 +
462 +       key = hash_long(key, lhead->lh_hbits);
463 +
464 +       mi_bits = lhead->lh_hbits >> 1;
465 +       ma_bits = lhead->lh_hbits - mi_bits;
466 +
467 +       lck->lk_nodes[dep].ln_major_key = major = key & ((1U << ma_bits) - 1);
468 +       lck->lk_nodes[dep].ln_minor_key = minor = key >> ma_bits;
469 +       lck->lk_nodes[dep].ln_mode = mode;
470 +
471 +       /*
472 +        * The major key list is an ordered list, so searches are started
473 +        * at the end of the list that is numerically closer to major_key,
474 +        * so at most half of the list will be walked (for well-distributed
475 +        * keys). The list traversal aborts early if the expected key
476 +        * location is passed.
477 +        */
478 +       reverse = (major >= (1 << (ma_bits - 1)));
479 +
480 +       if (reverse) {
481 +               list_for_each_entry_reverse(tmp,
482 +                                       &lhead->lh_children[dep].lc_list,
483 +                                       lk_nodes[dep].ln_major_list) {
484 +                       if (tmp->lk_nodes[dep].ln_major_key == major) {
485 +                               goto search_minor;
486 +
487 +                       } else if (tmp->lk_nodes[dep].ln_major_key < major) {
488 +                               /* attach _after_ @tmp */
489 +                               list_add(&lck->lk_nodes[dep].ln_major_list,
490 +                                        &tmp->lk_nodes[dep].ln_major_list);
491 +                               goto out_grant_major;
492 +                       }
493 +               }
494 +
495 +               list_add(&lck->lk_nodes[dep].ln_major_list,
496 +                        &lhead->lh_children[dep].lc_list);
497 +               goto out_grant_major;
498 +
499 +       } else {
500 +               list_for_each_entry(tmp, &lhead->lh_children[dep].lc_list,
501 +                                   lk_nodes[dep].ln_major_list) {
502 +                       if (tmp->lk_nodes[dep].ln_major_key == major) {
503 +                               goto search_minor;
504 +
505 +                       } else if (tmp->lk_nodes[dep].ln_major_key > major) {
506 +                               /* insert _before_ @tmp */
507 +                               list_add_tail(&lck->lk_nodes[dep].ln_major_list,
508 +                                       &tmp->lk_nodes[dep].ln_major_list);
509 +                               goto out_grant_major;
510 +                       }
511 +               }
512 +
513 +               list_add_tail(&lck->lk_nodes[dep].ln_major_list,
514 +                             &lhead->lh_children[dep].lc_list);
515 +               goto out_grant_major;
516 +       }
517 +
518 + search_minor:
519 +       /*
520 +        * NB: minor_key list doesn't have a "head", @list is just a
521 +        * temporary stub for helping list searching, make sure it's removed
522 +        * after searching.
523 +        * minor_key list is an ordered list too.
524 +        */
525 +       list_add_tail(&list, &tmp->lk_nodes[dep].ln_minor_list);
526 +
527 +       reverse = (minor >= (1 << (mi_bits - 1)));
528 +
529 +       if (reverse) {
530 +               list_for_each_entry_reverse(tmp2, &list,
531 +                                           lk_nodes[dep].ln_minor_list) {
532 +                       if (tmp2->lk_nodes[dep].ln_minor_key == minor) {
533 +                               goto out_enqueue;
534 +
535 +                       } else if (tmp2->lk_nodes[dep].ln_minor_key < minor) {
536 +                               /* attach _after_ @tmp2 */
537 +                               list_add(&lck->lk_nodes[dep].ln_minor_list,
538 +                                        &tmp2->lk_nodes[dep].ln_minor_list);
539 +                               goto out_grant_minor;
540 +                       }
541 +               }
542 +
543 +               list_add(&lck->lk_nodes[dep].ln_minor_list, &list);
544 +
545 +       } else {
546 +               list_for_each_entry(tmp2, &list,
547 +                                   lk_nodes[dep].ln_minor_list) {
548 +                       if (tmp2->lk_nodes[dep].ln_minor_key == minor) {
549 +                               goto out_enqueue;
550 +
551 +                       } else if (tmp2->lk_nodes[dep].ln_minor_key > minor) {
552 +                               /* insert _before_ @tmp2 */
553 +                               list_add_tail(&lck->lk_nodes[dep].ln_minor_list,
554 +                                       &tmp2->lk_nodes[dep].ln_minor_list);
555 +                               goto out_grant_minor;
556 +                       }
557 +               }
558 +
559 +               list_add_tail(&lck->lk_nodes[dep].ln_minor_list, &list);
560 +       }
561 +
562 + out_grant_minor:
563 +       if (list.next == &lck->lk_nodes[dep].ln_minor_list) {
564 +               /* new lock @lck is the first one on minor_key list, which
565 +                * means it has the smallest minor_key and it should
566 +                * replace @tmp as minor_key owner */
567 +               list_replace_init(&tmp->lk_nodes[dep].ln_major_list,
568 +                                 &lck->lk_nodes[dep].ln_major_list);
569 +       }
570 +       /* remove the temporary head */
571 +       list_del(&list);
572 +
573 + out_grant_major:
574 +       ln_grant_inc(dep, lck->lk_nodes[dep].ln_mode);
575 +       return 1; /* granted with holding lh_lock */
576 +
577 + out_enqueue:
578 +       list_del(&list); /* remove temprary head */
579 +       return htree_node_lock_enqueue(lck, tmp2, dep, wait, event);
580 +}
581 +
582 +/*
583 + * release the key of @lck at level @dep, and grant any blocked locks.
584 + * caller will still listen on @key if @event is not NULL, which means
585 + * caller can see a event (by event_cb) while granting any lock with
586 + * the same key at level @dep.
587 + * NB: ALWAYS called holding lhead::lh_lock
588 + * NB: listener will not block anyone because listening mode is HTREE_LOCK_NL
589 + */
590 +static void
591 +htree_node_unlock_internal(struct htree_lock_head *lhead,
592 +                          struct htree_lock *curlk, unsigned dep, void *event)
593 +{
594 +       struct htree_lock_node  *curln = &curlk->lk_nodes[dep];
595 +       struct htree_lock       *grtlk = NULL;
596 +       struct htree_lock_node  *grtln;
597 +       struct htree_lock       *poslk;
598 +       struct htree_lock       *tmplk;
599 +
600 +       if (!htree_node_is_granted(curlk, dep))
601 +               return;
602 +
603 +       if (!list_empty(&curln->ln_granted_list)) {
604 +               /* there is another granted lock */
605 +               grtlk = list_entry(curln->ln_granted_list.next,
606 +                                  struct htree_lock,
607 +                                  lk_nodes[dep].ln_granted_list);
608 +               list_del_init(&curln->ln_granted_list);
609 +       }
610 +
611 +       if (grtlk == NULL && !list_empty(&curln->ln_blocked_list)) {
612 +               /*
613 +                * @curlk is the only granted lock, so we confirmed:
614 +                * a) curln is key owner (attached on major/minor_list),
615 +                *    so if there is any blocked lock, it should be attached
616 +                *    on curln->ln_blocked_list
617 +                * b) we always can grant the first blocked lock
618 +                */
619 +               grtlk = list_entry(curln->ln_blocked_list.next,
620 +                                  struct htree_lock,
621 +                                  lk_nodes[dep].ln_blocked_list);
622 +               BUG_ON(grtlk->lk_task == NULL);
623 +               wake_up_process(grtlk->lk_task);
624 +       }
625 +
626 +       if (event != NULL &&
627 +           lhead->lh_children[dep].lc_events != HTREE_EVENT_DISABLE) {
628 +               curln->ln_ev_target = event;
629 +               curln->ln_mode = HTREE_LOCK_NL; /* listen! */
630 +       } else {
631 +               curln->ln_mode = HTREE_LOCK_INVAL;
632 +       }
633 +
634 +       if (grtlk == NULL) { /* I must be the only one locking this key */
635 +               struct htree_lock_node *tmpln;
636 +
637 +               BUG_ON(htree_key_list_empty(curln));
638 +
639 +               if (curln->ln_mode == HTREE_LOCK_NL) /* listening */
640 +                       return;
641 +
642 +               /* not listening */
643 +               if (list_empty(&curln->ln_alive_list)) { /* no more listener */
644 +                       htree_key_list_del_init(curln);
645 +                       return;
646 +               }
647 +
648 +               tmpln = list_entry(curln->ln_alive_list.next,
649 +                                  struct htree_lock_node, ln_alive_list);
650 +
651 +               BUG_ON(tmpln->ln_mode != HTREE_LOCK_NL);
652 +
653 +               htree_key_list_replace_init(curln, tmpln);
654 +               list_del_init(&curln->ln_alive_list);
655 +
656 +               return;
657 +       }
658 +
659 +       /* have a granted lock */
660 +       grtln = &grtlk->lk_nodes[dep];
661 +       if (!list_empty(&curln->ln_blocked_list)) {
662 +               /* only key owner can be on both lists */
663 +               BUG_ON(htree_key_list_empty(curln));
664 +
665 +               if (list_empty(&grtln->ln_blocked_list)) {
666 +                       list_add(&grtln->ln_blocked_list,
667 +                                &curln->ln_blocked_list);
668 +               }
669 +               list_del_init(&curln->ln_blocked_list);
670 +       }
671 +       /*
672 +        * NB: this is the tricky part:
673 +        * We have only two modes for child-lock (PR and PW), also,
674 +        * only owner of the key (attached on major/minor_list) can be on
675 +        * both blocked_list and granted_list, so @grtlk must be one
676 +        * of these two cases:
677 +        *
678 +        * a) @grtlk is taken from granted_list, which means we've granted
679 +        *    more than one lock so @grtlk has to be PR, the first blocked
680 +        *    lock must be PW and we can't grant it at all.
681 +        *    So even @grtlk is not owner of the key (empty blocked_list),
682 +        *    we don't care because we can't grant any lock.
683 +        * b) we just grant a new lock which is taken from head of blocked
684 +        *    list, and it should be the first granted lock, and it should
685 +        *    be the first one linked on blocked_list.
686 +        *
687 +        * Either way, we can get correct result by iterating blocked_list
688 +        * of @grtlk, and don't have to bother on how to find out
689 +        * owner of current key.
690 +        */
691 +       list_for_each_entry_safe(poslk, tmplk, &grtln->ln_blocked_list,
692 +                                lk_nodes[dep].ln_blocked_list) {
693 +               if (grtlk->lk_nodes[dep].ln_mode == HTREE_LOCK_PW ||
694 +                   poslk->lk_nodes[dep].ln_mode == HTREE_LOCK_PW)
695 +                       break;
696 +               /* grant all readers */
697 +               list_del_init(&poslk->lk_nodes[dep].ln_blocked_list);
698 +               list_add(&poslk->lk_nodes[dep].ln_granted_list,
699 +                        &grtln->ln_granted_list);
700 +
701 +               BUG_ON(poslk->lk_task == NULL);
702 +               wake_up_process(poslk->lk_task);
703 +       }
704 +
705 +       /* if @curln is the owner of this key, replace it with @grtln */
706 +       if (!htree_key_list_empty(curln))
707 +               htree_key_list_replace_init(curln, grtln);
708 +
709 +       if (curln->ln_mode == HTREE_LOCK_INVAL)
710 +               list_del_init(&curln->ln_alive_list);
711 +}
712 +
713 +/*
714 + * it's just wrapper of htree_node_lock_internal, it returns 1 on granted
715 + * and 0 only if @wait is false and can't grant it immediately
716 + */
717 +int
718 +htree_node_lock_try(struct htree_lock *lck, htree_lock_mode_t mode,
719 +                   u32 key, unsigned dep, int wait, void *event)
720 +{
721 +       struct htree_lock_head *lhead = lck->lk_head;
722 +       int rc;
723 +
724 +       BUG_ON(dep >= lck->lk_depth);
725 +       BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
726 +
727 +       htree_spin_lock(lhead, dep);
728 +       rc = htree_node_lock_internal(lhead, lck, mode, key, dep, wait, event);
729 +       if (rc != 0)
730 +               htree_spin_unlock(lhead, dep);
731 +       return rc >= 0;
732 +}
733 +EXPORT_SYMBOL(htree_node_lock_try);
734 +
735 +/* it's wrapper of htree_node_unlock_internal */
736 +void
737 +htree_node_unlock(struct htree_lock *lck, unsigned dep, void *event)
738 +{
739 +       struct htree_lock_head *lhead = lck->lk_head;
740 +
741 +       BUG_ON(dep >= lck->lk_depth);
742 +       BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
743 +
744 +       htree_spin_lock(lhead, dep);
745 +       htree_node_unlock_internal(lhead, lck, dep, event);
746 +       htree_spin_unlock(lhead, dep);
747 +}
748 +EXPORT_SYMBOL(htree_node_unlock);
749 +
750 +/* stop listening on child-lock level @dep */
751 +void
752 +htree_node_stop_listen(struct htree_lock *lck, unsigned dep)
753 +{
754 +       struct htree_lock_node *ln = &lck->lk_nodes[dep];
755 +       struct htree_lock_node *tmp;
756 +
757 +       BUG_ON(htree_node_is_granted(lck, dep));
758 +       BUG_ON(!list_empty(&ln->ln_blocked_list));
759 +       BUG_ON(!list_empty(&ln->ln_granted_list));
760 +
761 +       if (!htree_node_is_listening(lck, dep))
762 +               return;
763 +
764 +       htree_spin_lock(lck->lk_head, dep);
765 +       ln->ln_mode = HTREE_LOCK_INVAL;
766 +       ln->ln_ev_target = NULL;
767 +
768 +       if (htree_key_list_empty(ln)) { /* not owner */
769 +               list_del_init(&ln->ln_alive_list);
770 +               goto out;
771 +       }
772 +
773 +       /* I'm the owner... */
774 +       if (list_empty(&ln->ln_alive_list)) { /* no more listener */
775 +               htree_key_list_del_init(ln);
776 +               goto out;
777 +       }
778 +
779 +       tmp = list_entry(ln->ln_alive_list.next,
780 +                        struct htree_lock_node, ln_alive_list);
781 +
782 +       BUG_ON(tmp->ln_mode != HTREE_LOCK_NL);
783 +       htree_key_list_replace_init(ln, tmp);
784 +       list_del_init(&ln->ln_alive_list);
785 + out:
786 +       htree_spin_unlock(lck->lk_head, dep);
787 +}
788 +EXPORT_SYMBOL(htree_node_stop_listen);
789 +
790 +/* release all child-locks if we have any */
791 +static void
792 +htree_node_release_all(struct htree_lock *lck)
793 +{
794 +       int     i;
795 +
796 +       for (i = 0; i < lck->lk_depth; i++) {
797 +               if (htree_node_is_granted(lck, i))
798 +                       htree_node_unlock(lck, i, NULL);
799 +               else if (htree_node_is_listening(lck, i))
800 +                       htree_node_stop_listen(lck, i);
801 +       }
802 +}
803 +
804 +/*
805 + * obtain htree lock, it could be blocked inside if there's conflict
806 + * with any granted or blocked lock and @wait is true.
807 + * NB: ALWAYS called holding lhead::lh_lock
808 + */
809 +static int
810 +htree_lock_internal(struct htree_lock *lck, int wait)
811 +{
812 +       struct htree_lock_head *lhead = lck->lk_head;
813 +       int     granted = 0;
814 +       int     blocked = 0;
815 +       int     i;
816 +
817 +       for (i = 0; i < HTREE_LOCK_MAX; i++) {
818 +               if (lhead->lh_ngranted[i] != 0)
819 +                       granted |= 1 << i;
820 +               if (lhead->lh_nblocked[i] != 0)
821 +                       blocked |= 1 << i;
822 +       }
823 +       if ((htree_lock_compat[lck->lk_mode] & granted) != granted ||
824 +           (htree_lock_compat[lck->lk_mode] & blocked) != blocked) {
825 +               /* will block current lock even it just conflicts with any
826 +                * other blocked lock, so lock like EX wouldn't starve */
827 +               if (!wait)
828 +                       return -1;
829 +               lhead->lh_nblocked[lck->lk_mode]++;
830 +               lk_block_inc(lck->lk_mode);
831 +
832 +               lck->lk_task = current;
833 +               list_add_tail(&lck->lk_blocked_list, &lhead->lh_blocked_list);
834 +
835 +               set_current_state(TASK_UNINTERRUPTIBLE);
836 +               htree_spin_unlock(lhead, HTREE_DEP_ROOT);
837 +               /* wait to be given the lock */
838 +               if (lck->lk_task != NULL)
839 +                       schedule();
840 +               /* granted, no doubt. wake up will set me RUNNING */
841 +               return 0; /* without lh_lock */
842 +       }
843 +       lhead->lh_ngranted[lck->lk_mode]++;
844 +       lk_grant_inc(lck->lk_mode);
845 +       return 1;
846 +}
847 +
848 +/* release htree lock. NB: ALWAYS called holding lhead::lh_lock */
849 +static void
850 +htree_unlock_internal(struct htree_lock *lck)
851 +{
852 +       struct htree_lock_head *lhead = lck->lk_head;
853 +       struct htree_lock *tmp;
854 +       struct htree_lock *tmp2;
855 +       int granted = 0;
856 +       int i;
857 +
858 +       BUG_ON(lhead->lh_ngranted[lck->lk_mode] == 0);
859 +
860 +       lhead->lh_ngranted[lck->lk_mode]--;
861 +       lck->lk_mode = HTREE_LOCK_INVAL;
862 +
863 +       for (i = 0; i < HTREE_LOCK_MAX; i++) {
864 +               if (lhead->lh_ngranted[i] != 0)
865 +                       granted |= 1 << i;
866 +       }
867 +       list_for_each_entry_safe(tmp, tmp2,
868 +                                &lhead->lh_blocked_list, lk_blocked_list) {
869 +               /* conflict with any granted lock? */
870 +               if ((htree_lock_compat[tmp->lk_mode] & granted) != granted)
871 +                       break;
872 +
873 +               list_del_init(&tmp->lk_blocked_list);
874 +
875 +               BUG_ON(lhead->lh_nblocked[tmp->lk_mode] == 0);
876 +
877 +               lhead->lh_nblocked[tmp->lk_mode]--;
878 +               lhead->lh_ngranted[tmp->lk_mode]++;
879 +               granted |= 1 << tmp->lk_mode;
880 +
881 +               BUG_ON(tmp->lk_task == NULL);
882 +               wake_up_process(tmp->lk_task);
883 +       }
884 +}
885 +
886 +/* it's wrapper of htree_lock_internal and exported interface.
887 + * It always return 1 with granted lock if @wait is true, it can return 0
888 + * if @wait is false and locking request can't be granted immediately */
889 +int
890 +htree_lock_try(struct htree_lock *lck, struct htree_lock_head *lhead,
891 +              htree_lock_mode_t mode, int wait)
892 +{
893 +       int     rc;
894 +
895 +       BUG_ON(lck->lk_depth > lhead->lh_depth);
896 +       BUG_ON(lck->lk_head != NULL);
897 +       BUG_ON(lck->lk_task != NULL);
898 +
899 +       lck->lk_head = lhead;
900 +       lck->lk_mode = mode;
901 +
902 +       htree_spin_lock(lhead, HTREE_DEP_ROOT);
903 +       rc = htree_lock_internal(lck, wait);
904 +       if (rc != 0)
905 +               htree_spin_unlock(lhead, HTREE_DEP_ROOT);
906 +       return rc >= 0;
907 +}
908 +EXPORT_SYMBOL(htree_lock_try);
909 +
910 +/* it's wrapper of htree_unlock_internal and exported interface.
911 + * It will release all htree_node_locks and htree_lock */
912 +void
913 +htree_unlock(struct htree_lock *lck)
914 +{
915 +       BUG_ON(lck->lk_head == NULL);
916 +       BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
917 +
918 +       htree_node_release_all(lck);
919 +
920 +       htree_spin_lock(lck->lk_head, HTREE_DEP_ROOT);
921 +       htree_unlock_internal(lck);
922 +       htree_spin_unlock(lck->lk_head, HTREE_DEP_ROOT);
923 +       lck->lk_head = NULL;
924 +       lck->lk_task = NULL;
925 +}
926 +EXPORT_SYMBOL(htree_unlock);
927 +
928 +/* change lock mode */
929 +void
930 +htree_change_mode(struct htree_lock *lck, htree_lock_mode_t mode)
931 +{
932 +       BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL);
933 +       lck->lk_mode = mode;
934 +}
935 +EXPORT_SYMBOL(htree_change_mode);
936 +
937 +/* release htree lock, and lock it again with new mode.
938 + * This function will first release all htree_node_locks and htree_lock,
939 + * then try to gain htree_lock with new @mode.
940 + * It always return 1 with granted lock if @wait is true, it can return 0
941 + * if @wait is false and locking request can't be granted immediately */
942 +int
943 +htree_change_lock_try(struct htree_lock *lck, htree_lock_mode_t mode, int wait)
944 +{
945 +       struct htree_lock_head *lhead = lck->lk_head;
946 +       int rc;
947 +
948 +       BUG_ON(lhead == NULL);
949 +       BUG_ON(lck->lk_mode == mode);
950 +       BUG_ON(lck->lk_mode == HTREE_LOCK_INVAL || mode == HTREE_LOCK_INVAL);
951 +
952 +       htree_node_release_all(lck);
953 +
954 +       htree_spin_lock(lhead, HTREE_DEP_ROOT);
955 +       htree_unlock_internal(lck);
956 +       lck->lk_mode = mode;
957 +       rc = htree_lock_internal(lck, wait);
958 +       if (rc != 0)
959 +               htree_spin_unlock(lhead, HTREE_DEP_ROOT);
960 +       return rc >= 0;
961 +}
962 +EXPORT_SYMBOL(htree_change_lock_try);
963 +
964 +/* create a htree_lock head with @depth levels (number of child-locks),
965 + * it is a per resoruce structure */
966 +struct htree_lock_head *
967 +htree_lock_head_alloc(unsigned depth, unsigned hbits, unsigned priv)
968 +{
969 +       struct htree_lock_head *lhead;
970 +       int  i;
971 +
972 +       if (depth > HTREE_LOCK_DEP_MAX) {
973 +               printk(KERN_ERR "%d is larger than max htree_lock depth %d\n",
974 +                       depth, HTREE_LOCK_DEP_MAX);
975 +               return NULL;
976 +       }
977 +
978 +       lhead = kzalloc(offsetof(struct htree_lock_head,
979 +                                lh_children[depth]) + priv, GFP_NOFS);
980 +       if (lhead == NULL)
981 +               return NULL;
982 +
983 +       if (hbits < HTREE_HBITS_MIN)
984 +               lhead->lh_hbits = HTREE_HBITS_MIN;
985 +       else if (hbits > HTREE_HBITS_MAX)
986 +               lhead->lh_hbits = HTREE_HBITS_MAX;
987 +
988 +       lhead->lh_lock = 0;
989 +       lhead->lh_depth = depth;
990 +       INIT_LIST_HEAD(&lhead->lh_blocked_list);
991 +       if (priv > 0) {
992 +               lhead->lh_private = (void *)lhead +
993 +                       offsetof(struct htree_lock_head, lh_children[depth]);
994 +       }
995 +
996 +       for (i = 0; i < depth; i++) {
997 +               INIT_LIST_HEAD(&lhead->lh_children[i].lc_list);
998 +               lhead->lh_children[i].lc_events = HTREE_EVENT_DISABLE;
999 +       }
1000 +       return lhead;
1001 +}
1002 +EXPORT_SYMBOL(htree_lock_head_alloc);
1003 +
1004 +/* free the htree_lock head */
1005 +void
1006 +htree_lock_head_free(struct htree_lock_head *lhead)
1007 +{
1008 +       int     i;
1009 +
1010 +       BUG_ON(!list_empty(&lhead->lh_blocked_list));
1011 +       for (i = 0; i < lhead->lh_depth; i++)
1012 +               BUG_ON(!list_empty(&lhead->lh_children[i].lc_list));
1013 +       kfree(lhead);
1014 +}
1015 +EXPORT_SYMBOL(htree_lock_head_free);
1016 +
1017 +/* register event callback for @events of child-lock at level @dep */
1018 +void
1019 +htree_lock_event_attach(struct htree_lock_head *lhead, unsigned dep,
1020 +                       unsigned events, htree_event_cb_t callback)
1021 +{
1022 +       BUG_ON(lhead->lh_depth <= dep);
1023 +       lhead->lh_children[dep].lc_events = events;
1024 +       lhead->lh_children[dep].lc_callback = callback;
1025 +}
1026 +EXPORT_SYMBOL(htree_lock_event_attach);
1027 +
1028 +/* allocate a htree_lock, which is per-thread structure, @pbytes is some
1029 + * extra-bytes as private data for caller */
1030 +struct htree_lock *
1031 +htree_lock_alloc(unsigned depth, unsigned pbytes)
1032 +{
1033 +       struct htree_lock *lck;
1034 +       int i = offsetof(struct htree_lock, lk_nodes[depth]);
1035 +
1036 +       if (depth > HTREE_LOCK_DEP_MAX) {
1037 +               printk(KERN_ERR "%d is larger than max htree_lock depth %d\n",
1038 +                       depth, HTREE_LOCK_DEP_MAX);
1039 +               return NULL;
1040 +       }
1041 +       lck = kzalloc(i + pbytes, GFP_NOFS);
1042 +       if (lck == NULL)
1043 +               return NULL;
1044 +
1045 +       if (pbytes != 0)
1046 +               lck->lk_private = (void *)lck + i;
1047 +       lck->lk_mode = HTREE_LOCK_INVAL;
1048 +       lck->lk_depth = depth;
1049 +       INIT_LIST_HEAD(&lck->lk_blocked_list);
1050 +
1051 +       for (i = 0; i < depth; i++) {
1052 +               struct htree_lock_node *node = &lck->lk_nodes[i];
1053 +
1054 +               node->ln_mode = HTREE_LOCK_INVAL;
1055 +               INIT_LIST_HEAD(&node->ln_major_list);
1056 +               INIT_LIST_HEAD(&node->ln_minor_list);
1057 +               INIT_LIST_HEAD(&node->ln_alive_list);
1058 +               INIT_LIST_HEAD(&node->ln_blocked_list);
1059 +               INIT_LIST_HEAD(&node->ln_granted_list);
1060 +       }
1061 +
1062 +       return lck;
1063 +}
1064 +EXPORT_SYMBOL(htree_lock_alloc);
1065 +
1066 +/* free htree_lock node */
1067 +void
1068 +htree_lock_free(struct htree_lock *lck)
1069 +{
1070 +       BUG_ON(lck->lk_mode != HTREE_LOCK_INVAL);
1071 +       kfree(lck);
1072 +}
1073 +EXPORT_SYMBOL(htree_lock_free);
1074 --- linux-2.6.32-131.6.1/fs/ext4/ext4.h 2011-10-06 20:10:49.000000000 +0800
1075 +++ linux-2.6.32-131.6.1-pdo/fs/ext4/ext4.h     2011-12-08 18:25:00.000000000 +0800
1076 @@ -28,6 +28,7 @@
1077  #include <linux/mutex.h>
1078  #include <linux/timer.h>
1079  #include <linux/wait.h>
1080 +#include <linux/htree_lock.h>
1081  #include <linux/blockgroup_lock.h>
1082  #include <linux/percpu_counter.h>
1083  #ifdef __KERNEL__
1084 @@ -1277,6 +1278,7 @@ EXT4_INODE_BIT_FNS(state, state_flags)
1085  #define EXT4_FEATURE_INCOMPAT_MMP               0x0100
1086  #define EXT4_FEATURE_INCOMPAT_FLEX_BG          0x0200
1087  #define EXT4_FEATURE_INCOMPAT_DIRDATA          0x1000
1088 +#define EXT4_FEATURE_INCOMPAT_LARGEDIR         0x4000
1089  
1090  #define EXT4_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
1091  #define EXT4_FEATURE_INCOMPAT_SUPP     (EXT4_FEATURE_INCOMPAT_FILETYPE| \
1092 @@ -1286,7 +1288,8 @@ EXT4_INODE_BIT_FNS(state, state_flags)
1093                                          EXT4_FEATURE_INCOMPAT_64BIT| \
1094                                          EXT4_FEATURE_INCOMPAT_FLEX_BG| \
1095                                          EXT4_FEATURE_INCOMPAT_MMP| \
1096 -                                        EXT4_FEATURE_INCOMPAT_DIRDATA)
1097 +                                        EXT4_FEATURE_INCOMPAT_DIRDATA| \
1098 +                                        EXT4_FEATURE_INCOMPAT_LARGEDIR)
1099  
1100  #define EXT4_FEATURE_RO_COMPAT_SUPP    (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
1101                                          EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
1102 @@ -1536,6 +1539,76 @@ ext4_group_first_block_no(struct super_b
1103   */
1104  #define ERR_BAD_DX_DIR -75000
1105  
1106 +/* htree levels for ext4 */
1107 +#define EXT4_HTREE_LEVEL_COMPAT 2
1108 +#define EXT4_HTREE_LEVEL       3
1109 +
1110 +static inline int
1111 +ext4_dir_htree_level(struct super_block *sb)
1112 +{
1113 +       return EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_LARGEDIR) ?
1114 +               EXT4_HTREE_LEVEL : EXT4_HTREE_LEVEL_COMPAT;
1115 +}
1116 +
1117 +/* assume name-hash is protected by upper layer */
1118 +#define EXT4_HTREE_LOCK_HASH   0
1119 +
1120 +enum ext4_pdo_lk_types {
1121 +#if EXT4_HTREE_LOCK_HASH
1122 +       EXT4_LK_HASH,
1123 +#endif
1124 +       EXT4_LK_DX,             /* index block */
1125 +       EXT4_LK_DE,             /* directory entry block */
1126 +       EXT4_LK_SPIN,           /* spinlock */
1127 +       EXT4_LK_MAX,
1128 +};
1129 +
1130 +/* read-only bit */
1131 +#define EXT4_LB_RO(b)          (1 << (b))
1132 +/* read + write, high bits for writer */
1133 +#define EXT4_LB_RW(b)          ((1 << (b)) | (1 << (EXT4_LK_MAX + (b))))
1134 +
1135 +enum ext4_pdo_lock_bits {
1136 +       /* DX lock bits */
1137 +       EXT4_LB_DX_RO           = EXT4_LB_RO(EXT4_LK_DX),
1138 +       EXT4_LB_DX              = EXT4_LB_RW(EXT4_LK_DX),
1139 +       /* DE lock bits */
1140 +       EXT4_LB_DE_RO           = EXT4_LB_RO(EXT4_LK_DE),
1141 +       EXT4_LB_DE              = EXT4_LB_RW(EXT4_LK_DE),
1142 +       /* DX spinlock bits */
1143 +       EXT4_LB_SPIN_RO         = EXT4_LB_RO(EXT4_LK_SPIN),
1144 +       EXT4_LB_SPIN            = EXT4_LB_RW(EXT4_LK_SPIN),
1145 +       /* accurate searching */
1146 +       EXT4_LB_EXACT           = EXT4_LB_RO(EXT4_LK_MAX << 1),
1147 +};
1148 +
1149 +enum ext4_pdo_lock_opc {
1150 +       /* external */
1151 +       EXT4_HLOCK_READDIR      = (EXT4_LB_DE_RO | EXT4_LB_DX_RO),
1152 +       EXT4_HLOCK_LOOKUP       = (EXT4_LB_DE_RO | EXT4_LB_SPIN_RO |
1153 +                                  EXT4_LB_EXACT),
1154 +       EXT4_HLOCK_DEL          = (EXT4_LB_DE | EXT4_LB_SPIN_RO |
1155 +                                  EXT4_LB_EXACT),
1156 +       EXT4_HLOCK_ADD          = (EXT4_LB_DE | EXT4_LB_SPIN_RO),
1157 +
1158 +       /* internal */
1159 +       EXT4_HLOCK_LOOKUP_SAFE  = (EXT4_LB_DE_RO | EXT4_LB_DX_RO |
1160 +                                  EXT4_LB_EXACT),
1161 +       EXT4_HLOCK_DEL_SAFE     = (EXT4_LB_DE | EXT4_LB_DX_RO | EXT4_LB_EXACT),
1162 +       EXT4_HLOCK_SPLIT        = (EXT4_LB_DE | EXT4_LB_DX | EXT4_LB_SPIN),
1163 +};
1164 +
1165 +extern struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits);
1166 +#define ext4_htree_lock_head_free(lhead)       htree_lock_head_free(lhead)
1167 +
1168 +extern struct htree_lock *ext4_htree_lock_alloc(void);
1169 +#define ext4_htree_lock_free(lck)              htree_lock_free(lck)
1170 +
1171 +extern void ext4_htree_lock(struct htree_lock *lck,
1172 +                           struct htree_lock_head *lhead,
1173 +                           struct inode *dir, unsigned flags);
1174 +#define ext4_htree_unlock(lck)                  htree_unlock(lck)
1175 +
1176  void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
1177                         ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp);
1178  
1179 @@ -1769,14 +1842,16 @@ extern int ext4_htree_fill_tree(struct f
1180  extern struct inode *ext4_create_inode(handle_t *handle,
1181                                        struct inode * dir, int mode);
1182  extern int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1183 -                         struct inode *inode);
1184 +                         struct inode *inode, struct htree_lock *lck);
1185  extern int ext4_delete_entry(handle_t *handle, struct inode * dir,
1186                              struct ext4_dir_entry_2 * de_del,
1187                              struct buffer_head * bh);
1188  extern struct buffer_head * ext4_find_entry(struct inode *dir,
1189                                             const struct qstr *d_name,
1190 -                                           struct ext4_dir_entry_2 ** res_dir);
1191 -#define ll_ext4_find_entry(inode, dentry, res_dir) ext4_find_entry(inode, &(dentry)->d_name, res_dir)
1192 +                                           struct ext4_dir_entry_2 **res_dir,
1193 +                                           struct htree_lock *lck);
1194 +#define ll_ext4_find_entry(inode, dentry, res_dir, lck) \
1195 +       ext4_find_entry(inode, &(dentry)->d_name, res_dir, lck)
1196  extern int ext4_add_dot_dotdot(handle_t *handle, struct inode *dir,
1197                                struct inode *inode, const void *, const void *);
1198  extern struct buffer_head *ext4_append(handle_t *handle,
1199 @@ -1893,13 +1968,15 @@ static inline void ext4_r_blocks_count_s
1200         es->s_r_blocks_count_hi = cpu_to_le32(blk >> 32);
1201  }
1202  
1203 -static inline loff_t ext4_isize(struct ext4_inode *raw_inode)
1204 +static inline loff_t ext4_isize(struct super_block *sb,
1205 +                               struct ext4_inode *raw_inode)
1206  {
1207 -       if (S_ISREG(le16_to_cpu(raw_inode->i_mode)))
1208 +       if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_LARGEDIR) ||
1209 +           S_ISREG(le16_to_cpu(raw_inode->i_mode)))
1210                 return ((loff_t)le32_to_cpu(raw_inode->i_size_high) << 32) |
1211                         le32_to_cpu(raw_inode->i_size_lo);
1212 -       else
1213 -               return (loff_t) le32_to_cpu(raw_inode->i_size_lo);
1214 +
1215 +       return (loff_t) le32_to_cpu(raw_inode->i_size_lo);
1216  }
1217  
1218  static inline void ext4_isize_set(struct ext4_inode *raw_inode, loff_t i_size)
1219 --- linux-2.6.32-131.6.1/fs/ext4/namei.c        2011-10-06 20:10:49.000000000 +0800
1220 +++ linux-2.6.32-131.6.1-pdo/fs/ext4/namei.c    2011-12-14 22:55:28.000000000 +0800
1221 @@ -176,7 +176,7 @@ static struct dx_frame *dx_probe(const s
1222                                  struct inode *dir,
1223                                  struct dx_hash_info *hinfo,
1224                                  struct dx_frame *frame,
1225 -                                int *err);
1226 +                                struct htree_lock *lck, int *err);
1227  static void dx_release(struct dx_frame *frames);
1228  static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1229                        struct dx_hash_info *hinfo, struct dx_map_entry map[]);
1230 @@ -189,13 +189,13 @@ static void dx_insert_block(struct dx_fr
1231  static int ext4_htree_next_block(struct inode *dir, __u32 hash,
1232                                  struct dx_frame *frame,
1233                                  struct dx_frame *frames,
1234 -                                __u32 *start_hash);
1235 +                                __u32 *start_hash, struct htree_lock *lck);
1236  static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1237                 const struct qstr *d_name,
1238                 struct ext4_dir_entry_2 **res_dir,
1239 -               int *err);
1240 +               struct htree_lock *lck, int *err);
1241  static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1242 -                            struct inode *inode);
1243 +                            struct inode *inode, struct htree_lock *lck);
1244  
1245  /*
1246   * p is at least 6 bytes before the end of page
1247 @@ -225,7 +225,7 @@ struct dx_root_info * dx_get_dx_info(str
1248  
1249  static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
1250  {
1251 -       return le32_to_cpu(entry->block) & 0x00ffffff;
1252 +       return le32_to_cpu(entry->block) & 0x0fffffff;
1253  }
1254  
1255  static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
1256 @@ -368,6 +368,223 @@ struct stats dx_show_entries(struct dx_h
1257  }
1258  #endif /* DX_DEBUG */
1259  
1260 +/* private data for htree_lock */
1261 +struct ext4_dir_lock_data {
1262 +       unsigned                ld_flags;  /* bits-map for lock types */
1263 +       unsigned                ld_count;  /* # entries of the last DX block */
1264 +       struct dx_entry         ld_at_entry; /* copy of leaf dx_entry */
1265 +       struct dx_entry         *ld_at;    /* position of leaf dx_entry */
1266 +};
1267 +
1268 +#define ext4_htree_lock_data(l)        ((struct ext4_dir_lock_data *)(l)->lk_private)
1269 +
1270 +/* NB: ext4_lblk_t is 32 bits so we use high bits to identify invalid blk */
1271 +#define EXT4_HTREE_NODE_CHANGED        (0xcafeULL << 32)
1272 +
1273 +static void ext4_htree_event_cb(void *target, void *event)
1274 +{
1275 +       u64 *block = (u64 *)target;
1276 +
1277 +       if (*block == dx_get_block((struct dx_entry *)event))
1278 +               *block = EXT4_HTREE_NODE_CHANGED;
1279 +}
1280 +
1281 +struct htree_lock_head *ext4_htree_lock_head_alloc(unsigned hbits)
1282 +{
1283 +       struct htree_lock_head *lhead;
1284 +
1285 +       lhead = htree_lock_head_alloc(EXT4_LK_MAX, hbits, 0);
1286 +       if (lhead != NULL) {
1287 +               htree_lock_event_attach(lhead, EXT4_LK_SPIN, HTREE_EVENT_WR,
1288 +                                       ext4_htree_event_cb);
1289 +       }
1290 +       return lhead;
1291 +}
1292 +EXPORT_SYMBOL(ext4_htree_lock_head_alloc);
1293 +
1294 +struct htree_lock *ext4_htree_lock_alloc(void)
1295 +{
1296 +       return htree_lock_alloc(EXT4_LK_MAX,
1297 +                               sizeof(struct ext4_dir_lock_data));
1298 +}
1299 +EXPORT_SYMBOL(ext4_htree_lock_alloc);
1300 +
1301 +static htree_lock_mode_t ext4_htree_mode(unsigned flags)
1302 +{
1303 +       switch (flags) {
1304 +       default: /* 0 or unknown flags require EX lock */
1305 +               return HTREE_LOCK_EX;
1306 +       case EXT4_HLOCK_READDIR:
1307 +               return HTREE_LOCK_PR;
1308 +       case EXT4_HLOCK_LOOKUP:
1309 +               return HTREE_LOCK_CR;
1310 +       case EXT4_HLOCK_DEL:
1311 +       case EXT4_HLOCK_ADD:
1312 +               return HTREE_LOCK_CW;
1313 +       }
1314 +}
1315 +
1316 +/* return PR for read-only operations, otherwise return EX */
1317 +static inline htree_lock_mode_t ext4_htree_safe_mode(unsigned flags)
1318 +{
1319 +       int writer = (flags & EXT4_LB_DE) == EXT4_LB_DE;
1320 +
1321 +       /* 0 requires EX lock */
1322 +       return (flags == 0 || writer) ? HTREE_LOCK_EX : HTREE_LOCK_PR;
1323 +}
1324 +
1325 +static int ext4_htree_safe_locked(struct htree_lock *lck)
1326 +{
1327 +       int writer;
1328 +
1329 +       if (lck == NULL || lck->lk_mode == HTREE_LOCK_EX)
1330 +               return 1;
1331 +
1332 +       writer = (ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_DE) ==
1333 +                EXT4_LB_DE;
1334 +       if (writer) /* all readers & writers are excluded? */
1335 +               return lck->lk_mode == HTREE_LOCK_EX;
1336 +
1337 +       /* all writers are excluded? */
1338 +       return lck->lk_mode == HTREE_LOCK_PR ||
1339 +              lck->lk_mode == HTREE_LOCK_PW ||
1340 +              lck->lk_mode == HTREE_LOCK_EX;
1341 +}
1342 +
1343 +/* relock htree_lock with EX mode if it's change operation, otherwise
1344 + * relock it with PR mode. It's noop if PDO is disabled. */
1345 +static void ext4_htree_safe_relock(struct htree_lock *lck)
1346 +{
1347 +       if (!ext4_htree_safe_locked(lck)) {
1348 +               unsigned flags = ext4_htree_lock_data(lck)->ld_flags;
1349 +
1350 +               htree_change_lock(lck, ext4_htree_safe_mode(flags));
1351 +       }
1352 +}
1353 +
1354 +void ext4_htree_lock(struct htree_lock *lck, struct htree_lock_head *lhead,
1355 +                    struct inode *dir, unsigned flags)
1356 +{
1357 +       htree_lock_mode_t mode = is_dx(dir) ? ext4_htree_mode(flags) :
1358 +                                             ext4_htree_safe_mode(flags);
1359 +
1360 +       ext4_htree_lock_data(lck)->ld_flags = flags;
1361 +       htree_lock(lck, lhead, mode);
1362 +       if (!is_dx(dir))
1363 +               ext4_htree_safe_relock(lck); /* make sure it's safe locked */
1364 +}
1365 +EXPORT_SYMBOL(ext4_htree_lock);
1366 +
1367 +static int ext4_htree_node_lock(struct htree_lock *lck, struct dx_entry *at,
1368 +                               unsigned lmask, int wait, void *ev)
1369 +{
1370 +       u32     key = (at == NULL) ? 0 : dx_get_block(at);
1371 +       u32     mode;
1372 +
1373 +       /* NOOP if htree is well protected or caller doesn't require the lock */
1374 +       if (ext4_htree_safe_locked(lck) ||
1375 +          !(ext4_htree_lock_data(lck)->ld_flags & lmask))
1376 +               return 1;
1377 +
1378 +       mode = (ext4_htree_lock_data(lck)->ld_flags & lmask) == lmask ?
1379 +               HTREE_LOCK_PW : HTREE_LOCK_PR;
1380 +       while (1) {
1381 +               if (htree_node_lock_try(lck, mode, key, ffz(~lmask), wait, ev))
1382 +                       return 1;
1383 +               if (!(lmask & EXT4_LB_SPIN)) /* not a spinlock */
1384 +                       return 0;
1385 +               cpu_relax(); /* spin until granted */
1386 +       }
1387 +}
1388 +
1389 +static int ext4_htree_node_locked(struct htree_lock *lck, unsigned lmask)
1390 +{
1391 +       return ext4_htree_safe_locked(lck) ||
1392 +              htree_node_is_granted(lck, ffz(~lmask));
1393 +}
1394 +
1395 +static void ext4_htree_node_unlock(struct htree_lock *lck,
1396 +                                  unsigned lmask, void *buf)
1397 +{
1398 +       /* NB: it's safe to call mutiple times or even it's not locked */
1399 +       if (!ext4_htree_safe_locked(lck) &&
1400 +            htree_node_is_granted(lck, ffz(~lmask)))
1401 +               htree_node_unlock(lck, ffz(~lmask), buf);
1402 +}
1403 +
1404 +#define ext4_htree_dx_lock(lck, key)           \
1405 +       ext4_htree_node_lock(lck, key, EXT4_LB_DX, 1, NULL)
1406 +#define ext4_htree_dx_lock_try(lck, key)       \
1407 +       ext4_htree_node_lock(lck, key, EXT4_LB_DX, 0, NULL)
1408 +#define ext4_htree_dx_unlock(lck)              \
1409 +       ext4_htree_node_unlock(lck, EXT4_LB_DX, NULL)
1410 +#define ext4_htree_dx_locked(lck)              \
1411 +       ext4_htree_node_locked(lck, EXT4_LB_DX)
1412 +
1413 +static void ext4_htree_dx_need_lock(struct htree_lock *lck)
1414 +{
1415 +       struct ext4_dir_lock_data *ld;
1416 +
1417 +       if (ext4_htree_safe_locked(lck))
1418 +               return;
1419 +
1420 +       ld = ext4_htree_lock_data(lck);
1421 +       switch (ld->ld_flags) {
1422 +       default:
1423 +               return;
1424 +       case EXT4_HLOCK_LOOKUP:
1425 +               ld->ld_flags = EXT4_HLOCK_LOOKUP_SAFE;
1426 +               return;
1427 +       case EXT4_HLOCK_DEL:
1428 +               ld->ld_flags = EXT4_HLOCK_DEL_SAFE;
1429 +               return;
1430 +       case EXT4_HLOCK_ADD:
1431 +               ld->ld_flags = EXT4_HLOCK_SPLIT;
1432 +               return;
1433 +       }
1434 +}
1435 +
1436 +#define ext4_htree_de_lock(lck, key)           \
1437 +       ext4_htree_node_lock(lck, key, EXT4_LB_DE, 1, NULL)
1438 +#define ext4_htree_de_unlock(lck)              \
1439 +       ext4_htree_node_unlock(lck, EXT4_LB_DE, NULL)
1440 +
1441 +#define ext4_htree_spin_lock(lck, key, event)  \
1442 +       ext4_htree_node_lock(lck, key, EXT4_LB_SPIN, 0, event)
1443 +#define ext4_htree_spin_unlock(lck)            \
1444 +       ext4_htree_node_unlock(lck, EXT4_LB_SPIN, NULL)
1445 +#define ext4_htree_spin_unlock_listen(lck, p)  \
1446 +       ext4_htree_node_unlock(lck, EXT4_LB_SPIN, p)
1447 +
1448 +static void ext4_htree_spin_stop_listen(struct htree_lock *lck)
1449 +{
1450 +       if (!ext4_htree_safe_locked(lck) &&
1451 +           htree_node_is_listening(lck, ffz(~EXT4_LB_SPIN)))
1452 +               htree_node_stop_listen(lck, ffz(~EXT4_LB_SPIN));
1453 +}
1454 +
1455 +enum {
1456 +       DX_HASH_COL_IGNORE,     /* ignore collision while probing frames */
1457 +       DX_HASH_COL_YES,        /* there is collision and it does matter */
1458 +       DX_HASH_COL_NO,         /* there is no collision */
1459 +};
1460 +
1461 +static int dx_probe_hash_collision(struct htree_lock *lck,
1462 +                                  struct dx_entry *entries,
1463 +                                  struct dx_entry *at, u32 hash)
1464 +{
1465 +       if (!(ext4_htree_lock_data(lck)->ld_flags & EXT4_LB_EXACT)) {
1466 +               return DX_HASH_COL_IGNORE; /* don't care about collision */
1467 +
1468 +       } else if (at == entries + dx_get_count(entries) - 1) {
1469 +               return DX_HASH_COL_IGNORE; /* not in any leaf of this DX */
1470 +
1471 +       } else { /* hash collision? */
1472 +               return ((dx_get_hash(at + 1) & ~1) == hash) ?
1473 +                       DX_HASH_COL_YES : DX_HASH_COL_NO;
1474 +       }
1475 +}
1476 +
1477  /*
1478   * Probe for a directory leaf block to search.
1479   *
1480 @@ -379,16 +596,17 @@ struct stats dx_show_entries(struct dx_h
1481   */
1482  static struct dx_frame *
1483  dx_probe(const struct qstr *d_name, struct inode *dir,
1484 -        struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
1485 +        struct dx_hash_info *hinfo, struct dx_frame *frame_in,
1486 +        struct htree_lock *lck, int *err)
1487  {
1488         unsigned count, indirect;
1489 -       struct dx_entry *at, *entries, *p, *q, *m;
1490 +       struct dx_entry *at, *entries, *p, *q, *m, *dx = NULL;
1491         struct dx_root_info * info;
1492         struct buffer_head *bh;
1493         struct dx_frame *frame = frame_in;
1494         u32 hash;
1495  
1496 -       frame->bh = NULL;
1497 +       memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0]));
1498         if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
1499                 goto fail;
1500  
1501 @@ -418,9 +636,16 @@ dx_probe(const struct qstr *d_name, stru
1502                 goto fail;
1503         }
1504  
1505 -       if ((indirect = info->indirect_levels) > 1) {
1506 -               ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
1507 -                            info->indirect_levels);
1508 +       indirect = info->indirect_levels;
1509 +       if (indirect >= ext4_dir_htree_level(dir->i_sb)) {
1510 +               ext4_warning(dir->i_sb,
1511 +                            "Directory (ino: %lu) htree depth %#06x exceed "
1512 +                            "supported value", dir->i_ino,
1513 +                            ext4_dir_htree_level(dir->i_sb));
1514 +               if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) {
1515 +                       ext4_warning(dir->i_sb, "Enable large directory "
1516 +                                               "feature to access it");
1517 +               }
1518                 brelse(bh);
1519                 *err = ERR_BAD_DX_DIR;
1520                 goto fail;
1521 @@ -440,8 +665,15 @@ dx_probe(const struct qstr *d_name, stru
1522         dxtrace(printk("Look up %x", hash));
1523         while (1)
1524         {
1525 +               if (indirect == 0) { /* the last index level */
1526 +                       /* NB: ext4_htree_dx_lock() could be noop if
1527 +                        * DX-lock flag is not set for current operation */
1528 +                       ext4_htree_dx_lock(lck, dx);
1529 +                       ext4_htree_spin_lock(lck, dx, NULL);
1530 +               }
1531                 count = dx_get_count(entries);
1532 -               if (!count || count > dx_get_limit(entries)) {
1533 +               if (count == 0 || count > dx_get_limit(entries)) {
1534 +                       ext4_htree_spin_unlock(lck); /* release spin */
1535                         ext4_warning(dir->i_sb,
1536                                      "dx entry: no count or count > limit");
1537                         brelse(bh);
1538 @@ -482,9 +714,73 @@ dx_probe(const struct qstr *d_name, stru
1539                 frame->bh = bh;
1540                 frame->entries = entries;
1541                 frame->at = at;
1542 -               if (!indirect--) return frame;
1543 +
1544 +               if (indirect == 0) { /* the last index level */
1545 +                       struct ext4_dir_lock_data *ld;
1546 +                       u64 myblock;
1547 +
1548 +                       /* By default we only lock DE-block, however, we will
1549 +                        * also lock the last level DX-block if:
1550 +                        * a) there is hash collision
1551 +                        *    we will set DX-lock flag (a few lines below)
1552 +                        *    and redo to lock DX-block
1553 +                        *    see detail in dx_probe_hash_collision()
1554 +                        * b) it's a retry from splitting
1555 +                        *    we need to lock the last level DX-block so nobody
1556 +                        *    else can split any leaf blocks under the same
1557 +                        *    DX-block, see detail in ext4_dx_add_entry()
1558 +                        */
1559 +                       if (ext4_htree_dx_locked(lck)) {
1560 +                               /* DX-block is locked, just lock DE-block
1561 +                                * and return */
1562 +                               ext4_htree_spin_unlock(lck);
1563 +                               if (!ext4_htree_safe_locked(lck))
1564 +                                       ext4_htree_de_lock(lck, frame->at);
1565 +                               return frame;
1566 +                       }
1567 +                       /* it's pdirop and no DX lock */
1568 +                       if (dx_probe_hash_collision(lck, entries, at, hash) ==
1569 +                           DX_HASH_COL_YES) {
1570 +                               /* found hash collision, set DX-lock flag
1571 +                                * and retry to abtain DX-lock */
1572 +                               ext4_htree_spin_unlock(lck);
1573 +                               ext4_htree_dx_need_lock(lck);
1574 +                               continue;
1575 +                       }
1576 +                       ld = ext4_htree_lock_data(lck);
1577 +                       /* because I don't lock DX, so @at can't be trusted
1578 +                        * after I release spinlock so I have to save it */
1579 +                       ld->ld_at = at;
1580 +                       ld->ld_at_entry = *at;
1581 +                       ld->ld_count = dx_get_count(entries);
1582 +
1583 +                       frame->at = &ld->ld_at_entry;
1584 +                       myblock = dx_get_block(at);
1585 +
1586 +                       /* NB: ordering locking */
1587 +                       ext4_htree_spin_unlock_listen(lck, &myblock);
1588 +                       /* other thread can split this DE-block because:
1589 +                        * a) I don't have lock for the DE-block yet
1590 +                        * b) I released spinlock on DX-block
1591 +                        * if it happened I can detect it by listening
1592 +                        * splitting event on this DE-block */
1593 +                       ext4_htree_de_lock(lck, frame->at);
1594 +                       ext4_htree_spin_stop_listen(lck);
1595 +
1596 +                       if (myblock == EXT4_HTREE_NODE_CHANGED) {
1597 +                               /* someone split this DE-block before
1598 +                                * I locked it, I need to retry and lock
1599 +                                * valid DE-block */
1600 +                               ext4_htree_de_unlock(lck);
1601 +                               continue;
1602 +                       }
1603 +                       return frame;
1604 +               }
1605 +               dx = at;
1606 +               indirect--;
1607                 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
1608                         goto fail2;
1609 +
1610                 at = entries = ((struct dx_node *) bh->b_data)->entries;
1611                 if (dx_get_limit(entries) != dx_node_limit (dir)) {
1612                         ext4_warning(dir->i_sb,
1613 @@ -512,13 +808,18 @@ fail:
1614  static void dx_release (struct dx_frame *frames)
1615  {
1616         struct dx_root_info *info;
1617 +       int i;
1618 +
1619         if (frames[0].bh == NULL)
1620                 return;
1621  
1622         info = dx_get_dx_info((struct ext4_dir_entry_2*)frames[0].bh->b_data);
1623 -       if (info->indirect_levels)
1624 -               brelse(frames[1].bh);
1625 -       brelse(frames[0].bh);
1626 +       for (i = 0; i <= info->indirect_levels; i++) {
1627 +               if (frames[i].bh == NULL)
1628 +                       break;
1629 +               brelse(frames[i].bh);
1630 +               frames[i].bh = NULL;
1631 +       }
1632  }
1633  
1634  /*
1635 @@ -541,7 +842,7 @@ static void dx_release (struct dx_frame 
1636  static int ext4_htree_next_block(struct inode *dir, __u32 hash,
1637                                  struct dx_frame *frame,
1638                                  struct dx_frame *frames,
1639 -                                __u32 *start_hash)
1640 +                                __u32 *start_hash, struct htree_lock *lck)
1641  {
1642         struct dx_frame *p;
1643         struct buffer_head *bh;
1644 @@ -556,12 +857,22 @@ static int ext4_htree_next_block(struct 
1645          * this loop, num_frames indicates the number of interior
1646          * nodes need to be read.
1647          */
1648 +       ext4_htree_de_unlock(lck);
1649         while (1) {
1650 -               if (++(p->at) < p->entries + dx_get_count(p->entries))
1651 -                       break;
1652 +               if (num_frames > 0 || ext4_htree_dx_locked(lck)) {
1653 +                       /* num_frames > 0 :
1654 +                        *   DX block
1655 +                        * ext4_htree_dx_locked:
1656 +                        *   frame->at is reliable pointer returned by dx_probe,
1657 +                        *   otherwise dx_probe already knew no collision */
1658 +                       if (++(p->at) < p->entries + dx_get_count(p->entries))
1659 +                               break;
1660 +               }
1661                 if (p == frames)
1662                         return 0;
1663                 num_frames++;
1664 +               if (num_frames == 1)
1665 +                       ext4_htree_dx_unlock(lck);
1666                 p--;
1667         }
1668  
1669 @@ -584,6 +895,13 @@ static int ext4_htree_next_block(struct 
1670          * block so no check is necessary
1671          */
1672         while (num_frames--) {
1673 +               if (num_frames == 0) {
1674 +                       /* it's not always necessary, we just don't want to
1675 +                        * detect hash collision again */
1676 +                       ext4_htree_dx_need_lock(lck);
1677 +                       ext4_htree_dx_lock(lck, p->at);
1678 +               }
1679 +
1680                 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
1681                                       0, &err)))
1682                         return err; /* Failure */
1683 @@ -592,6 +910,7 @@ static int ext4_htree_next_block(struct 
1684                 p->bh = bh;
1685                 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
1686         }
1687 +       ext4_htree_de_lock(lck, p->at);
1688         return 1;
1689  }
1690  
1691 @@ -661,7 +980,7 @@ int ext4_htree_fill_tree(struct file *di
1692  {
1693         struct dx_hash_info hinfo;
1694         struct ext4_dir_entry_2 *de;
1695 -       struct dx_frame frames[2], *frame;
1696 +       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1697         struct inode *dir;
1698         ext4_lblk_t block;
1699         int count = 0;
1700 @@ -684,10 +1003,10 @@ int ext4_htree_fill_tree(struct file *di
1701         }
1702         hinfo.hash = start_hash;
1703         hinfo.minor_hash = 0;
1704 -       frame = dx_probe(NULL, dir, &hinfo, frames, &err);
1705 +       /* assume it's PR locked */
1706 +       frame = dx_probe(NULL, dir, &hinfo, frames, NULL, &err);
1707         if (!frame)
1708                 return err;
1709 -
1710         /* Add '.' and '..' from the htree header */
1711         if (!start_hash && !start_minor_hash) {
1712                 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
1713 @@ -714,7 +1033,7 @@ int ext4_htree_fill_tree(struct file *di
1714                 count += ret;
1715                 hashval = ~0;
1716                 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
1717 -                                           frame, frames, &hashval);
1718 +                                           frame, frames, &hashval, NULL);
1719                 *next_hash = hashval;
1720                 if (ret < 0) {
1721                         err = ret;
1722 @@ -814,9 +1133,17 @@ static void dx_insert_block(struct dx_fr
1723  
1724  static void ext4_update_dx_flag(struct inode *inode)
1725  {
1726 +       /* Disable it for ldiskfs, because going from a DX directory to
1727 +        * a non-DX directory while it is in use will completely break
1728 +        * the htree-locking.
1729 +        * If we really want to support this operation in the future,
1730 +        * we need to exclusively lock the directory at here which will
1731 +        * increase complexity of code */
1732 +#if 0
1733         if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
1734                                      EXT4_FEATURE_COMPAT_DIR_INDEX))
1735                 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
1736 +#endif
1737  }
1738  
1739  /*
1740 @@ -889,8 +1216,9 @@ static inline int search_dirblock(struct
1741   * to brelse() it when appropriate.
1742   */
1743  struct buffer_head * ext4_find_entry(struct inode *dir,
1744 -                                     const struct qstr *d_name,
1745 -                                     struct ext4_dir_entry_2 ** res_dir)
1746 +                                    const struct qstr *d_name,
1747 +                                    struct ext4_dir_entry_2 **res_dir,
1748 +                                    struct htree_lock *lck)
1749  {
1750         struct super_block *sb;
1751         struct buffer_head *bh_use[NAMEI_RA_SIZE];
1752 @@ -911,7 +1239,7 @@ struct buffer_head * ext4_find_entry(str
1753         if (namelen > EXT4_NAME_LEN)
1754                 return NULL;
1755         if (is_dx(dir)) {
1756 -               bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1757 +               bh = ext4_dx_find_entry(dir, d_name, res_dir, lck, &err);
1758                 /*
1759                  * On success, or if the error was file not found,
1760                  * return.  Otherwise, fall back to doing a search the
1761 @@ -921,6 +1249,7 @@ struct buffer_head * ext4_find_entry(str
1762                         return bh;
1763                 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1764                                "falling back\n"));
1765 +               ext4_htree_safe_relock(lck);
1766         }
1767         nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1768         start = EXT4_I(dir)->i_dir_start_lookup;
1769 @@ -998,13 +1327,15 @@ cleanup_and_exit:
1770  }
1771  EXPORT_SYMBOL(ext4_find_entry);
1772  
1773 -static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1774 -                      struct ext4_dir_entry_2 **res_dir, int *err)
1775 +static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
1776 +                               const struct qstr *d_name,
1777 +                               struct ext4_dir_entry_2 **res_dir,
1778 +                               struct htree_lock *lck, int *err)
1779  {
1780         struct super_block * sb;
1781         struct dx_hash_info     hinfo;
1782         u32 hash;
1783 -       struct dx_frame frames[2], *frame;
1784 +       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1785         struct ext4_dir_entry_2 *de, *top;
1786         struct buffer_head *bh;
1787         ext4_lblk_t block;
1788 @@ -1015,13 +1346,16 @@ static struct buffer_head * ext4_dx_find
1789         sb = dir->i_sb;
1790         /* NFS may look up ".." - look at dx_root directory block */
1791         if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
1792 -               if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1793 +               if (!(frame = dx_probe(d_name, dir, &hinfo, frames, lck, err)))
1794                         return NULL;
1795         } else {
1796                 frame = frames;
1797                 frame->bh = NULL;                       /* for dx_release() */
1798                 frame->at = (struct dx_entry *)frames;  /* hack for zero entry*/
1799                 dx_set_block(frame->at, 0);             /* dx_root block is 0 */
1800 +               /* "." and ".." are stored in root DX lock */
1801 +               ext4_htree_dx_need_lock(lck);
1802 +               ext4_htree_dx_lock(lck, NULL);
1803         }
1804         hash = hinfo.hash;
1805         do {
1806 @@ -1050,7 +1384,7 @@ static struct buffer_head * ext4_dx_find
1807                 brelse(bh);
1808                 /* Check to see if we should continue to search */
1809                 retval = ext4_htree_next_block(dir, hash, frame,
1810 -                                              frames, NULL);
1811 +                                              frames, NULL, lck);
1812                 if (retval < 0) {
1813                         ext4_warning(sb,
1814                              "error reading index page in directory #%lu",
1815 @@ -1076,7 +1410,7 @@ static struct dentry *ext4_lookup(struct
1816         if (dentry->d_name.len > EXT4_NAME_LEN)
1817                 return ERR_PTR(-ENAMETOOLONG);
1818  
1819 -       bh = ext4_find_entry(dir, &dentry->d_name, &de);
1820 +       bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
1821         inode = NULL;
1822         if (bh) {
1823                 __u32 ino = le32_to_cpu(de->inode);
1824 @@ -1144,7 +1478,7 @@ struct dentry *ext4_get_parent(struct de
1825         struct ext4_dir_entry_2 * de;
1826         struct buffer_head *bh;
1827  
1828 -       bh = ext4_find_entry(child->d_inode, &dotdot, &de);
1829 +       bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
1830         inode = NULL;
1831         if (!bh)
1832                 return ERR_PTR(-ENOENT);
1833 @@ -1233,8 +1567,9 @@ static struct ext4_dir_entry_2* dx_pack_
1834   * Returns pointer to de in block into which the new entry will be inserted.
1835   */
1836  static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1837 -                       struct buffer_head **bh,struct dx_frame *frame,
1838 -                       struct dx_hash_info *hinfo, int *error)
1839 +                       struct buffer_head **bh, struct dx_frame *frames,
1840 +                       struct dx_frame *frame, struct dx_hash_info *hinfo,
1841 +                       struct htree_lock *lck, int *error)
1842  {
1843         unsigned blocksize = dir->i_sb->s_blocksize;
1844         unsigned count, continued;
1845 @@ -1291,7 +1626,14 @@ static struct ext4_dir_entry_2 *do_split
1846                                         hash2, split, count-split));
1847  
1848         /* Fancy dance to stay within two buffers */
1849 -       de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1850 +       if (hinfo->hash < hash2) {
1851 +               de2 = dx_move_dirents(data1, data2, map + split,
1852 +                                     count - split, blocksize);
1853 +       } else {
1854 +               /* make sure we will add entry to the same block which
1855 +                * we have already locked */
1856 +               de2 = dx_move_dirents(data1, data2, map, split, blocksize);
1857 +       }
1858         de = dx_pack_dirents(data1, blocksize);
1859         de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
1860                                            blocksize);
1861 @@ -1300,13 +1642,21 @@ static struct ext4_dir_entry_2 *do_split
1862         dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1863         dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1864  
1865 -       /* Which block gets the new entry? */
1866 -       if (hinfo->hash >= hash2)
1867 -       {
1868 -               swap(*bh, bh2);
1869 -               de = de2;
1870 +       ext4_htree_spin_lock(lck, frame > frames ? (frame - 1)->at : NULL,
1871 +                            frame->at); /* notify block is being split */
1872 +       if (hinfo->hash < hash2) {
1873 +               dx_insert_block(frame, hash2 + continued, newblock);
1874 +
1875 +       } else {
1876 +               /* switch block number */
1877 +               dx_insert_block(frame, hash2 + continued,
1878 +                               dx_get_block(frame->at));
1879 +               dx_set_block(frame->at, newblock);
1880 +               (frame->at)++;
1881         }
1882 -       dx_insert_block(frame, hash2 + continued, newblock);
1883 +       ext4_htree_spin_unlock(lck);
1884 +       ext4_htree_dx_unlock(lck);
1885 +
1886         err = ext4_handle_dirty_metadata(handle, dir, bh2);
1887         if (err)
1888                 goto journal_error;
1889 @@ -1418,7 +1768,7 @@ static int add_dirent_to_buf(handle_t *h
1890         if (!IS_NOCMTIME(dir))
1891                 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1892         ext4_update_dx_flag(dir);
1893 -       dir->i_version++;
1894 +       inode_inc_iversion(dir);
1895         ext4_mark_inode_dirty(handle, dir);
1896         BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1897         err = ext4_handle_dirty_metadata(handle, dir, bh);
1898 @@ -1438,7 +1788,7 @@ static int make_indexed_dir(handle_t *ha
1899         const char      *name = dentry->d_name.name;
1900         int             namelen = dentry->d_name.len;
1901         struct buffer_head *bh2;
1902 -       struct dx_frame frames[2], *frame;
1903 +       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1904         struct dx_entry *entries;
1905         struct ext4_dir_entry_2 *de, *de2, *dot_de, *dotdot_de;
1906         char            *data1, *top;
1907 @@ -1517,7 +1867,7 @@ static int make_indexed_dir(handle_t *ha
1908         frame->at = entries;
1909         frame->bh = bh;
1910         bh = bh2;
1911 -       de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1912 +       de = do_split(handle,dir, &bh, frames, frame, &hinfo, NULL, &retval);
1913         dx_release (frames);
1914         if (!(de))
1915                 return retval;
1916 @@ -1616,7 +1966,7 @@ out:
1917   * the entry, as someone else might have used it while you slept.
1918   */
1919  int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1920 -                  struct inode *inode)
1921 +                  struct inode *inode, struct htree_lock *lck)
1922  {
1923         struct inode *dir = dentry->d_parent->d_inode;
1924         struct buffer_head *bh;
1925 @@ -1635,9 +1985,10 @@ int ext4_add_entry(handle_t *handle, str
1926                 if (dentry->d_name.len == 2 &&
1927                     memcmp(dentry->d_name.name, "..", 2) == 0)
1928                         return ext4_update_dotdot(handle, dentry, inode);
1929 -               retval = ext4_dx_add_entry(handle, dentry, inode);
1930 +               retval = ext4_dx_add_entry(handle, dentry, inode, lck);
1931                 if (!retval || (retval != ERR_BAD_DX_DIR))
1932                         return retval;
1933 +               ext4_htree_safe_relock(lck);
1934                 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1935                 dx_fallback++;
1936                 ext4_mark_inode_dirty(handle, dir);
1937 @@ -1674,18 +2025,21 @@ EXPORT_SYMBOL(ext4_add_entry);
1938   * Returns 0 for success, or a negative error value
1939   */
1940  static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1941 -                            struct inode *inode)
1942 +                            struct inode *inode, struct htree_lock *lck)
1943  {
1944 -       struct dx_frame frames[2], *frame;
1945 +       struct dx_frame frames[EXT4_HTREE_LEVEL], *frame;
1946         struct dx_entry *entries, *at;
1947         struct dx_hash_info hinfo;
1948         struct buffer_head *bh;
1949         struct inode *dir = dentry->d_parent->d_inode;
1950         struct super_block *sb = dir->i_sb;
1951         struct ext4_dir_entry_2 *de;
1952 +       int restart;
1953         int err;
1954  
1955 -       frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1956 +again:
1957 +       restart = 0;
1958 +       frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, lck, &err);
1959         if (!frame)
1960                 return err;
1961         entries = frame->entries;
1962 @@ -1694,33 +2048,53 @@ static int ext4_dx_add_entry(handle_t *h
1963         if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1964                 goto cleanup;
1965  
1966 -       BUFFER_TRACE(bh, "get_write_access");
1967 -       err = ext4_journal_get_write_access(handle, bh);
1968 -       if (err)
1969 -               goto journal_error;
1970 -
1971         err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1972         if (err != -ENOSPC)
1973                 goto cleanup;
1974  
1975 +       err = 0;
1976         /* Block full, should compress but for now just split */
1977         dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
1978                        dx_get_count(entries), dx_get_limit(entries)));
1979         /* Need to split index? */
1980         if (dx_get_count(entries) == dx_get_limit(entries)) {
1981                 ext4_lblk_t newblock;
1982 -               unsigned icount = dx_get_count(entries);
1983 -               int levels = frame - frames;
1984 +               int levels = frame - frames + 1;
1985 +               unsigned icount;
1986 +               int add_level = 1;
1987                 struct dx_entry *entries2;
1988                 struct dx_node *node2;
1989                 struct buffer_head *bh2;
1990  
1991 -               if (levels && (dx_get_count(frames->entries) ==
1992 -                              dx_get_limit(frames->entries))) {
1993 -                       ext4_warning(sb, "Directory index full!");
1994 +               if (!ext4_htree_safe_locked(lck)) { /* retry with EX lock */
1995 +                       ext4_htree_safe_relock(lck);
1996 +                       restart = 1;
1997 +                       goto cleanup;
1998 +               }
1999 +               while (frame > frames) {
2000 +                       if (dx_get_count((frame - 1)->entries) <
2001 +                           dx_get_limit((frame - 1)->entries)) {
2002 +                               add_level = 0;
2003 +                               break;
2004 +                       }
2005 +                       frame--; /* split higher index block */
2006 +                       at = frame->at;
2007 +                       entries = frame->entries;
2008 +                       restart = 1;
2009 +               }
2010 +               if (add_level && levels == ext4_dir_htree_level(sb)) {
2011 +                       ext4_warning(sb, "Directory (ino: %lu) index full, "
2012 +                                        "reach max htree level :%d",
2013 +                                        dir->i_ino, levels);
2014 +                       if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) {
2015 +                               ext4_warning(sb, "Large directory feature is"
2016 +                                                "not enabled on this "
2017 +                                                "filesystem");
2018 +                       }
2019                         err = -ENOSPC;
2020                         goto cleanup;
2021                 }
2022 +               icount = dx_get_count(entries);
2023                 bh2 = ext4_append (handle, dir, &newblock, &err);
2024                 if (!(bh2))
2025                         goto cleanup;
2026 @@ -1733,7 +2107,7 @@ static int ext4_dx_add_entry(handle_t *h
2027                 err = ext4_journal_get_write_access(handle, frame->bh);
2028                 if (err)
2029                         goto journal_error;
2030 -               if (levels) {
2031 +               if (!add_level) {
2032                         unsigned icount1 = icount/2, icount2 = icount - icount1;
2033                         unsigned hash2 = dx_get_hash(entries + icount1);
2034                         dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2035 @@ -1741,7 +2115,7 @@ static int ext4_dx_add_entry(handle_t *h
2036  
2037                         BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2038                         err = ext4_journal_get_write_access(handle,
2039 -                                                            frames[0].bh);
2040 +                                                           (frame - 1)->bh);
2041                         if (err)
2042                                 goto journal_error;
2043  
2044 @@ -1757,18 +2131,24 @@ static int ext4_dx_add_entry(handle_t *h
2045                                 frame->entries = entries = entries2;
2046                                 swap(frame->bh, bh2);
2047                         }
2048 -                       dx_insert_block(frames + 0, hash2, newblock);
2049 -                       dxtrace(dx_show_index("node", frames[1].entries));
2050 +                       dx_insert_block((frame - 1), hash2, newblock);
2051 +                       dxtrace(dx_show_index("node", frame->entries));
2052                         dxtrace(dx_show_index("node",
2053                                ((struct dx_node *) bh2->b_data)->entries));
2054                         err = ext4_handle_dirty_metadata(handle, inode, bh2);
2055                         if (err)
2056                                 goto journal_error;
2057                         brelse (bh2);
2058 +                       ext4_handle_dirty_metadata(handle, inode,
2059 +                                                  (frame - 1)->bh);
2060 +                       if (restart) {
2061 +                               ext4_handle_dirty_metadata(handle, inode,
2062 +                                                          frame->bh);
2063 +                               goto cleanup;
2064 +                       }
2065                 } else {
2066                         struct dx_root_info * info;
2067 -                       dxtrace(printk(KERN_DEBUG
2068 -                                      "Creating second level index...\n"));
2069 +
2070                         memcpy((char *) entries2, (char *) entries,
2071                                icount * sizeof(struct dx_entry));
2072                         dx_set_limit(entries2, dx_node_limit(dir));
2073 @@ -1778,32 +2158,60 @@ static int ext4_dx_add_entry(handle_t *h
2074                         dx_set_block(entries + 0, newblock);
2075                         info = dx_get_dx_info((struct ext4_dir_entry_2*)
2076                                         frames[0].bh->b_data);
2077 -                       info->indirect_levels = 1;
2078 +                       info->indirect_levels += 1;
2079 +                       dxtrace(printk(KERN_DEBUG
2080 +                                      "Creating %d level index...\n",
2081 +                                      info->indirect_levels));
2082 +                       ext4_handle_dirty_metadata(handle, inode, frame->bh);
2083 +                       ext4_handle_dirty_metadata(handle, inode, bh2);
2084 +                       brelse(bh2);
2085 +                       restart = 1;
2086 +                       goto cleanup;
2087 +               }
2088 +       } else if (!ext4_htree_dx_locked(lck)) {
2089 +               struct ext4_dir_lock_data *ld = ext4_htree_lock_data(lck);
2090  
2091 -                       /* Add new access path frame */
2092 -                       frame = frames + 1;
2093 -                       frame->at = at = at - entries + entries2;
2094 -                       frame->entries = entries = entries2;
2095 -                       frame->bh = bh2;
2096 -                       err = ext4_journal_get_write_access(handle,
2097 -                                                            frame->bh);
2098 -                       if (err)
2099 -                               goto journal_error;
2100 +               /* not well protected, require DX lock */
2101 +               ext4_htree_dx_need_lock(lck);
2102 +               at = frame > frames ? (frame - 1)->at : NULL;
2103 +
2104 +               /* NB: no risk of deadlock because it's just a try.
2105 +                *
2106 +                * NB: we check ld_count for twice, the first time before
2107 +                * having DX lock, the second time after holding DX lock.
2108 +                *
2109 +                * NB: We never free blocks for directory so far, which
2110 +                * means value returned by dx_get_count() should equal to
2111 +                * ld->ld_count if nobody split any DE-block under @at,
2112 +                * and ld->ld_at still points to valid dx_entry. */
2113 +               if ((ld->ld_count != dx_get_count(entries)) ||
2114 +                   !ext4_htree_dx_lock_try(lck, at) ||
2115 +                   (ld->ld_count != dx_get_count(entries))) {
2116 +                       restart = 1;
2117 +                       goto cleanup;
2118                 }
2119 -               ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
2120 +               /* OK, I've got DX lock and nothing changed */
2121 +               frame->at = ld->ld_at;
2122         }
2123 -       de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2124 +       de = do_split(handle, dir, &bh, frames, frame, &hinfo, lck, &err);
2125         if (!de)
2126                 goto cleanup;
2127 +
2128         err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2129         goto cleanup;
2130  
2131  journal_error:
2132         ext4_std_error(dir->i_sb, err);
2133  cleanup:
2134 +       ext4_htree_dx_unlock(lck);
2135 +       ext4_htree_de_unlock(lck);
2136         if (bh)
2137                 brelse(bh);
2138         dx_release(frames);
2139 +       /* @restart is true means htree-path has been changed, we need to
2140 +        * repeat dx_probe() to find out valid htree-path */
2141 +       if (restart && err == 0)
2142 +               goto again;
2143         return err;
2144  }
2145  
2146 @@ -1838,7 +2246,7 @@ int ext4_delete_entry(handle_t *handle,
2147                                         blocksize);
2148                         else
2149                                 de->inode = 0;
2150 -                       dir->i_version++;
2151 +                       inode_inc_iversion(dir);
2152                         BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2153                         ext4_handle_dirty_metadata(handle, dir, bh);
2154                         return 0;
2155 @@ -1882,7 +2290,7 @@ static void ext4_dec_count(handle_t *han
2156  static int ext4_add_nondir(handle_t *handle,
2157                 struct dentry *dentry, struct inode *inode)
2158  {
2159 -       int err = ext4_add_entry(handle, dentry, inode);
2160 +       int err = ext4_add_entry(handle, dentry, inode, NULL);
2161         if (!err) {
2162                 ext4_mark_inode_dirty(handle, inode);
2163                 d_instantiate(dentry, inode);
2164 @@ -2112,7 +2520,7 @@ retry:
2165                 goto out_stop;
2166         }
2167  
2168 -       err = ext4_add_entry(handle, dentry, inode);
2169 +       err = ext4_add_entry(handle, dentry, inode, NULL);
2170         if (err) {
2171                 clear_nlink(inode);
2172                 unlock_new_inode(inode);
2173 @@ -2381,7 +2789,7 @@ static int ext4_rmdir(struct inode *dir,
2174                 return PTR_ERR(handle);
2175  
2176         retval = -ENOENT;
2177 -       bh = ext4_find_entry(dir, &dentry->d_name, &de);
2178 +       bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2179         if (!bh)
2180                 goto end_rmdir;
2181  
2182 @@ -2443,7 +2851,7 @@ static int ext4_unlink(struct inode *dir
2183                 ext4_handle_sync(handle);
2184  
2185         retval = -ENOENT;
2186 -       bh = ext4_find_entry(dir, &dentry->d_name, &de);
2187 +       bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2188         if (!bh)
2189                 goto end_unlink;
2190  
2191 @@ -2567,7 +2975,7 @@ retry:
2192         ext4_inc_count(handle, inode);
2193         atomic_inc(&inode->i_count);
2194  
2195 -       err = ext4_add_entry(handle, dentry, inode);
2196 +       err = ext4_add_entry(handle, dentry, inode, NULL);
2197         if (!err) {
2198                 ext4_mark_inode_dirty(handle, inode);
2199                 d_instantiate(dentry, inode);
2200 @@ -2612,7 +3020,7 @@ static int ext4_rename(struct inode *old
2201         if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2202                 ext4_handle_sync(handle);
2203  
2204 -       old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2205 +       old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de, NULL);
2206         /*
2207          *  Check for inode number is _not_ due to possible IO errors.
2208          *  We might rmdir the source, keep it as pwd of some process
2209 @@ -2625,7 +3033,7 @@ static int ext4_rename(struct inode *old
2210                 goto end_rename;
2211  
2212         new_inode = new_dentry->d_inode;
2213 -       new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
2214 +       new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de, NULL);
2215         if (new_bh) {
2216                 if (!new_inode) {
2217                         brelse(new_bh);
2218 @@ -2651,7 +3059,7 @@ static int ext4_rename(struct inode *old
2219                         goto end_rename;
2220         }
2221         if (!new_bh) {
2222 -               retval = ext4_add_entry(handle, new_dentry, old_inode);
2223 +               retval = ext4_add_entry(handle, new_dentry, old_inode, NULL);
2224                 if (retval)
2225                         goto end_rename;
2226         } else {
2227 @@ -2693,7 +3101,8 @@ static int ext4_rename(struct inode *old
2228                 struct buffer_head *old_bh2;
2229                 struct ext4_dir_entry_2 *old_de2;
2230  
2231 -               old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
2232 +               old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name,
2233 +                                         &old_de2, NULL);
2234                 if (old_bh2) {
2235                         retval = ext4_delete_entry(handle, old_dir,
2236                                                    old_de2, old_bh2);
2237 --- linux-2.6.32-131.6.1/fs/ext4/inode.c        2011-10-06 20:10:49.000000000 +0800
2238 +++ linux-2.6.32-131.6.1-pdo/fs/ext4/inode.c    2011-12-01 22:02:11.000000000 +0800
2239 @@ -5112,7 +5112,7 @@ struct inode *ext4_iget(struct super_blo
2240         if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
2241                 ei->i_file_acl |=
2242                         ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
2243 -       inode->i_size = ext4_isize(raw_inode);
2244 +       inode->i_size = ext4_isize(sb, raw_inode);
2245         ei->i_disksize = inode->i_size;
2246  #ifdef CONFIG_QUOTA
2247         ei->i_reserved_quota = 0;
2248 --- linux-2.6.32-131.6.1/fs/ext4/Makefile       2011-10-06 20:10:49.000000000 +0800
2249 +++ linux-2.6.32-131.6.1-pdo/fs/ext4/Makefile   2011-10-06 12:21:30.000000000 +0800
2250 @@ -7,7 +7,7 @@ obj-$(CONFIG_EXT4_FS) += ext4.o
2251  ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
2252                 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
2253                 ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
2254 -               mmp.o dynlocks.o
2255 +               htree_lock.o mmp.o dynlocks.o
2256  
2257  ext4-$(CONFIG_EXT4_FS_XATTR)           += xattr.o xattr_user.o xattr_trusted.o
2258  ext4-$(CONFIG_EXT4_FS_POSIX_ACL)       += acl.o