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
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))
10 + * struct ext4_journal_cb_entry - Base structure for callback information.
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().
17 +struct ext4_journal_cb_entry {
18 + /* list information for other callbacks attached to the same handle */
19 + struct list_head jce_list;
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);
25 + /* user data goes here */
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)
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.
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.
46 + * There is no guaranteed calling order of multiple registered callbacks on
47 + * the same transaction.
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,
53 + struct ext4_journal_cb_entry *jce)
55 + struct ext4_sb_info *sbi =
56 + EXT4_SB(handle->h_transaction->t_journal->j_private);
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);
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
70 +static inline void ext4_journal_callback_del(handle_t *handle,
71 + struct ext4_journal_cb_entry *jce)
73 + struct ext4_sb_info *sbi =
74 + EXT4_SB(handle->h_transaction->t_journal->j_private);
76 + spin_lock(&sbi->s_md_lock);
77 + list_del_init(&jce->jce_list);
78 + spin_unlock(&sbi->s_md_lock);
81 +#define HAVE_EXT4_JOURNAL_CALLBACK_ADD
84 ext4_mark_iloc_dirty(handle_t *handle,
86 Index: linux-stage/fs/ext4/mballoc.c
87 ===================================================================
88 --- linux-stage.orig/fs/ext4/mballoc.c
89 +++ linux-stage/fs/ext4/mballoc.c
91 * mballoc.c contains the multiblocks allocation routines
94 +#include "ext4_jbd2.h"
96 #include <linux/debugfs.h>
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,
106 static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
108 -static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
111 +static void ext4_free_data_callback(struct super_block *sb, struct ext4_journal_cb_entry *jce, int error);
113 static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
115 @@ -2942,8 +2941,6 @@ int ext4_mb_init(struct super_block *sb,
117 ext4_mb_history_init(sb);
119 - if (sbi->s_journal)
120 - sbi->s_journal->j_commit_callback = release_blocks_on_commit;
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.
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,
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;
141 - list_for_each_safe(l, ltmp, &txn->t_private_list) {
142 - entry = list_entry(l, struct ext4_free_data, list);
144 - mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
145 - entry->count, entry->group, entry);
147 - err = ext4_mb_load_buddy(sb, entry->group, &e4b);
148 - /* we expect to find existing buddy because it's pinned */
152 - /* there are blocks to put in buddy to make them really free */
153 - count += entry->count;
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);
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()
164 - page_cache_release(e4b.bd_buddy_page);
165 - page_cache_release(e4b.bd_bitmap_page);
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);
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 */
178 + /* there are blocks to put in buddy to make them really free */
179 + count += entry->efd_count;
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);
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()
190 + page_cache_release(e4b.bd_buddy_page);
191 + page_cache_release(e4b.bd_bitmap_page);
193 + ext4_unlock_group(sb, entry->efd_group);
195 + kmem_cache_free(ext4_free_data_cachep, entry);
196 + ext4_mb_unload_buddy(&e4b);
198 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
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);
208 + if (ext4_ac_cachep == NULL)
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);
222 + if (ext4_free_data_cachep == NULL)
225 ext4_create_debugfs_entry();
229 + kmem_cache_destroy(ext4_ac_cachep);
231 + kmem_cache_destroy(ext4_pspace_cachep);
235 void exit_ext4_mballoc(void)
236 @@ -3150,7 +3145,7 @@ void exit_ext4_mballoc(void)
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();
245 @@ -3688,8 +3683,8 @@ static void ext4_mb_generate_from_freeli
246 n = rb_first(&(grp->bb_free_root));
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);
256 @@ -4974,11 +4969,11 @@ out3:
257 * AND the blocks are associated with the same group.
259 static int can_merge(struct ext4_free_data *entry1,
260 - struct ext4_free_data *entry2)
261 + struct ext4_free_data *entry2)
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))
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;
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);
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;
290 /* first free block exent. We need to
291 @@ -5013,15 +5007,15 @@ ext4_mb_free_metadata(handle_t *handle,
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)
300 - else if (block >= (entry->start_blk + entry->count))
301 + else if (block >= (entry->efd_start_blk + entry->efd_count))
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);
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);
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);
332 node = rb_next(new_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);
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);
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
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;
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;
380 #define MB_DEFAULT_GROUP_PREALLOC 512
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;
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 */
393 + /* this links the free block information from group_info */
394 + struct rb_node efd_node;
396 /* group which free block extent belongs */
397 - ext4_group_t group;
398 + ext4_group_t efd_group;
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;
406 /* transaction which freed this extent */
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
418 EXPORT_SYMBOL(ext4_journal_abort_handle);
420 +static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
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;
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);
434 + spin_unlock(&sbi->s_md_lock);
437 /* Deal with the reporting of failure conditions on a filesystem such as
438 * inconsistencies detected or read IO failures.
440 @@ -2997,6 +3014,8 @@ static int ext4_fill_super(struct super_
442 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
444 + sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
448 if (test_opt(sb, NOBH)) {