]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal.h
Merge pull request #190 from Dikay900/fs_free_space
[bcachefs-tools-debian] / libbcachefs / journal.h
index 51d29a01b7b2c0d2979872a9cbdbc2bfd7b389c9..e1e9e60fa5360590aab042296dac685b607af90d 100644 (file)
  */
 
 #include <linux/hash.h>
-#include <linux/prefetch.h>
 
 #include "journal_types.h"
 
@@ -120,7 +119,6 @@ static inline void journal_wake(struct journal *j)
 {
        wake_up(&j->wait);
        closure_wake_up(&j->async_wait);
-       closure_wake_up(&j->preres_wait);
 }
 
 static inline struct journal_buf *journal_cur_buf(struct journal *j)
@@ -137,9 +135,7 @@ static inline u64 journal_last_seq(struct journal *j)
 
 static inline u64 journal_cur_seq(struct journal *j)
 {
-       EBUG_ON(j->pin.back - 1 != atomic64_read(&j->seq));
-
-       return j->pin.back - 1;
+       return atomic64_read(&j->seq);
 }
 
 static inline u64 journal_last_unwritten_seq(struct journal *j)
@@ -253,9 +249,10 @@ static inline bool journal_entry_empty(struct jset *j)
        return true;
 }
 
-void __bch2_journal_buf_put(struct journal *);
-
-static inline void bch2_journal_buf_put(struct journal *j, unsigned idx)
+/*
+ * Drop reference on a buffer index and return true if the count has hit zero.
+ */
+static inline union journal_res_state journal_state_buf_put(struct journal *j, unsigned idx)
 {
        union journal_res_state s;
 
@@ -265,9 +262,30 @@ static inline void bch2_journal_buf_put(struct journal *j, unsigned idx)
                                    .buf2_count = idx == 2,
                                    .buf3_count = idx == 3,
                                    }).v, &j->reservations.counter);
+       return s;
+}
+
+void bch2_journal_buf_put_final(struct journal *, u64, bool);
 
-       if (!journal_state_count(s, idx) && idx == s.unwritten_idx)
-               __bch2_journal_buf_put(j);
+static inline void __bch2_journal_buf_put(struct journal *j, unsigned idx, u64 seq)
+{
+       union journal_res_state s;
+
+       s = journal_state_buf_put(j, idx);
+       if (!journal_state_count(s, idx))
+               bch2_journal_buf_put_final(j, seq, idx == s.unwritten_idx);
+}
+
+static inline void bch2_journal_buf_put(struct journal *j, unsigned idx, u64 seq)
+{
+       union journal_res_state s;
+
+       s = journal_state_buf_put(j, idx);
+       if (!journal_state_count(s, idx)) {
+               spin_lock(&j->lock);
+               bch2_journal_buf_put_final(j, seq, idx == s.unwritten_idx);
+               spin_unlock(&j->lock);
+       }
 }
 
 /*
@@ -287,7 +305,7 @@ static inline void bch2_journal_res_put(struct journal *j,
                                       BCH_JSET_ENTRY_btree_keys,
                                       0, 0, 0);
 
-       bch2_journal_buf_put(j, res->idx);
+       bch2_journal_buf_put(j, res->idx, res->seq);
 
        res->ref = 0;
 }
@@ -295,9 +313,14 @@ static inline void bch2_journal_res_put(struct journal *j,
 int bch2_journal_res_get_slowpath(struct journal *, struct journal_res *,
                                  unsigned);
 
-/* First two bits for JOURNAL_WATERMARK: */
-#define JOURNAL_RES_GET_NONBLOCK       (1 << 2)
-#define JOURNAL_RES_GET_CHECK          (1 << 3)
+/* First bits for BCH_WATERMARK: */
+enum journal_res_flags {
+       __JOURNAL_RES_GET_NONBLOCK      = BCH_WATERMARK_BITS,
+       __JOURNAL_RES_GET_CHECK,
+};
+
+#define JOURNAL_RES_GET_NONBLOCK       (1 << __JOURNAL_RES_GET_NONBLOCK)
+#define JOURNAL_RES_GET_CHECK          (1 << __JOURNAL_RES_GET_CHECK)
 
 static inline int journal_res_get_fast(struct journal *j,
                                       struct journal_res *res,
@@ -305,34 +328,23 @@ static inline int journal_res_get_fast(struct journal *j,
 {
        union journal_res_state old, new;
        u64 v = atomic64_read(&j->reservations.counter);
-       unsigned u64s, offset;
 
        do {
                old.v = new.v = v;
 
-               /*
-                * Round up the end of the journal reservation to the next
-                * cacheline boundary:
-                */
-               u64s = res->u64s;
-               offset = sizeof(struct jset) / sizeof(u64) +
-                         new.cur_entry_offset + u64s;
-               u64s += ((offset - 1) & ((SMP_CACHE_BYTES / sizeof(u64)) - 1)) + 1;
-
-
                /*
                 * Check if there is still room in the current journal
                 * entry:
                 */
-               if (new.cur_entry_offset + u64s > j->cur_entry_u64s)
+               if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
                        return 0;
 
                EBUG_ON(!journal_state_count(new, new.idx));
 
-               if ((flags & JOURNAL_WATERMARK_MASK) < j->watermark)
+               if ((flags & BCH_WATERMARK_MASK) < j->watermark)
                        return 0;
 
-               new.cur_entry_offset += u64s;
+               new.cur_entry_offset += res->u64s;
                journal_state_inc(&new);
 
                /*
@@ -349,15 +361,8 @@ static inline int journal_res_get_fast(struct journal *j,
 
        res->ref        = true;
        res->idx        = old.idx;
-       res->u64s       = u64s;
        res->offset     = old.cur_entry_offset;
        res->seq        = le64_to_cpu(j->buf[old.idx].data->seq);
-
-       offset = res->offset;
-       while (offset < res->offset + res->u64s) {
-               prefetchw(vstruct_idx(j->buf[res->idx].data, offset));
-               offset += SMP_CACHE_BYTES / sizeof(u64);
-       }
        return 1;
 }
 
@@ -387,103 +392,6 @@ out:
        return 0;
 }
 
-/* journal_preres: */
-
-static inline void journal_set_watermark(struct journal *j)
-{
-       union journal_preres_state s = READ_ONCE(j->prereserved);
-       unsigned watermark = JOURNAL_WATERMARK_any;
-
-       if (fifo_free(&j->pin) < j->pin.size / 4)
-               watermark = max_t(unsigned, watermark, JOURNAL_WATERMARK_copygc);
-       if (fifo_free(&j->pin) < j->pin.size / 8)
-               watermark = max_t(unsigned, watermark, JOURNAL_WATERMARK_reserved);
-
-       if (s.reserved > s.remaining)
-               watermark = max_t(unsigned, watermark, JOURNAL_WATERMARK_copygc);
-       if (!s.remaining)
-               watermark = max_t(unsigned, watermark, JOURNAL_WATERMARK_reserved);
-
-       if (watermark == j->watermark)
-               return;
-
-       swap(watermark, j->watermark);
-       if (watermark > j->watermark)
-               journal_wake(j);
-}
-
-static inline void bch2_journal_preres_put(struct journal *j,
-                                          struct journal_preres *res)
-{
-       union journal_preres_state s = { .reserved = res->u64s };
-
-       if (!res->u64s)
-               return;
-
-       s.v = atomic64_sub_return(s.v, &j->prereserved.counter);
-       res->u64s = 0;
-
-       if (unlikely(s.waiting)) {
-               clear_bit(ilog2((((union journal_preres_state) { .waiting = 1 }).v)),
-                         (unsigned long *) &j->prereserved.v);
-               closure_wake_up(&j->preres_wait);
-       }
-
-       if (s.reserved <= s.remaining && j->watermark)
-               journal_set_watermark(j);
-}
-
-int __bch2_journal_preres_get(struct journal *,
-                       struct journal_preres *, unsigned, unsigned);
-
-static inline int bch2_journal_preres_get_fast(struct journal *j,
-                                              struct journal_preres *res,
-                                              unsigned new_u64s,
-                                              unsigned flags,
-                                              bool set_waiting)
-{
-       int d = new_u64s - res->u64s;
-       union journal_preres_state old, new;
-       u64 v = atomic64_read(&j->prereserved.counter);
-       int ret;
-
-       do {
-               old.v = new.v = v;
-               ret = 0;
-
-               if ((flags & JOURNAL_WATERMARK_reserved) ||
-                   new.reserved + d < new.remaining) {
-                       new.reserved += d;
-                       ret = 1;
-               } else if (set_waiting && !new.waiting)
-                       new.waiting = true;
-               else
-                       return 0;
-       } while ((v = atomic64_cmpxchg(&j->prereserved.counter,
-                                      old.v, new.v)) != old.v);
-
-       if (ret)
-               res->u64s += d;
-       return ret;
-}
-
-static inline int bch2_journal_preres_get(struct journal *j,
-                                         struct journal_preres *res,
-                                         unsigned new_u64s,
-                                         unsigned flags)
-{
-       if (new_u64s <= res->u64s)
-               return 0;
-
-       if (bch2_journal_preres_get_fast(j, res, new_u64s, flags, false))
-               return 0;
-
-       if (flags & JOURNAL_RES_GET_NONBLOCK)
-               return -EAGAIN;
-
-       return __bch2_journal_preres_get(j, res, new_u64s, flags);
-}
-
 /* journal_entry_res: */
 
 void bch2_journal_entry_res_resize(struct journal *,
@@ -497,7 +405,6 @@ int bch2_journal_flush_seq(struct journal *, u64);
 int bch2_journal_flush(struct journal *);
 bool bch2_journal_noflush_seq(struct journal *, u64);
 int bch2_journal_meta(struct journal *);
-int bch2_journal_log_msg(struct journal *, const char *, ...);
 
 void bch2_journal_halt(struct journal *);
 
@@ -526,6 +433,7 @@ bool bch2_journal_seq_pins_to_text(struct printbuf *, struct journal *, u64 *);
 int bch2_set_nr_journal_buckets(struct bch_fs *, struct bch_dev *,
                                unsigned nr);
 int bch2_dev_journal_alloc(struct bch_dev *);
+int bch2_fs_journal_alloc(struct bch_fs *);
 
 void bch2_dev_journal_stop(struct journal *, struct bch_dev *);