]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
bcachefs: fix deadlock in journal_entry_open()
authorJeongjun Park <aha310510@gmail.com>
Sun, 2 Feb 2025 06:13:51 +0000 (15:13 +0900)
committerKent Overstreet <kent.overstreet@linux.dev>
Fri, 7 Feb 2025 03:35:11 +0000 (22:35 -0500)
In the previous commit b3d82c2f2761, 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: b3d82c2f2761 ("bcachefs: Guard against journal seq overflow")
Signed-off-by: Jeongjun Park <aha310510@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/journal.c
fs/bcachefs/journal.h
fs/bcachefs/super.c
fs/bcachefs/super.h

index cb2c3722f674198948968519a22dfc8f9ad784eb..0a943a27ef44985f6c5bbb35b37aadadf1f17d1c 100644 (file)
@@ -319,6 +319,16 @@ void bch2_journal_halt(struct journal *j)
        spin_unlock(&j->lock);
 }
 
+void bch2_journal_halt_locked(struct journal *j)
+{
+       lockdep_assert_held(&j->lock);
+
+       __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL, true);
+       if (!j->err_seq)
+               j->err_seq = journal_cur_seq(j);
+       journal_wake(j);
+}
+
 static bool journal_entry_want_write(struct journal *j)
 {
        bool ret = !journal_entry_is_open(j) ||
@@ -381,9 +391,12 @@ static int journal_entry_open(struct journal *j)
        if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf))
                return JOURNAL_ERR_max_in_flight;
 
-       if (bch2_fs_fatal_err_on(journal_cur_seq(j) >= JOURNAL_SEQ_MAX,
-                                c, "cannot start: journal seq overflow"))
+       if (journal_cur_seq(j) >= JOURNAL_SEQ_MAX) {
+               bch_err(c, "cannot start: journal seq overflow");
+               if (bch2_fs_emergency_read_only_locked(c))
+                       bch_err(c, "fatal error - emergency read only");
                return JOURNAL_ERR_insufficient_devices; /* -EROFS */
+       }
 
        BUG_ON(!j->cur_entry_sectors);
 
index dccddd5420adf6c8f58ddf9bf3def6e04b2225aa..107f7f901cd968ec66f07183117f318cd794dac4 100644 (file)
@@ -409,6 +409,7 @@ bool bch2_journal_noflush_seq(struct journal *, u64, u64);
 int bch2_journal_meta(struct journal *);
 
 void bch2_journal_halt(struct journal *);
+void bch2_journal_halt_locked(struct journal *);
 
 static inline int bch2_journal_error(struct journal *j)
 {
index d97ea7bd1171bbf8af02daefe0d976c5e17fb1a6..6d97d412fed98b5e1f824e1fa352facc96bea200 100644 (file)
@@ -411,6 +411,17 @@ bool bch2_fs_emergency_read_only(struct bch_fs *c)
        return ret;
 }
 
+bool bch2_fs_emergency_read_only_locked(struct bch_fs *c)
+{
+       bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags);
+
+       bch2_journal_halt_locked(&c->journal);
+       bch2_fs_read_only_async(c);
+
+       wake_up(&bch2_read_only_wait);
+       return ret;
+}
+
 static int bch2_fs_read_write_late(struct bch_fs *c)
 {
        int ret;
index fa6d5221651082600c28d4c761e62bd18cb4e15c..04f8287eff5c3d30f035f8763b69cf11e5bcb5aa 100644 (file)
@@ -29,6 +29,7 @@ int bch2_dev_resize(struct bch_fs *, struct bch_dev *, u64);
 struct bch_dev *bch2_dev_lookup(struct bch_fs *, const char *);
 
 bool bch2_fs_emergency_read_only(struct bch_fs *);
+bool bch2_fs_emergency_read_only_locked(struct bch_fs *);
 void bch2_fs_read_only(struct bch_fs *);
 
 int bch2_fs_read_write(struct bch_fs *);