Add a new lock for synchronizing between journal IO path and btree write
buffer flush.

Signed-off-by: Kent Overstreet <[email protected]>
---
 fs/bcachefs/journal.c       | 1 +
 fs/bcachefs/journal_io.c    | 2 ++
 fs/bcachefs/journal_types.h | 6 ++++++
 3 files changed, 9 insertions(+)

diff --git a/fs/bcachefs/journal.c b/fs/bcachefs/journal.c
index 7d448136434b..3a6b73d4ec24 100644
--- a/fs/bcachefs/journal.c
+++ b/fs/bcachefs/journal.c
@@ -1215,6 +1215,7 @@ int bch2_fs_journal_init(struct journal *j)
        static struct lock_class_key res_key;
        unsigned i;
 
+       mutex_init(&j->buf_lock);
        spin_lock_init(&j->lock);
        spin_lock_init(&j->err_lock);
        init_waitqueue_head(&j->wait);
diff --git a/fs/bcachefs/journal_io.c b/fs/bcachefs/journal_io.c
index 51796454bba4..2394d4b02fff 100644
--- a/fs/bcachefs/journal_io.c
+++ b/fs/bcachefs/journal_io.c
@@ -1869,9 +1869,11 @@ void bch2_journal_write(struct closure *cl)
        if (ret)
                goto err;
 
+       mutex_lock(&j->buf_lock);
        journal_buf_realloc(j, w);
 
        ret = bch2_journal_write_prep(j, w);
+       mutex_unlock(&j->buf_lock);
        if (ret)
                goto err;
 
diff --git a/fs/bcachefs/journal_types.h b/fs/bcachefs/journal_types.h
index 2427cce64fed..0fed32f94976 100644
--- a/fs/bcachefs/journal_types.h
+++ b/fs/bcachefs/journal_types.h
@@ -181,6 +181,12 @@ struct journal {
         */
        darray_u64              early_journal_entries;
 
+       /*
+        * Protects journal_buf->data, when accessing without a jorunal
+        * reservation: for synchronization between the btree write buffer code
+        * and the journal write path:
+        */
+       struct mutex            buf_lock;
        /*
         * Two journal entries -- one is currently open for new entries, the
         * other is possibly being written out.
-- 
2.42.0


Reply via email to