add patch fix-compile-error-when-using-BUFFER_TRACE
[ext4-patch-queue.git] / discard-dirty-data-when-forgetting-unjournaled-buffer
blobf5a59704a7a9c7b0c4f42c40ad82c1b0d2d47958
1 jbd2: discard dirty data when forgetting an un-journalled buffer
3 From: "zhangyi (F)" <yi.zhang@huawei.com>
5 We do not unmap and clear dirty flag when forgetting a buffer without
6 journal or does not belongs to any transaction, so the invalid dirty
7 data may still be written to the disk later. It's fine if the
8 corresponding block is never used before the next mount, and it's also
9 fine that we invoke clean_bdev_aliases() related functions to unmap
10 the block device mapping when re-allocating such freed block as data
11 block. But this logic is somewhat fragile and risky that may lead to
12 data corruption if we forget to clean bdev aliases. So, It's better to
13 discard dirty data during forget time.
15 We have been already handled all the cases of forgetting journalled
16 buffer, this patch deal with the remaining two cases.
18 - buffer is not journalled yet,
19 - buffer is journalled but doesn't belongs to any transaction.
21 We invoke __bforget() instead of __brelese() when forgetting an
22 un-journalled buffer in jbd2_journal_forget(). After this patch we can
23 remove all clean_bdev_aliases() related calls in ext4.
25 Suggested-by: Jan Kara <jack@suse.cz>
26 Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>
27 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
28 Reviewed-by: Jan Kara <jack@suse.cz>
29 ---
30  fs/jbd2/transaction.c | 42 ++++++++++++++++++++++++++++++++++++++----
31  1 file changed, 38 insertions(+), 4 deletions(-)
33 diff --git a/fs/jbd2/transaction.c b/fs/jbd2/transaction.c
34 index f0d8dab..a43b630 100644
35 --- a/fs/jbd2/transaction.c
36 +++ b/fs/jbd2/transaction.c
37 @@ -1597,9 +1597,7 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
38                         __jbd2_journal_unfile_buffer(jh);
39                         if (!buffer_jbd(bh)) {
40                                 spin_unlock(&journal->j_list_lock);
41 -                               jbd_unlock_bh_state(bh);
42 -                               __bforget(bh);
43 -                               goto drop;
44 +                               goto not_jbd;
45                         }
46                 }
47                 spin_unlock(&journal->j_list_lock);
48 @@ -1632,9 +1630,40 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
49                         if (was_modified)
50                                 drop_reserve = 1;
51                 }
52 +       } else {
53 +               /*
54 +                * Finally, if the buffer is not belongs to any
55 +                * transaction, we can just drop it now if it has no
56 +                * checkpoint.
57 +                */
58 +               spin_lock(&journal->j_list_lock);
59 +               if (!jh->b_cp_transaction) {
60 +                       JBUFFER_TRACE(jh, "belongs to none transaction");
61 +                       spin_unlock(&journal->j_list_lock);
62 +                       goto not_jbd;
63 +               }
65 +               /*
66 +                * Otherwise, if the buffer has been written to disk,
67 +                * it is safe to remove the checkpoint and drop it.
68 +                */
69 +               if (!buffer_dirty(bh)) {
70 +                       __jbd2_journal_remove_checkpoint(jh);
71 +                       spin_unlock(&journal->j_list_lock);
72 +                       goto not_jbd;
73 +               }
75 +               /*
76 +                * The buffer is still not written to disk, we should
77 +                * attach this buffer to current transaction so that the
78 +                * buffer can be checkpointed only after the current
79 +                * transaction commits.
80 +                */
81 +               clear_buffer_dirty(bh);
82 +               __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
83 +               spin_unlock(&journal->j_list_lock);
84         }
86 -not_jbd:
87         jbd_unlock_bh_state(bh);
88         __brelse(bh);
89  drop:
90 @@ -1643,6 +1672,11 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
91                 handle->h_buffer_credits++;
92         }
93         return err;
95 +not_jbd:
96 +       jbd_unlock_bh_state(bh);
97 +       __bforget(bh);
98 +       goto drop;
99  }
101  /**
102 -- 
103 2.7.4