Whamcloud - gitweb
LU-1212 ptlrpc: ptlrpc_grow_req_bufs is racy
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-journal-callback-rhel5.patch
1 Index: linux-stage/fs/ext4/ext4_jbd2.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4_jbd2.h
4 +++ linux-stage/fs/ext4/ext4_jbd2.h
5 @@ -106,6 +106,80 @@
6  #define EXT4_MAXQUOTAS_INIT_BLOCKS(sb) (MAXQUOTAS*EXT4_QUOTA_INIT_BLOCKS(sb))
7  #define EXT4_MAXQUOTAS_DEL_BLOCKS(sb) (MAXQUOTAS*EXT4_QUOTA_DEL_BLOCKS(sb))
8  
9 +/**
10 + *   struct ext4_journal_cb_entry - Base structure for callback information.
11 + *
12 + *   This struct is a 'seed' structure for a using with your own callback
13 + *   structs. If you are using callbacks you must allocate one of these
14 + *   or another struct of your own definition which has this struct
15 + *   as it's first element and pass it to ext4_journal_callback_add().
16 + */
17 +struct ext4_journal_cb_entry {
18 +       /* list information for other callbacks attached to the same handle */
19 +       struct list_head jce_list;
20 +
21 +       /*  Function to call with this callback structure */
22 +       void (*jce_func)(struct super_block *sb,
23 +                        struct ext4_journal_cb_entry *jce, int error);
24 +
25 +       /* user data goes here */
26 +};
27 +
28 +/**
29 + * ext4_journal_callback_add: add a function to call after transaction commit
30 + * @handle: active journal transaction handle to register callback on
31 + * @func: callback function to call after the transaction has committed:
32 + *        @sb: superblock of current filesystem for transaction
33 + *        @jce: returned journal callback data
34 + *        @rc: journal state at commit (0 = transaction committed properly)
35 + * @jce: journal callback data (internal and function private data struct)
36 + *
37 + * The registered function will be called in the context of the journal thread
38 + * after the transaction for which the handle was created has completed.
39 + *
40 + * No locks are held when the callback function is called, so it is safe to
41 + * call blocking functions from within the callback, but the callback should
42 + * not block or run for too long, or the filesystem will be blocked waiting for
43 + * the next transaction to commit. No journaling functions can be used, or
44 + * there is a risk of deadlock.
45 + *
46 + * There is no guaranteed calling order of multiple registered callbacks on
47 + * the same transaction.
48 + */
49 +static inline void ext4_journal_callback_add(handle_t *handle,
50 +                       void (*func)(struct super_block *sb,
51 +                                    struct ext4_journal_cb_entry *jce,
52 +                                    int rc),
53 +                       struct ext4_journal_cb_entry *jce)
54 +{
55 +       struct ext4_sb_info *sbi =
56 +                       EXT4_SB(handle->h_transaction->t_journal->j_private);
57 +
58 +       /* Add the jce to transaction's private list */
59 +       jce->jce_func = func;
60 +       spin_lock(&sbi->s_md_lock);
61 +       list_add_tail(&jce->jce_list, &handle->h_transaction->t_private_list);
62 +       spin_unlock(&sbi->s_md_lock);
63 +}
64 +
65 +/**
66 + * ext4_journal_callback_del: delete a registered callback
67 + * @handle: active journal transaction handle on which callback was registered
68 + * @jce: registered journal callback entry to unregister
69 + */
70 +static inline void ext4_journal_callback_del(handle_t *handle,
71 +                                            struct ext4_journal_cb_entry *jce)
72 +{
73 +       struct ext4_sb_info *sbi =
74 +                       EXT4_SB(handle->h_transaction->t_journal->j_private);
75 +
76 +       spin_lock(&sbi->s_md_lock);
77 +       list_del_init(&jce->jce_list);
78 +       spin_unlock(&sbi->s_md_lock);
79 +}
80 +
81 +#define HAVE_EXT4_JOURNAL_CALLBACK_ADD
82 +
83  int
84  ext4_mark_iloc_dirty(handle_t *handle,
85                      struct inode *inode,
86 Index: linux-stage/fs/ext4/mballoc.c
87 ===================================================================
88 --- linux-stage.orig/fs/ext4/mballoc.c
89 +++ linux-stage/fs/ext4/mballoc.c
90 @@ -21,6 +21,7 @@
91   * mballoc.c contains the multiblocks allocation routines
92   */
93  
94 +#include "ext4_jbd2.h"
95  #include "mballoc.h"
96  #include <linux/debugfs.h>
97  
98 @@ -335,14 +336,12 @@
99   */
100  static struct kmem_cache *ext4_pspace_cachep;
101  static struct kmem_cache *ext4_ac_cachep;
102 -static struct kmem_cache *ext4_free_ext_cachep;
103 +static struct kmem_cache *ext4_free_data_cachep;
104  static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
105                                         ext4_group_t group);
106  static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
107                                                 ext4_group_t group);
108 -static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
109 -
110 -
111 +static void ext4_free_data_callback(struct super_block *sb, struct ext4_journal_cb_entry *jce, int error);
112  
113  static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
114  {
115 @@ -2942,8 +2941,6 @@ int ext4_mb_init(struct super_block *sb,
116  
117         ext4_mb_history_init(sb);
118  
119 -       if (sbi->s_journal)
120 -               sbi->s_journal->j_commit_callback = release_blocks_on_commit;
121         return 0;
122  }
123  
124 @@ -3032,46 +3029,42 @@ int ext4_mb_release(struct super_block *
125   * This function is called by the jbd2 layer once the commit has finished,
126   * so we know we can free the blocks that were released with that commit.
127   */
128 -static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
129 +static void ext4_free_data_callback(struct super_block *sb,
130 +                                   struct ext4_journal_cb_entry *jce,
131 +                                   int rc)
132  {
133 -       struct super_block *sb = journal->j_private;
134 +       struct ext4_free_data *entry = (struct ext4_free_data *)jce;
135         struct ext4_buddy e4b;
136         struct ext4_group_info *db;
137         int err, count = 0, count2 = 0;
138 -       struct ext4_free_data *entry;
139 -       struct list_head *l, *ltmp;
140  
141 -       list_for_each_safe(l, ltmp, &txn->t_private_list) {
142 -               entry = list_entry(l, struct ext4_free_data, list);
143 -
144 -               mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
145 -                        entry->count, entry->group, entry);
146 -
147 -               err = ext4_mb_load_buddy(sb, entry->group, &e4b);
148 -               /* we expect to find existing buddy because it's pinned */
149 -               BUG_ON(err != 0);
150 -
151 -               db = e4b.bd_info;
152 -               /* there are blocks to put in buddy to make them really free */
153 -               count += entry->count;
154 -               count2++;
155 -               ext4_lock_group(sb, entry->group);
156 -               /* Take it out of per group rb tree */
157 -               rb_erase(&entry->node, &(db->bb_free_root));
158 -               mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
159 -
160 -               if (!db->bb_free_root.rb_node) {
161 -                       /* No more items in the per group rb tree
162 -                        * balance refcounts from ext4_mb_free_metadata()
163 -                        */
164 -                       page_cache_release(e4b.bd_buddy_page);
165 -                       page_cache_release(e4b.bd_bitmap_page);
166 -               }
167 -               ext4_unlock_group(sb, entry->group);
168 +       mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
169 +                entry->efd_count, entry->efd_group, entry);
170  
171 -               kmem_cache_free(ext4_free_ext_cachep, entry);
172 -               ext4_mb_unload_buddy(&e4b);
173 +       err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
174 +       /* we expect to find existing buddy because it's pinned */
175 +       BUG_ON(err != 0);
176 +
177 +       db = e4b.bd_info;
178 +       /* there are blocks to put in buddy to make them really free */
179 +       count += entry->efd_count;
180 +       count2++;
181 +       ext4_lock_group(sb, entry->efd_group);
182 +       /* Take it out of per group rb tree */
183 +       rb_erase(&entry->efd_node, &(db->bb_free_root));
184 +       mb_free_blocks(NULL, &e4b, entry->efd_start_blk, entry->efd_count);
185 +
186 +       if (!db->bb_free_root.rb_node) {
187 +               /* No more items in the per group rb tree
188 +                * balance refcounts from ext4_mb_free_metadata()
189 +                */
190 +               page_cache_release(e4b.bd_buddy_page);
191 +               page_cache_release(e4b.bd_bitmap_page);
192         }
193 +       ext4_unlock_group(sb, entry->efd_group);
194 +
195 +       kmem_cache_free(ext4_free_data_cachep, entry);
196 +       ext4_mb_unload_buddy(&e4b);
197  
198         mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
199  }
200 @@ -3123,22 +3116,24 @@ int __init init_ext4_mballoc(void)
201                 kmem_cache_create("ext4_alloc_context",
202                                      sizeof(struct ext4_allocation_context),
203                                      0, SLAB_RECLAIM_ACCOUNT, NULL, NULL);
204 -       if (ext4_ac_cachep == NULL) {
205 -               kmem_cache_destroy(ext4_pspace_cachep);
206 -               return -ENOMEM;
207 -       }
208 +       if (ext4_ac_cachep == NULL)
209 +               goto out_err;
210  
211 -       ext4_free_ext_cachep =
212 -               kmem_cache_create("ext4_free_block_extents",
213 +       ext4_free_data_cachep =
214 +               kmem_cache_create("ext4_free_data",
215                                      sizeof(struct ext4_free_data),
216                                      0, SLAB_RECLAIM_ACCOUNT, NULL, NULL);
217 -       if (ext4_free_ext_cachep == NULL) {
218 -               kmem_cache_destroy(ext4_pspace_cachep);
219 -               kmem_cache_destroy(ext4_ac_cachep);
220 -               return -ENOMEM;
221 -       }
222 +       if (ext4_free_data_cachep == NULL)
223 +               goto out1_err;
224 +
225         ext4_create_debugfs_entry();
226         return 0;
227 +
228 +out1_err:
229 +       kmem_cache_destroy(ext4_ac_cachep);
230 +out_err:
231 +       kmem_cache_destroy(ext4_pspace_cachep);
232 +       return -ENOMEM;
233  }
234  
235  void exit_ext4_mballoc(void)
236 @@ -3150,7 +3145,7 @@ void exit_ext4_mballoc(void)
237         rcu_barrier();
238         kmem_cache_destroy(ext4_pspace_cachep);
239         kmem_cache_destroy(ext4_ac_cachep);
240 -       kmem_cache_destroy(ext4_free_ext_cachep);
241 +       kmem_cache_destroy(ext4_free_data_cachep);
242         ext4_remove_debugfs_entry();
243  }
244  
245 @@ -3688,8 +3683,8 @@ static void ext4_mb_generate_from_freeli
246         n = rb_first(&(grp->bb_free_root));
247  
248         while (n) {
249 -               entry = rb_entry(n, struct ext4_free_data, node);
250 -               mb_set_bits(bitmap, entry->start_blk, entry->count);
251 +               entry = rb_entry(n, struct ext4_free_data, efd_node);
252 +               mb_set_bits(bitmap, entry->efd_start_blk, entry->efd_count);
253                 n = rb_next(n);
254         }
255         return;
256 @@ -4974,11 +4969,11 @@ out3:
257   * AND the blocks are associated with the same group.
258   */
259  static int can_merge(struct ext4_free_data *entry1,
260 -                       struct ext4_free_data *entry2)
261 +                    struct ext4_free_data *entry2)
262  {
263 -       if ((entry1->t_tid == entry2->t_tid) &&
264 -           (entry1->group == entry2->group) &&
265 -           ((entry1->start_blk + entry1->count) == entry2->start_blk))
266 +       if ((entry1->efd_tid == entry2->efd_tid) &&
267 +           (entry1->efd_group == entry2->efd_group) &&
268 +           ((entry1->efd_start_blk + entry1->efd_count) == entry2->efd_start_blk))
269                 return 1;
270         return 0;
271  }
272 @@ -4991,7 +4986,6 @@ ext4_mb_free_metadata(handle_t *handle, 
273         struct ext4_free_data *entry;
274         struct ext4_group_info *db = e4b->bd_info;
275         struct super_block *sb = e4b->bd_sb;
276 -       struct ext4_sb_info *sbi = EXT4_SB(sb);
277         struct rb_node **n = &db->bb_free_root.rb_node, *node;
278         struct rb_node *parent = NULL, *new_node;
279  
280 @@ -4999,8 +4993,8 @@ ext4_mb_free_metadata(handle_t *handle, 
281         BUG_ON(e4b->bd_bitmap_page == NULL);
282         BUG_ON(e4b->bd_buddy_page == NULL);
283  
284 -       new_node = &new_entry->node;
285 -       block = new_entry->start_blk;
286 +       new_node = &new_entry->efd_node;
287 +       block = new_entry->efd_start_blk;
288  
289         if (!*n) {
290                 /* first free block exent. We need to
291 @@ -5013,15 +5007,15 @@ ext4_mb_free_metadata(handle_t *handle, 
292         }
293         while (*n) {
294                 parent = *n;
295 -               entry = rb_entry(parent, struct ext4_free_data, node);
296 -               if (block < entry->start_blk)
297 +               entry = rb_entry(parent, struct ext4_free_data, efd_node);
298 +               if (block < entry->efd_start_blk)
299                         n = &(*n)->rb_left;
300 -               else if (block >= (entry->start_blk + entry->count))
301 +               else if (block >= (entry->efd_start_blk + entry->efd_count))
302                         n = &(*n)->rb_right;
303                 else {
304                         ext4_grp_locked_error(sb, e4b->bd_group, __func__,
305                                         "Double free of blocks %d (%d %d)",
306 -                                       block, entry->start_blk, entry->count);
307 +                                       block, entry->efd_start_blk, entry->efd_count);
308                         return 0;
309                 }
310         }
311 @@ -5032,34 +5026,29 @@ ext4_mb_free_metadata(handle_t *handle, 
312         /* Now try to see the extent can be merged to left and right */
313         node = rb_prev(new_node);
314         if (node) {
315 -               entry = rb_entry(node, struct ext4_free_data, node);
316 +               entry = rb_entry(node, struct ext4_free_data, efd_node);
317                 if (can_merge(entry, new_entry)) {
318 -                       new_entry->start_blk = entry->start_blk;
319 -                       new_entry->count += entry->count;
320 +                       new_entry->efd_start_blk = entry->efd_start_blk;
321 +                       new_entry->efd_count += entry->efd_count;
322                         rb_erase(node, &(db->bb_free_root));
323 -                       spin_lock(&sbi->s_md_lock);
324 -                       list_del(&entry->list);
325 -                       spin_unlock(&sbi->s_md_lock);
326 -                       kmem_cache_free(ext4_free_ext_cachep, entry);
327 +                       ext4_journal_callback_del(handle, &entry->efd_jce);
328 +                       kmem_cache_free(ext4_free_data_cachep, entry);
329                 }
330         }
331  
332         node = rb_next(new_node);
333         if (node) {
334 -               entry = rb_entry(node, struct ext4_free_data, node);
335 +               entry = rb_entry(node, struct ext4_free_data, efd_node);
336                 if (can_merge(new_entry, entry)) {
337 -                       new_entry->count += entry->count;
338 +                       new_entry->efd_count += entry->efd_count;
339                         rb_erase(node, &(db->bb_free_root));
340 -                       spin_lock(&sbi->s_md_lock);
341 -                       list_del(&entry->list);
342 -                       spin_unlock(&sbi->s_md_lock);
343 -                       kmem_cache_free(ext4_free_ext_cachep, entry);
344 +                       ext4_journal_callback_del(handle, &entry->efd_jce);
345 +                       kmem_cache_free(ext4_free_data_cachep, entry);
346                 }
347         }
348         /* Add the extent to transaction's private list */
349 -       spin_lock(&sbi->s_md_lock);
350 -       list_add(&new_entry->list, &handle->h_transaction->t_private_list);
351 -       spin_unlock(&sbi->s_md_lock);
352 +       ext4_journal_callback_add(handle, ext4_free_data_callback,
353 +                                 &new_entry->efd_jce);
354         return 0;
355  }
356  
357 @@ -5191,11 +5180,11 @@ do_more:
358                  * blocks being freed are metadata. these blocks shouldn't
359                  * be used until this transaction is committed
360                  */
361 -               new_entry  = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
362 -               new_entry->start_blk = bit;
363 -               new_entry->group  = block_group;
364 -               new_entry->count = count;
365 -               new_entry->t_tid = handle->h_transaction->t_tid;
366 +               new_entry = kmem_cache_alloc(ext4_free_data_cachep, GFP_NOFS);
367 +               new_entry->efd_start_blk = bit;
368 +               new_entry->efd_group  = block_group;
369 +               new_entry->efd_count = count;
370 +               new_entry->efd_tid = handle->h_transaction->t_tid;
371  
372                 ext4_lock_group(sb, block_group);
373                 mb_clear_bits(bitmap_bh->b_data, bit, count);
374 Index: linux-stage/fs/ext4/mballoc.h
375 ===================================================================
376 --- linux-stage.orig/fs/ext4/mballoc.h
377 +++ linux-stage/fs/ext4/mballoc.h
378 @@ -107,23 +107,24 @@ extern u8 mb_enable_debug;
379   */
380  #define MB_DEFAULT_GROUP_PREALLOC      512
381  
382 -
383  struct ext4_free_data {
384 -       /* this links the free block information from group_info */
385 -       struct rb_node node;
386 +       /* MUST be the first member */
387 +       struct ext4_journal_cb_entry    efd_jce;
388  
389 -       /* this links the free block information from ext4_sb_info */
390 -       struct list_head list;
391 +       /* ext4_free_data private data starts from here */
392 +
393 +       /* this links the free block information from group_info */
394 +       struct rb_node          efd_node;
395  
396         /* group which free block extent belongs */
397 -       ext4_group_t group;
398 +       ext4_group_t            efd_group;
399  
400         /* free block extent */
401 -       ext4_grpblk_t start_blk;
402 -       ext4_grpblk_t count;
403 +       ext4_grpblk_t           efd_start_blk;
404 +       ext4_grpblk_t           efd_count;
405  
406         /* transaction which freed this extent */
407 -       tid_t   t_tid;
408 +       tid_t                   efd_tid;
409  };
410  
411  struct ext4_prealloc_space {
412 Index: linux-stage/fs/ext4/super.c
413 ===================================================================
414 --- linux-stage.orig/fs/ext4/super.c
415 +++ linux-stage/fs/ext4/super.c
416 @@ -304,6 +304,23 @@ void ext4_journal_abort_handle(const cha
417  
418  EXPORT_SYMBOL(ext4_journal_abort_handle);
419  
420 +static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
421 +{
422 +       struct super_block              *sb = journal->j_private;
423 +       struct ext4_sb_info             *sbi = EXT4_SB(sb);
424 +       int                             error = is_journal_aborted(journal);
425 +       struct ext4_journal_cb_entry    *jce, *tmp;
426 +
427 +       spin_lock(&sbi->s_md_lock);
428 +       list_for_each_entry_safe(jce, tmp, &txn->t_private_list, jce_list) {
429 +               list_del_init(&jce->jce_list);
430 +               spin_unlock(&sbi->s_md_lock);
431 +               jce->jce_func(sb, jce, error);
432 +               spin_lock(&sbi->s_md_lock);
433 +       }
434 +       spin_unlock(&sbi->s_md_lock);
435 +}
436 +
437  /* Deal with the reporting of failure conditions on a filesystem such as
438   * inconsistencies detected or read IO failures.
439   *
440 @@ -2997,6 +3014,8 @@ static int ext4_fill_super(struct super_
441         }
442         set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
443  
444 +       sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
445 +
446  no_journal:
447  
448         if (test_opt(sb, NOBH)) {