Sync up latest jmap patches
[ext4-patch-queue.git] / add-support-for-log-metadata-block-tracking-in-log
blobaa8fb559c4be943d2e1964e74408edbb06bea8fd
1 Add support for tracking metadata blocks in the log.
3 From: Abutalib Aghayev <agayev@cs.cmu.edu>
5 This patch adds two important data structures, jmap and transaction_infos,
6 and supporting functions.  Jmap is a map from a metadata block number to
7 the log block number.  When a transaction commits, jmap is updated with new
8 mappings; when a block is revoked, the mapping for the block is removed
9 from the jmap.  Transaction_infos is an array of transaction_info
10 structures that contain information about transactions currently present in
11 the log.  It contains a linked list of live blocks in a transaction, and it
12 is updated after every commit to keep the list up-to-date.
13 Transaction_infos array will be used by the cleaner for identifying live
14 blocks and migrating them to appropriate location.
16 [ Modified by tytso to conditionalize changes on the JBD2_LAZY journal flag ]
18 Signed-off-by: Abutalib Aghayev <agayev@cs.cmu.edu>
19 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
20 ---
21  fs/jbd2/Makefile            |   3 +-
22  fs/jbd2/commit.c            |  23 +++++
23  fs/jbd2/jmap.c              | 455 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24  fs/jbd2/journal.c           |  17 +++-
25  include/linux/jbd2.h        |  14 +++
26  include/linux/jmap.h        | 131 +++++++++++++++++++++++++
27  include/trace/events/jbd2.h | 169 ++++++++++++++++++++++++++++++++
28  7 files changed, 807 insertions(+), 5 deletions(-)
30 diff --git a/fs/jbd2/Makefile b/fs/jbd2/Makefile
31 index 802a3413872a..a54f50b3a06e 100644
32 --- a/fs/jbd2/Makefile
33 +++ b/fs/jbd2/Makefile
34 @@ -4,4 +4,5 @@
36  obj-$(CONFIG_JBD2) += jbd2.o
38 -jbd2-objs := transaction.o commit.o recovery.o checkpoint.o revoke.o journal.o
39 +jbd2-objs := transaction.o commit.o recovery.o checkpoint.o revoke.o journal.o \
40 +               jmap.o
41 diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c
42 index 8c514367ba5a..50e1a0b375c5 100644
43 --- a/fs/jbd2/commit.c
44 +++ b/fs/jbd2/commit.c
45 @@ -362,6 +362,8 @@ void jbd2_journal_commit_transaction(journal_t *journal)
46         int flags;
47         int err;
48         unsigned long long blocknr;
49 +       struct blk_mapping *mappings = NULL;
50 +       struct blk_mapping *map_ptr = NULL;
51         ktime_t start_time;
52         u64 commit_time;
53         char *tagp = NULL;
54 @@ -563,6 +565,14 @@ void jbd2_journal_commit_transaction(journal_t *journal)
55         J_ASSERT(commit_transaction->t_nr_buffers <=
56                  atomic_read(&commit_transaction->t_outstanding_credits));
58 +       if (journal->j_flags & JBD2_LAZY) {
59 +               int nr_mappings = commit_transaction->t_nr_buffers;
61 +               map_ptr = mappings = kmalloc(sizeof(*mappings) * nr_mappings, GFP_NOFS);
62 +               if (!mappings)
63 +                       jbd2_journal_abort(journal, -ENOMEM);
64 +       }
66         err = 0;
67         bufs = 0;
68         descriptor = NULL;
69 @@ -661,6 +671,11 @@ void jbd2_journal_commit_transaction(journal_t *journal)
70                         continue;
71                 }
72                 jbd2_file_log_bh(&io_bufs, wbuf[bufs]);
73 +               if (map_ptr) {
74 +                       map_ptr->fsblk = jh2bh(jh)->b_blocknr;
75 +                       map_ptr->logblk = blocknr;
76 +                       map_ptr++;
77 +               }
79                 /* Record the new block's tag in the current descriptor
80                     buffer */
81 @@ -895,6 +910,14 @@ void jbd2_journal_commit_transaction(journal_t *journal)
82             transaction can be removed from any checkpoint list it was on
83             before. */
85 +       if (mappings) {
86 +               err = jbd2_transaction_infos_add(journal, commit_transaction,
87 +                                                mappings, map_ptr - mappings);
88 +               if (err)
89 +                       jbd2_journal_abort(journal, -ENOMEM);
90 +               kfree(mappings);
91 +       }
93         jbd_debug(3, "JBD2: commit phase 6\n");
95         J_ASSERT(list_empty(&commit_transaction->t_inode_list));
96 diff --git a/fs/jbd2/jmap.c b/fs/jbd2/jmap.c
97 new file mode 100644
98 index 000000000000..ddaa7d5f5634
99 --- /dev/null
100 +++ b/fs/jbd2/jmap.c
101 @@ -0,0 +1,455 @@
102 +#include <linux/blk_types.h>
103 +#include <linux/jbd2.h>
104 +#include <linux/jmap.h>
105 +#include <trace/events/jbd2.h>
107 +static struct kmem_cache *jbd2_jmap_cache;
109 +int jbd2_journal_init_jmap_cache(void)
111 +       jbd2_jmap_cache = KMEM_CACHE(jmap_entry, SLAB_RECLAIM_ACCOUNT);
112 +       if (!jbd2_jmap_cache)
113 +               return -ENOMEM;
114 +       return 0;
117 +void jbd2_journal_destroy_jmap_cache(void)
119 +       if (jbd2_jmap_cache)
120 +               kmem_cache_destroy(jbd2_jmap_cache);
121 +       jbd2_jmap_cache = NULL;
125 + * Allocate an array of transaction_info structures and initialize the list
126 + * heads inside them.
127 + */
128 +int jbd2_init_transaction_infos(journal_t *journal)
130 +       int i;
131 +       struct transaction_infos *tis = kzalloc(sizeof(*tis), GFP_KERNEL);
132 +       if (!tis)
133 +               return -ENOMEM;
135 +       tis->buf = kzalloc(sizeof(*tis->buf) * MAX_LIVE_TRANSACTIONS,
136 +                       GFP_KERNEL);
137 +       if (!tis->buf) {
138 +               kfree(tis);
139 +               return -ENOMEM;
140 +       }
142 +       for (i = 0; i < MAX_LIVE_TRANSACTIONS; ++i)
143 +               INIT_LIST_HEAD(&tis->buf[i].live_logblks);
145 +       journal->j_transaction_infos = tis;
146 +       return 0;
150 + * Free the array of transaction_info structures.
151 + */
152 +void jbd2_free_transaction_infos(journal_t *journal)
154 +       struct transaction_infos *tis = journal->j_transaction_infos;
155 +       if (!tis)
156 +               return;
157 +       kfree(tis->buf);
158 +       kfree(tis);
162 + * Fill an entry to be stored in jmap.
163 + */
164 +static void fill_entry(struct jmap_entry *entry, struct blk_mapping *mapping,
165 +                       int t_idx, struct list_head *list)
167 +       entry->mapping = *mapping;
168 +       entry->fsblk_last_modified = jiffies;
169 +       entry->t_idx = t_idx;
170 +       list_add(&entry->list, list);
174 + * A helper function for jbd2_transaction_infos_add.  Scans through the mappings
175 + * array, dropping revoked entries from jmap and updating existing entries.
176 + * Moves the new mappings to the beginning of the mappings array and returns the
177 + * number of new mappings.  Should be called with a write lock on j_jmap_lock.
178 + */
179 +static int process_existing_mappings(journal_t *journal,
180 +                               struct transaction_info *ti, int t_idx,
181 +                               struct blk_mapping *mappings, int nr_mappings)
183 +       struct jmap_entry *je;
184 +       int i, nr_new = 0;
186 +       for (i = 0; i < nr_mappings; ++i) {
187 +               je = jbd2_jmap_lookup(journal, mappings[i].fsblk, __func__);
188 +               if (!je) {
189 +                       mappings[nr_new++] = mappings[i];
190 +                       continue;
191 +               }
192 +               /*
193 +                * We are either deleting the entry because it was revoked, or
194 +                * we are moving it to the live blocks list of this transaction.
195 +                * In either case, we remove it from its existing list.
196 +                */
197 +               list_del(&je->list);
199 +               if (je->revoked) {
200 +                       rb_erase(&je->rb_node, &journal->j_jmap);
201 +                       kmem_cache_free(jbd2_jmap_cache, je);
202 +               } else {
203 +                       trace_jbd2_jmap_replace(je, &mappings[i], t_idx);
204 +                       fill_entry(je, &mappings[i], t_idx, &ti->live_logblks);
205 +               }
206 +       }
207 +       return nr_new;
211 + * A helper function for jbd2_transaction_infos_add.  Allocates an array of
212 + * jmap_entry structures and returns the pointer to array if successful.
213 + * Otherwise, returns NULL.
214 + */
215 +static struct jmap_entry **alloc_jmap_entries(int nr_entries)
217 +       struct jmap_entry **jmap_entries;
218 +       int i;
220 +       jmap_entries = kmalloc(sizeof(struct jmap_entry *) * nr_entries,
221 +                       GFP_NOFS);
222 +       if (!jmap_entries)
223 +               return NULL;
225 +       for (i = 0; i < nr_entries; i++) {
226 +               jmap_entries[i] = kmem_cache_zalloc(jbd2_jmap_cache, GFP_NOFS);
227 +               if (!jmap_entries[i])
228 +                       goto out_err;
229 +       }
230 +       return jmap_entries;
232 +out_err:
233 +       for (i = 0; i < nr_entries && jmap_entries[i]; ++i)
234 +               kmem_cache_free(jbd2_jmap_cache, jmap_entries[i]);
235 +       kfree(jmap_entries);
236 +       return NULL;
240 + * A helper function for jbd2_transaction_infos_add.  Adds new mappings to jmap
241 + * and updates the linked list of live logblks of the new transaction.  Should
242 + * be called with write lock on j_jmap_lock.
243 + */
244 +static void add_new_mappings(journal_t *journal, struct transaction_info *ti,
245 +                       int t_idx, struct blk_mapping *mappings,
246 +                       struct jmap_entry **new_entries, int nr_new)
248 +       struct rb_node **p;
249 +       struct rb_node *parent = NULL;
250 +       struct jmap_entry *je;
251 +       int i;
253 +       for (i = 0; i < nr_new; ++i) {
254 +               p = &journal->j_jmap.rb_node;
255 +               while (*p) {
256 +                       parent = *p;
257 +                       je = rb_entry(parent, struct jmap_entry, rb_node);
259 +                       if (mappings[i].fsblk < je->mapping.fsblk)
260 +                               p = &(*p)->rb_left;
261 +                       else if (mappings[i].fsblk > je->mapping.fsblk)
262 +                               p = &(*p)->rb_right;
263 +                       else
264 +                               BUG_ON(1);
265 +               }
266 +               fill_entry(new_entries[i], &mappings[i], t_idx,
267 +                       &ti->live_logblks);
268 +               rb_link_node(&new_entries[i]->rb_node, parent, p);
269 +               rb_insert_color(&new_entries[i]->rb_node, &journal->j_jmap);
270 +               trace_jbd2_jmap_insert(&mappings[i], t_idx);
271 +       }
275 + * This function is called after a transaction commits.  It adds new
276 + * transaction_info structure to transaction_infos and populates jmap map with
277 + * the new mappings that are part of the committed transaction.  It also adds
278 + * all the mappings to the linked list that is part of the transaction_info
279 + * structure.
280 + */
281 +int jbd2_transaction_infos_add(journal_t *journal, transaction_t *transaction,
282 +                       struct blk_mapping *mappings, int nr_mappings)
284 +       struct transaction_infos *tis = journal->j_transaction_infos;
285 +       int t_idx = tis->head;
286 +       struct transaction_info *ti = &tis->buf[t_idx];
287 +       struct jmap_entry **new_entries = NULL;
288 +       int nr_new = 0;
290 +       /*
291 +        * We are possibly reusing space of an old transaction_info.  The old
292 +        * transaction should not have any live blocks in it.
293 +        */
294 +       BUG_ON(!list_empty(&ti->live_logblks));
296 +       write_lock(&journal->j_jmap_lock);
297 +       nr_new = process_existing_mappings(journal, ti, t_idx, mappings,
298 +                                       nr_mappings);
299 +       write_unlock(&journal->j_jmap_lock);
301 +       if (nr_new == 0)
302 +               goto move_head;
304 +       new_entries = alloc_jmap_entries(nr_new);
305 +       if (!new_entries)
306 +               return -ENOMEM;
308 +       write_lock(&journal->j_jmap_lock);
309 +       add_new_mappings(journal, ti, t_idx, mappings, new_entries, nr_new);
310 +       write_unlock(&journal->j_jmap_lock);
312 +       kfree(new_entries);
314 +move_head:
315 +       write_lock(&journal->j_jmap_lock);
316 +       ti->tid = transaction->t_tid;
317 +       ti->offset = transaction->t_log_start;
318 +       tis->head = (tis->head + 1) & (MAX_LIVE_TRANSACTIONS - 1);
319 +       write_unlock(&journal->j_jmap_lock);
321 +       trace_jbd2_transaction_infos_add(t_idx, ti, nr_mappings);
322 +       return 0;
326 + * Look up fsblk in the jmap and return the corresponding jmap entry if found.
327 + * Should be called with a read lock on j_jmap_lock.
328 + */
329 +struct jmap_entry *jbd2_jmap_lookup(journal_t *journal, sector_t fsblk,
330 +                               const char *func)
332 +       struct rb_node *p;
334 +       BUG_ON(!journal);
336 +       for (p = journal->j_jmap.rb_node; p; ) {
337 +               struct jmap_entry *je = rb_entry(p, struct jmap_entry, rb_node);
338 +               if (je->mapping.fsblk > fsblk)
339 +                       p = p->rb_left;
340 +               else if (je->mapping.fsblk < fsblk)
341 +                       p = p->rb_right;
342 +               else {
343 +                       trace_jbd2_jmap_lookup(fsblk, je->mapping.logblk, func);
344 +                       return je;
345 +               }
346 +       }
347 +       trace_jbd2_jmap_lookup(fsblk, 0, func);
348 +       return NULL;
352 + * Revoke a mapping for the fsblk in the jmap.  A lookup for fsblk will return
353 + * NULL and the mapping will be removed from the jmap during commit, unless
354 + * fsblk is reallocated as a metadata block.
355 + */
356 +void jbd2_jmap_revoke(journal_t *journal, sector_t fsblk)
358 +       struct jmap_entry *je;
360 +       write_lock(&journal->j_jmap_lock);
361 +       je = jbd2_jmap_lookup(journal, fsblk, __func__);
362 +       /*
363 +        * For now, since we do not construct jmap from the journal, it is
364 +        * possible that a metadata block that was revoked is not in the jmap.
365 +        * Eventually, this should not be the case and we should have a
366 +        * BUG_ON(!je) here.
367 +        */
368 +       if (je) {
369 +               BUG_ON(je->revoked);
370 +               je->revoked = true;
371 +       }
372 +       write_unlock(&journal->j_jmap_lock);
376 + * Cancel a revoke for the fsblk in the jmap.
377 + */
378 +void jbd2_jmap_cancel_revoke(journal_t *journal, sector_t fsblk)
380 +       struct jmap_entry *je;
382 +       write_lock(&journal->j_jmap_lock);
383 +       je = jbd2_jmap_lookup(journal, fsblk, __func__);
384 +       BUG_ON(!je);
385 +       BUG_ON(!je->revoked);
386 +       je->revoked = false;
387 +       write_unlock(&journal->j_jmap_lock);
391 + * Read bh from its most up-to-date location, either from the file system or
392 + * from the log.
393 + *
394 + * If there is no mapping for the bh in jmap, this function acts like submit_bh.
395 + * Otherwise, it submits a read for the block pointed by the mapping located in
396 + * the log.  Upon completion, bh will be filled with the contents of the block
397 + * read from the log.
398 + */
399 +void jbd2_submit_bh(journal_t *journal, int rw, int op_flags,
400 +                   struct buffer_head *bh, const char *func)
402 +       sector_t fsblk = bh->b_blocknr;
403 +       sector_t logblk;
404 +       struct jmap_entry *je;
406 +       BUG_ON(!buffer_locked(bh));
408 +       if (!journal || !(journal->j_flags & JBD2_LAZY)) {
409 +               submit_bh(rw, op_flags, bh);
410 +               return;
411 +       }
413 +       read_lock(&journal->j_jmap_lock);
414 +       je = jbd2_jmap_lookup(journal, fsblk, func);
415 +       if (!je) {
416 +               read_unlock(&journal->j_jmap_lock);
417 +               submit_bh(rw, op_flags, bh);
418 +               return;
419 +       }
420 +       logblk = je->mapping.logblk;
421 +       read_unlock(&journal->j_jmap_lock);
423 +       BUG_ON(rw == WRITE);
424 +       read_block_from_log(journal, bh, op_flags, logblk);
428 + * End_io handler for read_block_from_log that copies the contents of
429 + * log_bh read from log to the embedded bh.
430 + */
431 +static void jbd2_end_log_read(struct buffer_head *log_bh, int uptodate)
433 +       struct buffer_head *bh = log_bh->b_private;
435 +       if (uptodate) {
436 +               trace_jbd2_jmap_printf1("read from log", bh->b_blocknr);
437 +               memcpy(bh->b_data, log_bh->b_data, log_bh->b_size);
438 +       } else {
439 +               trace_jbd2_jmap_printf1("failed to read from log", bh->b_blocknr);
440 +       }
442 +       unlock_buffer(log_bh);
443 +       put_bh(log_bh);
444 +       brelse(log_bh);
446 +       bh->b_end_io(bh, uptodate);
450 + * This function fills |bh| with the contents of the |blk|.  Assume
451 + * jmap maps metadata block 123 to log block 100123.  To read the
452 + * metadata block 123, we obtain a buffer head for it and call
453 + * read_block_from_log passing the obtained buffer head as |bh| and
454 + * 100123 as |blk|.  If block 100123 is cached, then we copy the
455 + * contents to |bh| and return.  Otherwise, we submit a request and
456 + * end_io handler copies the contents of block 100123 to |bh|.
457 + * Returns -ENOMEM if getblk fails, 1 if block is not cached, 0 if
458 + * block is cached.
459 + */
460 +int read_block_from_log(journal_t *journal, struct buffer_head *bh,
461 +                       int op_flags, sector_t blk)
463 +       struct buffer_head *log_bh;
465 +       BUG_ON(!buffer_locked(bh));
467 +       log_bh = __getblk(journal->j_fs_dev, blk, bh->b_size);
468 +       if (unlikely(!log_bh)) {
469 +               bh->b_end_io(bh, 0);
470 +               return -ENOMEM;
471 +       }
473 +       lock_buffer(log_bh);
474 +       if (buffer_uptodate(log_bh)) {
475 +               memcpy(bh->b_data, log_bh->b_data, bh->b_size);
476 +               unlock_buffer(log_bh);
477 +               brelse(log_bh);
478 +               bh->b_end_io(bh, 1);
479 +               return 0;
480 +       }
482 +       log_bh->b_end_io = jbd2_end_log_read;
483 +       log_bh->b_private = bh;
484 +       get_bh(log_bh);
485 +       submit_bh(READ, op_flags, log_bh);
486 +       return 1;
490 + * Copy of ll_rw_block that uses jbd2_submit_bh instead of submit_bh.
491 + */
492 +void jbd2_ll_rw_block(journal_t *journal, int rw, int op_flags,
493 +                     int nr, struct buffer_head *bhs[], const char *func)
495 +       int i;
497 +       for (i = 0; i < nr; i++) {
498 +               struct buffer_head *bh = bhs[i];
500 +               if (!trylock_buffer(bh))
501 +                       continue;
502 +               BUG_ON(rw == WRITE);
503 +               if (!buffer_uptodate(bh)) {
504 +                       bh->b_end_io = end_buffer_read_sync;
505 +                       get_bh(bh);
506 +                       jbd2_submit_bh(journal, rw, op_flags, bh, func);
507 +                       continue;
508 +               }
509 +               unlock_buffer(bh);
510 +       }
514 + * Copy of bh_submit_read that uses jbd2_submit_bh instead of submit_bh.
515 + */
516 +int jbd2_bh_submit_read(journal_t *journal, struct buffer_head *bh,
517 +                       const char *func)
519 +       BUG_ON(!buffer_locked(bh));
521 +       if (buffer_uptodate(bh)) {
522 +               unlock_buffer(bh);
523 +               return 0;
524 +       }
526 +       get_bh(bh);
527 +       bh->b_end_io = end_buffer_read_sync;
528 +       jbd2_submit_bh(journal, READ, 0, bh, func);
529 +       wait_on_buffer(bh);
530 +       if (buffer_uptodate(bh))
531 +               return 0;
532 +       return -EIO;
535 +int jbd2_smr_journal_init(journal_t *journal)
537 +       journal->j_jmap = RB_ROOT;
538 +       rwlock_init(&journal->j_jmap_lock);
539 +       return jbd2_init_transaction_infos(journal);
542 +void jbd2_smr_journal_exit(journal_t *journal)
544 +       jbd2_free_transaction_infos(journal);
547 +void jbd2_sb_breadahead(journal_t *journal, struct super_block *sb,
548 +                       sector_t block)
550 +       struct buffer_head *bh = __getblk(sb->s_bdev, block, sb->s_blocksize);
551 +       if (likely(bh)) {
552 +               jbd2_ll_rw_block(journal, REQ_OP_READ, REQ_RAHEAD, 1,
553 +                                &bh, __func__);
554 +               brelse(bh);
555 +       }
557 diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
558 index f7fd801be997..0fb2cd419ee3 100644
559 --- a/fs/jbd2/journal.c
560 +++ b/fs/jbd2/journal.c
561 @@ -1120,15 +1120,17 @@ static journal_t *journal_init_common(struct block_device *bdev,
562         journal->j_max_batch_time = 15000; /* 15ms */
563         atomic_set(&journal->j_reserved_credits, 0);
565 +       err = jbd2_smr_journal_init(journal);
566 +       if (err)
567 +               goto out_err;
569         /* The journal is marked for error until we succeed with recovery! */
570         journal->j_flags = JBD2_ABORT;
572         /* Set up a default-sized revoke table for the new mount. */
573         err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
574 -       if (err) {
575 -               kfree(journal);
576 -               return NULL;
577 -       }
578 +       if (err)
579 +               goto out_err;
581         spin_lock_init(&journal->j_history_lock);
583 @@ -1162,6 +1164,9 @@ static journal_t *journal_init_common(struct block_device *bdev,
584         journal->j_superblock = (journal_superblock_t *)bh->b_data;
586         return journal;
587 +out_err:
588 +       kfree(journal);
589 +       return NULL;
592  /* jbd2_journal_init_dev and jbd2_journal_init_inode:
593 @@ -1741,6 +1746,7 @@ int jbd2_journal_destroy(journal_t *journal)
594                 jbd2_journal_destroy_revoke(journal);
595         if (journal->j_chksum_driver)
596                 crypto_free_shash(journal->j_chksum_driver);
597 +       jbd2_smr_journal_exit(journal);
598         kfree(journal->j_wbuf);
599         kfree(journal);
601 @@ -2641,6 +2647,8 @@ static int __init journal_init_caches(void)
602                 ret = jbd2_journal_init_handle_cache();
603         if (ret == 0)
604                 ret = jbd2_journal_init_transaction_cache();
605 +       if (ret == 0)
606 +               ret = jbd2_journal_init_jmap_cache();
607         return ret;
610 @@ -2650,6 +2658,7 @@ static void jbd2_journal_destroy_caches(void)
611         jbd2_journal_destroy_journal_head_cache();
612         jbd2_journal_destroy_handle_cache();
613         jbd2_journal_destroy_transaction_cache();
614 +       jbd2_journal_destroy_jmap_cache();
615         jbd2_journal_destroy_slabs();
618 diff --git a/include/linux/jbd2.h b/include/linux/jbd2.h
619 index 9a07b0485784..771588026353 100644
620 --- a/include/linux/jbd2.h
621 +++ b/include/linux/jbd2.h
622 @@ -25,6 +25,7 @@
623  #include <linux/types.h>
624  #include <linux/buffer_head.h>
625  #include <linux/journal-head.h>
626 +#include <linux/jmap.h>
627  #include <linux/stddef.h>
628  #include <linux/mutex.h>
629  #include <linux/timer.h>
630 @@ -732,6 +733,9 @@ jbd2_time_diff(unsigned long start, unsigned long end)
631   *     prior abort)?
632   * @j_sb_buffer: First part of superblock buffer
633   * @j_superblock: Second part of superblock buffer
634 + * @j_map: A map from file system blocks to log blocks
635 + * @j_transaction_infos: An array of information structures per live transaction
636 + * @j_map_lock: Protect j_jmap and j_transaction_infos
637   * @j_format_version: Version of the superblock format
638   * @j_state_lock: Protect the various scalars in the journal
639   * @j_barrier_count:  Number of processes waiting to create a barrier lock
640 @@ -807,6 +811,15 @@ struct journal_s
641         struct buffer_head      *j_sb_buffer;
642         journal_superblock_t    *j_superblock;
644 +       /* A map from file system blocks to journal blocks */
645 +       struct rb_root          j_jmap;
647 +       /* An array of housekeeping information about live transactions */
648 +       struct transaction_infos *j_transaction_infos;
650 +       /* Protect j_jmap and j_transaction_infos */
651 +       rwlock_t                j_jmap_lock;
653         /* Version of the superblock format */
654         int                     j_format_version;
656 @@ -1129,6 +1142,7 @@ JBD2_FEATURE_INCOMPAT_FUNCS(csum3,                CSUM_V3)
657                                                  * mode */
658  #define JBD2_REC_ERR   0x080   /* The errno in the sb has been recorded */
659  #define JBD2_NO_CLEANUP        0x100   /* Don't flush empty the journal on shutdown  */
660 +#define JBD2_LAZY      0x200   /* Do lazy journalling  */
662  /*
663   * Function declarations for the journaling transaction and buffer
664 diff --git a/include/linux/jmap.h b/include/linux/jmap.h
665 new file mode 100644
666 index 000000000000..a602ece7cc89
667 --- /dev/null
668 +++ b/include/linux/jmap.h
669 @@ -0,0 +1,131 @@
670 +#ifndef _LINUX_JMAP_H
671 +#define _LINUX_JMAP_H
673 +#include <linux/buffer_head.h>
674 +#include <linux/journal-head.h>
675 +#include <linux/list.h>
676 +#include <linux/circ_buf.h>
679 + * Maximum number of transactions.  This guides the size of the circular buffer
680 + * in which we store housekeeping information per transaction.  We start
681 + * cleaning either when the circular buffer is full or when we hit the free
682 + * space threshold, whichever happens first.  For starters, we make this
683 + * constant large to make sure that we start cleaning only when we hit the free
684 + * space threshold.  Later we can empirically determine a sensible value.
685 + */
686 +#define MAX_LIVE_TRANSACTIONS 65536
689 + * Forward declaration for journal_t so that we don't get circular dependency
690 + * between jbd2.h and jmap.h
691 + */
692 +struct journal_s;
693 +typedef struct journal_s journal_t;
696 + * A mapping from file system block to log block.
697 + */
698 +struct blk_mapping {
699 +       sector_t fsblk;
700 +       sector_t logblk;
704 + * An RB-tree entry wrapper for blk_mapping with extra housekeeping information.
705 + */
706 +struct jmap_entry {
707 +       struct rb_node rb_node;
709 +       /* The actual mapping information. */
710 +       struct blk_mapping mapping;
712 +       /*
713 +        * If a block that is mapped gets deleted, the revoked bit is set.  A
714 +        * lookup for a deleted block fails.  If a deleted block gets
715 +        * re-allocated as a metadata block, the mapping is updated and revoked
716 +        * bit is cleared.
717 +        */
718 +       bool revoked;
720 +       /*
721 +        * All log blocks that are part of the same transaction in the log are
722 +        * chained with a linked list.  The root of the list is stored in the
723 +        * transaction_info structure described below.
724 +        */
725 +       struct list_head list;
727 +       /*
728 +        * The last time when fsblk was written again to the journal and
729 +        * therefore was remapped to a different log block.
730 +        */
731 +       unsigned long fsblk_last_modified;
733 +       /*
734 +        * Index of the transaction in the transaction_info_buffer (described
735 +        * below) of which the log block is part of.
736 +        */
737 +       int t_idx;
741 + * Housekeeping information about committed transaction.
742 + */
743 +struct transaction_info {
744 +       /* Id of the transaction */
745 +       tid_t tid;
747 +       /* Offset where the transaction starts in the log */
748 +       sector_t offset;
750 +       /*
751 +        * A list of live log blocks referenced in the RB-tree that belong to
752 +        * this transaction.  It is used during cleaning to locate live blocks
753 +        * and migrate them to appropriate location.  If this list is empty,
754 +        * then the transaction does not contain any live blocks and we can
755 +        * reuse its space.  If this list is not empty, then we can quickly
756 +        * locate all the live blocks in this transaction.
757 +        */
758 +       struct list_head live_logblks;
762 + * An array of transaction_info structures about all the transactions in the
763 + * log.  Since there can only be a limited number of transactions in the log, we
764 + * use a circular buffer to store housekeeping information about transactions.
765 + */
766 +struct transaction_infos {
767 +       struct transaction_info *buf;
768 +       int head;
769 +       int tail;
772 +extern int jbd2_smr_journal_init(journal_t *journal);
773 +extern void jbd2_smr_journal_exit(journal_t *journal);
775 +extern int jbd2_journal_init_jmap_cache(void);
776 +extern void jbd2_journal_destroy_jmap_cache(void);
778 +extern int jbd2_init_transaction_infos(journal_t *journal);
779 +extern void jbd2_free_transaction_infos(journal_t *journal);
780 +extern int jbd2_transaction_infos_add(journal_t *journal,
781 +                               transaction_t *transaction,
782 +                               struct blk_mapping *mappings,
783 +                               int nr_mappings);
785 +extern struct jmap_entry *jbd2_jmap_lookup(journal_t *journal, sector_t fsblk,
786 +                                       const char *func);
787 +extern void jbd2_jmap_revoke(journal_t *journal, sector_t fsblk);
788 +extern void jbd2_jmap_cancel_revoke(journal_t *journal, sector_t fsblk);
789 +extern void jbd2_submit_bh(journal_t *journal, int rw, int op_flags,
790 +                          struct buffer_head *bh, const char *func);
791 +extern int read_block_from_log(journal_t *journal, struct buffer_head *bh,
792 +                              int op_flags, sector_t blk);
793 +extern void jbd2_ll_rw_block(journal_t *journal, int rw, int op_flags, int nr,
794 +                            struct buffer_head *bhs[], const char *func);
795 +extern int jbd2_bh_submit_read(journal_t *journal, struct buffer_head *bh,
796 +                              const char *func);
797 +extern void jbd2_sb_breadahead(journal_t *journal, struct super_block *sb,
798 +                              sector_t block);
800 +#endif
801 diff --git a/include/trace/events/jbd2.h b/include/trace/events/jbd2.h
802 index c1d1f3eb242d..bc1511a425ec 100644
803 --- a/include/trace/events/jbd2.h
804 +++ b/include/trace/events/jbd2.h
805 @@ -379,6 +379,175 @@ TRACE_EVENT(jbd2_lock_buffer_stall,
806                 __entry->stall_ms)
807  );
809 +TRACE_EVENT(jbd2_jmap_replace,
811 +       TP_PROTO(struct jmap_entry *jentry, struct blk_mapping *mapping, \
812 +               int t_idx),
814 +       TP_ARGS(jentry, mapping, t_idx),
816 +       TP_STRUCT__entry(
817 +               __field(sector_t, fsblk         )
818 +               __field(sector_t, old_logblk    )
819 +               __field(sector_t, new_logblk    )
820 +               __field(int, old_t_idx          )
821 +               __field(int, new_t_idx          )
822 +       ),
824 +       TP_fast_assign(
825 +               __entry->fsblk          = mapping->fsblk;
826 +               __entry->old_logblk     = jentry->mapping.logblk;
827 +               __entry->new_logblk     = mapping->logblk;
828 +               __entry->old_t_idx       = jentry->t_idx;
829 +               __entry->new_t_idx       = t_idx;
830 +       ),
832 +       TP_printk("remap %llu from %llu to %llu, move from transaction at index %d to transaction at index %d",
833 +                 (unsigned long long) __entry->fsblk,
834 +                 (unsigned long long) __entry->old_logblk,
835 +                 (unsigned long long) __entry->new_logblk,
836 +                 __entry->old_t_idx,
837 +                 __entry->new_t_idx)
840 +TRACE_EVENT(jbd2_jmap_insert,
842 +       TP_PROTO(struct blk_mapping *mapping, int t_idx),
844 +       TP_ARGS(mapping, t_idx),
846 +       TP_STRUCT__entry(
847 +               __field(sector_t, fsblk )
848 +               __field(sector_t, logblk)
849 +               __field(int, t_idx)
850 +       ),
852 +       TP_fast_assign(
853 +               __entry->fsblk  = mapping->fsblk;
854 +               __entry->logblk = mapping->logblk;
855 +               __entry->t_idx = t_idx;
856 +       ),
858 +       TP_printk("map %llu to %llu, insert to transaction %d",
859 +                 (unsigned long long) __entry->fsblk,
860 +                 (unsigned long long) __entry->logblk,
861 +                 __entry->t_idx)
864 +TRACE_EVENT(jbd2_jmap_lookup,
866 +       TP_PROTO(sector_t fsblk, sector_t logblk, const char *func),
868 +       TP_ARGS(fsblk, logblk, func),
870 +       TP_STRUCT__entry(
871 +               __field(sector_t, fsblk )
872 +               __field(sector_t, logblk)
873 +               __string(func, func)
874 +       ),
876 +       TP_fast_assign(
877 +               __entry->fsblk  = fsblk;
878 +               __entry->logblk = logblk;
879 +               __assign_str(func, func);
880 +       ),
882 +       TP_printk("%s: lookup %llu -> %llu",
883 +                 __get_str(func),
884 +                 (unsigned long long) __entry->fsblk,
885 +                 (unsigned long long) __entry->logblk)
888 +TRACE_EVENT(jbd2_jmap_printf,
890 +       TP_PROTO(const char *s),
892 +       TP_ARGS(s),
894 +       TP_STRUCT__entry(
895 +               __string(s, s)
896 +       ),
898 +       TP_fast_assign(
899 +               __assign_str(s, s);
900 +       ),
902 +       TP_printk("%s",
903 +               __get_str(s))
906 +TRACE_EVENT(jbd2_jmap_printf1,
908 +       TP_PROTO(const char *s, sector_t fsblk),
910 +       TP_ARGS(s, fsblk),
912 +       TP_STRUCT__entry(
913 +               __string(s, s)
914 +               __field(sector_t, fsblk )
915 +       ),
917 +       TP_fast_assign(
918 +               __assign_str(s, s);
919 +               __entry->fsblk  = fsblk;
920 +       ),
922 +       TP_printk("%s: %llu",
923 +                 __get_str(s),
924 +                 (unsigned long long) __entry->fsblk)
927 +TRACE_EVENT(jbd2_jmap_printf2,
929 +       TP_PROTO(const char *s, sector_t fsblk, sector_t logblk),
931 +       TP_ARGS(s, fsblk, logblk),
933 +       TP_STRUCT__entry(
934 +               __string(s, s)
935 +               __field(sector_t, fsblk )
936 +               __field(sector_t, logblk)
937 +       ),
939 +       TP_fast_assign(
940 +               __assign_str(s, s);
941 +               __entry->fsblk  = fsblk;
942 +               __entry->logblk = logblk;
943 +       ),
945 +       TP_printk("%s: %llu:%llu",
946 +                 __get_str(s),
947 +                 (unsigned long long) __entry->fsblk,
948 +                 (unsigned long long) __entry->logblk)
951 +TRACE_EVENT(jbd2_transaction_infos_add,
953 +       TP_PROTO(int t_idx, struct transaction_info *ti, int nr_mappings),
955 +       TP_ARGS(t_idx, ti, nr_mappings),
957 +       TP_STRUCT__entry(
958 +               __field(int, t_idx      )
959 +               __field(tid_t, tid      )
960 +               __field(sector_t, offset)
961 +               __field(int, nr_mappings)
962 +       ),
964 +       TP_fast_assign(
965 +               __entry->t_idx  = t_idx;
966 +               __entry->tid    = ti->tid;
967 +               __entry->offset = ti->offset;
968 +               __entry->nr_mappings = nr_mappings;
969 +       ),
971 +       TP_printk("inserted transaction %u (offset %llu) at index %d with %d mappings",
972 +                 __entry->tid,
973 +                 (unsigned long long) __entry->offset,
974 +                 __entry->t_idx,
975 +                 __entry->nr_mappings)
978  #endif /* _TRACE_JBD2_H */
980  /* This part must be outside protection */