Skip to content

Commit e34c8dd

Browse files
Zhihao Chengtytso
authored andcommitted
jbd2: Fix wrongly judgement for buffer head removing while doing checkpoint
Following process, jbd2_journal_commit_transaction // there are several dirty buffer heads in transaction->t_checkpoint_list P1 wb_workfn jbd2_log_do_checkpoint if (buffer_locked(bh)) // false __block_write_full_page trylock_buffer(bh) test_clear_buffer_dirty(bh) if (!buffer_dirty(bh)) __jbd2_journal_remove_checkpoint(jh) if (buffer_write_io_error(bh)) // false >> bh IO error occurs << jbd2_cleanup_journal_tail __jbd2_update_log_tail jbd2_write_superblock // The bh won't be replayed in next mount. , which could corrupt the ext4 image, fetch a reproducer in [Link]. Since writeback process clears buffer dirty after locking buffer head, we can fix it by try locking buffer and check dirtiness while buffer is locked, the buffer head can be removed if it is neither dirty nor locked. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217490 Fixes: 470decc ("[PATCH] jbd2: initial copy of files from jbd") Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com> Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20230606135928.434610-5-yi.zhang@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent b98dba2 commit e34c8dd

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

fs/jbd2/checkpoint.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,6 @@ int jbd2_log_do_checkpoint(journal_t *journal)
204204
jh = transaction->t_checkpoint_list;
205205
bh = jh2bh(jh);
206206

207-
/*
208-
* The buffer may be writing back, or flushing out in the
209-
* last couple of cycles, or re-adding into a new transaction,
210-
* need to check it again until it's unlocked.
211-
*/
212-
if (buffer_locked(bh)) {
213-
get_bh(bh);
214-
spin_unlock(&journal->j_list_lock);
215-
wait_on_buffer(bh);
216-
/* the journal_head may have gone by now */
217-
BUFFER_TRACE(bh, "brelse");
218-
__brelse(bh);
219-
goto retry;
220-
}
221207
if (jh->b_transaction != NULL) {
222208
transaction_t *t = jh->b_transaction;
223209
tid_t tid = t->t_tid;
@@ -252,7 +238,22 @@ int jbd2_log_do_checkpoint(journal_t *journal)
252238
spin_lock(&journal->j_list_lock);
253239
goto restart;
254240
}
255-
if (!buffer_dirty(bh)) {
241+
if (!trylock_buffer(bh)) {
242+
/*
243+
* The buffer is locked, it may be writing back, or
244+
* flushing out in the last couple of cycles, or
245+
* re-adding into a new transaction, need to check
246+
* it again until it's unlocked.
247+
*/
248+
get_bh(bh);
249+
spin_unlock(&journal->j_list_lock);
250+
wait_on_buffer(bh);
251+
/* the journal_head may have gone by now */
252+
BUFFER_TRACE(bh, "brelse");
253+
__brelse(bh);
254+
goto retry;
255+
} else if (!buffer_dirty(bh)) {
256+
unlock_buffer(bh);
256257
BUFFER_TRACE(bh, "remove from checkpoint");
257258
/*
258259
* If the transaction was released or the checkpoint
@@ -262,6 +263,7 @@ int jbd2_log_do_checkpoint(journal_t *journal)
262263
!transaction->t_checkpoint_list)
263264
goto out;
264265
} else {
266+
unlock_buffer(bh);
265267
/*
266268
* We are about to write the buffer, it could be
267269
* raced by some other transaction shrink or buffer

0 commit comments

Comments
 (0)