Skip to content

Commit 2ef995d

Browse files
name2965Kent Overstreet
authored andcommitted
bcachefs: fix deadlock in journal_entry_open()
In the previous commit b3d82c2, code was added to prevent journal sequence overflow. Among them, the code added to journal_entry_open() uses the bch2_fs_fatal_err_on() function to handle errors. However, __journal_res_get() , which calls journal_entry_open() , calls journal_entry_open() while holding journal->lock , but bch2_fs_fatal_err_on() internally tries to acquire journal->lock , which results in a deadlock. So we need to add a locked helper to handle fatal errors even when the journal->lock is held. Fixes: b3d82c2 ("bcachefs: Guard against journal seq overflow") Signed-off-by: Jeongjun Park <aha310510@gmail.com> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 6b37037 commit 2ef995d

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

fs/bcachefs/journal.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ void bch2_journal_halt(struct journal *j)
319319
spin_unlock(&j->lock);
320320
}
321321

322+
void bch2_journal_halt_locked(struct journal *j)
323+
{
324+
lockdep_assert_held(&j->lock);
325+
326+
__journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL, true);
327+
if (!j->err_seq)
328+
j->err_seq = journal_cur_seq(j);
329+
journal_wake(j);
330+
}
331+
322332
static bool journal_entry_want_write(struct journal *j)
323333
{
324334
bool ret = !journal_entry_is_open(j) ||
@@ -381,9 +391,12 @@ static int journal_entry_open(struct journal *j)
381391
if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf))
382392
return JOURNAL_ERR_max_in_flight;
383393

384-
if (bch2_fs_fatal_err_on(journal_cur_seq(j) >= JOURNAL_SEQ_MAX,
385-
c, "cannot start: journal seq overflow"))
394+
if (journal_cur_seq(j) >= JOURNAL_SEQ_MAX) {
395+
bch_err(c, "cannot start: journal seq overflow");
396+
if (bch2_fs_emergency_read_only_locked(c))
397+
bch_err(c, "fatal error - emergency read only");
386398
return JOURNAL_ERR_insufficient_devices; /* -EROFS */
399+
}
387400

388401
BUG_ON(!j->cur_entry_sectors);
389402

fs/bcachefs/journal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ bool bch2_journal_noflush_seq(struct journal *, u64, u64);
409409
int bch2_journal_meta(struct journal *);
410410

411411
void bch2_journal_halt(struct journal *);
412+
void bch2_journal_halt_locked(struct journal *);
412413

413414
static inline int bch2_journal_error(struct journal *j)
414415
{

fs/bcachefs/super.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,17 @@ bool bch2_fs_emergency_read_only(struct bch_fs *c)
411411
return ret;
412412
}
413413

414+
bool bch2_fs_emergency_read_only_locked(struct bch_fs *c)
415+
{
416+
bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags);
417+
418+
bch2_journal_halt_locked(&c->journal);
419+
bch2_fs_read_only_async(c);
420+
421+
wake_up(&bch2_read_only_wait);
422+
return ret;
423+
}
424+
414425
static int bch2_fs_read_write_late(struct bch_fs *c)
415426
{
416427
int ret;

fs/bcachefs/super.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int bch2_dev_resize(struct bch_fs *, struct bch_dev *, u64);
2929
struct bch_dev *bch2_dev_lookup(struct bch_fs *, const char *);
3030

3131
bool bch2_fs_emergency_read_only(struct bch_fs *);
32+
bool bch2_fs_emergency_read_only_locked(struct bch_fs *);
3233
void bch2_fs_read_only(struct bch_fs *);
3334

3435
int bch2_fs_read_write(struct bch_fs *);

0 commit comments

Comments
 (0)