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>
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);
47 spin_unlock(&journal->j_list_lock);
48 @@ -1632,9 +1630,40 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
54 + * Finally, if the buffer is not belongs to any
55 + * transaction, we can just drop it now if it has no
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);
66 + * Otherwise, if the buffer has been written to disk,
67 + * it is safe to remove the checkpoint and drop it.
69 + if (!buffer_dirty(bh)) {
70 + __jbd2_journal_remove_checkpoint(jh);
71 + spin_unlock(&journal->j_list_lock);
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.
81 + clear_buffer_dirty(bh);
82 + __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
83 + spin_unlock(&journal->j_list_lock);
87 jbd_unlock_bh_state(bh);
90 @@ -1643,6 +1672,11 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
91 handle->h_buffer_credits++;
96 + jbd_unlock_bh_state(bh);