]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal.c
Update bcachefs sources to 841a95c29f4c bcachefs: fix userspace build errors
[bcachefs-tools-debian] / libbcachefs / journal.c
index acf9c358b82df7f576b4f071ba5e422febdbf260..8294d7fd6632be802b4fb43302ca0407231fbedd 100644 (file)
@@ -10,6 +10,7 @@
 #include "bkey_methods.h"
 #include "btree_gc.h"
 #include "btree_update.h"
+#include "btree_write_buffer.h"
 #include "buckets.h"
 #include "error.h"
 #include "journal.h"
@@ -251,7 +252,7 @@ static bool journal_entry_want_write(struct journal *j)
        return ret;
 }
 
-static bool journal_entry_close(struct journal *j)
+bool bch2_journal_entry_close(struct journal *j)
 {
        bool ret;
 
@@ -332,6 +333,7 @@ static int journal_entry_open(struct journal *j)
        buf->must_flush = false;
        buf->separate_flush = false;
        buf->flush_time = 0;
+       buf->need_flush_to_write_buffer = true;
 
        memset(buf->data, 0, sizeof(*buf->data));
        buf->data->seq  = cpu_to_le64(journal_cur_seq(j));
@@ -380,7 +382,7 @@ static bool journal_quiesced(struct journal *j)
        bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
 
        if (!ret)
-               journal_entry_close(j);
+               bch2_journal_entry_close(j);
        return ret;
 }
 
@@ -433,7 +435,7 @@ retry:
 
        /*
         * Recheck after taking the lock, so we don't race with another thread
-        * that just did journal_entry_open() and call journal_entry_close()
+        * that just did journal_entry_open() and call bch2_journal_entry_close()
         * unnecessarily
         */
        if (journal_res_get_fast(j, res, flags)) {
@@ -768,6 +770,48 @@ void bch2_journal_block(struct journal *j)
        journal_quiesce(j);
 }
 
+static struct journal_buf *__bch2_next_write_buffer_flush_journal_buf(struct journal *j, u64 max_seq)
+{
+       struct journal_buf *ret = NULL;
+
+       mutex_lock(&j->buf_lock);
+       spin_lock(&j->lock);
+       max_seq = min(max_seq, journal_cur_seq(j));
+
+       for (u64 seq = journal_last_unwritten_seq(j);
+            seq <= max_seq;
+            seq++) {
+               unsigned idx = seq & JOURNAL_BUF_MASK;
+               struct journal_buf *buf = j->buf + idx;
+
+               if (buf->need_flush_to_write_buffer) {
+                       if (seq == journal_cur_seq(j))
+                               __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
+
+                       union journal_res_state s;
+                       s.v = atomic64_read_acquire(&j->reservations.counter);
+
+                       ret = journal_state_count(s, idx)
+                               ? ERR_PTR(-EAGAIN)
+                               : buf;
+                       break;
+               }
+       }
+
+       spin_unlock(&j->lock);
+       if (IS_ERR_OR_NULL(ret))
+               mutex_unlock(&j->buf_lock);
+       return ret;
+}
+
+struct journal_buf *bch2_next_write_buffer_flush_journal_buf(struct journal *j, u64 max_seq)
+{
+       struct journal_buf *ret;
+
+       wait_event(j->wait, (ret = __bch2_next_write_buffer_flush_journal_buf(j, max_seq)) != ERR_PTR(-EAGAIN));
+       return ret;
+}
+
 /* allocate journal on a device: */
 
 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
@@ -1035,7 +1079,7 @@ void bch2_fs_journal_stop(struct journal *j)
        bch2_journal_reclaim_stop(j);
        bch2_journal_flush_all_pins(j);
 
-       wait_event(j->wait, journal_entry_close(j));
+       wait_event(j->wait, bch2_journal_entry_close(j));
 
        /*
         * Always write a new journal entry, to make sure the clock hands are up
@@ -1219,6 +1263,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);