Skip to content

Commit 5975992

Browse files
zhangyi089tytso
authored andcommitted
jbd2: discard dirty data when forgetting an un-journalled buffer
We do not unmap and clear dirty flag when forgetting a buffer without journal or does not belongs to any transaction, so the invalid dirty data may still be written to the disk later. It's fine if the corresponding block is never used before the next mount, and it's also fine that we invoke clean_bdev_aliases() related functions to unmap the block device mapping when re-allocating such freed block as data block. But this logic is somewhat fragile and risky that may lead to data corruption if we forget to clean bdev aliases. So, It's better to discard dirty data during forget time. We have been already handled all the cases of forgetting journalled buffer, this patch deal with the remaining two cases. - buffer is not journalled yet, - buffer is journalled but doesn't belongs to any transaction. We invoke __bforget() instead of __brelese() when forgetting an un-journalled buffer in jbd2_journal_forget(). After this patch we can remove all clean_bdev_aliases() related calls in ext4. Suggested-by: Jan Kara <jack@suse.cz> Signed-off-by: zhangyi (F) <yi.zhang@huawei.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jan Kara <jack@suse.cz>
1 parent 904cdbd commit 5975992

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

fs/jbd2/transaction.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,9 +1597,7 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
15971597
__jbd2_journal_unfile_buffer(jh);
15981598
if (!buffer_jbd(bh)) {
15991599
spin_unlock(&journal->j_list_lock);
1600-
jbd_unlock_bh_state(bh);
1601-
__bforget(bh);
1602-
goto drop;
1600+
goto not_jbd;
16031601
}
16041602
}
16051603
spin_unlock(&journal->j_list_lock);
@@ -1632,9 +1630,40 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
16321630
if (was_modified)
16331631
drop_reserve = 1;
16341632
}
1633+
} else {
1634+
/*
1635+
* Finally, if the buffer is not belongs to any
1636+
* transaction, we can just drop it now if it has no
1637+
* checkpoint.
1638+
*/
1639+
spin_lock(&journal->j_list_lock);
1640+
if (!jh->b_cp_transaction) {
1641+
JBUFFER_TRACE(jh, "belongs to none transaction");
1642+
spin_unlock(&journal->j_list_lock);
1643+
goto not_jbd;
1644+
}
1645+
1646+
/*
1647+
* Otherwise, if the buffer has been written to disk,
1648+
* it is safe to remove the checkpoint and drop it.
1649+
*/
1650+
if (!buffer_dirty(bh)) {
1651+
__jbd2_journal_remove_checkpoint(jh);
1652+
spin_unlock(&journal->j_list_lock);
1653+
goto not_jbd;
1654+
}
1655+
1656+
/*
1657+
* The buffer is still not written to disk, we should
1658+
* attach this buffer to current transaction so that the
1659+
* buffer can be checkpointed only after the current
1660+
* transaction commits.
1661+
*/
1662+
clear_buffer_dirty(bh);
1663+
__jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1664+
spin_unlock(&journal->j_list_lock);
16351665
}
16361666

1637-
not_jbd:
16381667
jbd_unlock_bh_state(bh);
16391668
__brelse(bh);
16401669
drop:
@@ -1643,6 +1672,11 @@ int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
16431672
handle->h_buffer_credits++;
16441673
}
16451674
return err;
1675+
1676+
not_jbd:
1677+
jbd_unlock_bh_state(bh);
1678+
__bforget(bh);
1679+
goto drop;
16461680
}
16471681

16481682
/**

0 commit comments

Comments
 (0)