-#ifndef _BCACHE_JOURNAL_H
-#define _BCACHE_JOURNAL_H
+#ifndef _BCACHEFS_JOURNAL_H
+#define _BCACHEFS_JOURNAL_H
/*
* THE JOURNAL:
#include "journal_types.h"
-/*
- * Only used for holding the journal entries we read in btree_journal_read()
- * during cache_registration
- */
-struct journal_replay {
- struct list_head list;
- struct jset j;
-};
+struct bch_fs;
-#define JOURNAL_PIN (32 * 1024)
+static inline void journal_wake(struct journal *j)
+{
+ wake_up(&j->wait);
+ closure_wake_up(&j->async_wait);
+}
-static inline bool journal_pin_active(struct journal_entry_pin *pin)
+static inline struct journal_buf *journal_cur_buf(struct journal *j)
{
- return pin->pin_list != NULL;
+ return j->buf + j->reservations.idx;
}
-static inline struct journal_entry_pin_list *
-journal_seq_pin(struct journal *j, u64 seq)
+static inline struct journal_buf *journal_prev_buf(struct journal *j)
{
- return &j->pin.data[(size_t) seq & j->pin.mask];
+ return j->buf + !j->reservations.idx;
}
-void bch2_journal_pin_add(struct journal *, struct journal_res *,
- struct journal_entry_pin *, journal_pin_flush_fn);
-void bch2_journal_pin_drop(struct journal *, struct journal_entry_pin *);
-void bch2_journal_pin_add_if_older(struct journal *,
- struct journal_entry_pin *,
- struct journal_entry_pin *,
- journal_pin_flush_fn);
-void bch2_journal_flush_pins(struct journal *);
+/* Sequence number of oldest dirty journal entry */
-struct closure;
-struct bch_fs;
-struct keylist;
+static inline u64 journal_last_seq(struct journal *j)
+{
+ return j->pin.front;
+}
-struct bkey_i *bch2_journal_find_btree_root(struct bch_fs *, struct jset *,
- enum btree_id, unsigned *);
+static inline u64 journal_cur_seq(struct journal *j)
+{
+ BUG_ON(j->pin.back - 1 != atomic64_read(&j->seq));
-int bch2_journal_seq_should_ignore(struct bch_fs *, u64, struct btree *);
+ return j->pin.back - 1;
+}
u64 bch2_inode_journal_seq(struct journal *, u64);
s->buf1_count += s->idx == 1;
}
-static inline void bch2_journal_set_has_inode(struct journal_buf *buf, u64 inum)
+static inline void bch2_journal_set_has_inode(struct journal *j,
+ struct journal_res *res,
+ u64 inum)
{
- set_bit(hash_64(inum, ilog2(sizeof(buf->has_inode) * 8)), buf->has_inode);
+ struct journal_buf *buf = &j->buf[res->idx];
+ unsigned long bit = hash_64(inum, ilog2(sizeof(buf->has_inode) * 8));
+
+ /* avoid atomic op if possible */
+ if (unlikely(!test_bit(bit, buf->has_inode)))
+ set_bit(bit, buf->has_inode);
}
/*
return u64s + sizeof(struct jset_entry) / sizeof(u64);
}
-static inline void bch2_journal_add_entry_at(struct journal_buf *buf,
- const void *data, size_t u64s,
- unsigned type, enum btree_id id,
- unsigned level, unsigned offset)
+static inline struct jset_entry *
+bch2_journal_add_entry_noreservation(struct journal_buf *buf, size_t u64s)
{
- struct jset_entry *entry = vstruct_idx(buf->data, offset);
+ struct jset *jset = buf->data;
+ struct jset_entry *entry = vstruct_idx(jset, le32_to_cpu(jset->u64s));
+ memset(entry, 0, sizeof(*entry));
entry->u64s = cpu_to_le16(u64s);
- entry->btree_id = id;
- entry->level = level;
- entry->flags = 0;
- SET_JOURNAL_ENTRY_TYPE(entry, type);
- memcpy_u64s(entry->_data, data, u64s);
+ le32_add_cpu(&jset->u64s, jset_u64s(u64s));
+
+ return entry;
}
-static inline void bch2_journal_add_keys(struct journal *j, struct journal_res *res,
- enum btree_id id, const struct bkey_i *k)
+static inline void bch2_journal_add_entry(struct journal *j, struct journal_res *res,
+ unsigned type, enum btree_id id,
+ unsigned level,
+ const void *data, unsigned u64s)
{
struct journal_buf *buf = &j->buf[res->idx];
- unsigned actual = jset_u64s(k->k.u64s);
+ struct jset_entry *entry = vstruct_idx(buf->data, res->offset);
+ unsigned actual = jset_u64s(u64s);
EBUG_ON(!res->ref);
- BUG_ON(actual > res->u64s);
-
- bch2_journal_set_has_inode(buf, k->k.p.inode);
-
- bch2_journal_add_entry_at(buf, k, k->k.u64s,
- JOURNAL_ENTRY_BTREE_KEYS, id,
- 0, res->offset);
+ EBUG_ON(actual > res->u64s);
res->offset += actual;
res->u64s -= actual;
+
+ memset(entry, 0, sizeof(*entry));
+ entry->u64s = cpu_to_le16(u64s);
+ entry->type = type;
+ entry->btree_id = id;
+ entry->level = level;
+ memcpy_u64s(entry->_data, data, u64s);
+}
+
+static inline void bch2_journal_add_keys(struct journal *j, struct journal_res *res,
+ enum btree_id id, const struct bkey_i *k)
+{
+ bch2_journal_add_entry(j, res, BCH_JSET_ENTRY_btree_keys,
+ id, 0, k, k->k.u64s);
}
void bch2_journal_buf_put_slowpath(struct journal *, bool);
lock_release(&j->res_map, 0, _RET_IP_);
- while (res->u64s) {
- bch2_journal_add_entry_at(&j->buf[res->idx], NULL, 0,
- JOURNAL_ENTRY_BTREE_KEYS,
- 0, 0, res->offset);
- res->offset += jset_u64s(0);
- res->u64s -= jset_u64s(0);
- }
+ while (res->u64s)
+ bch2_journal_add_entry(j, res,
+ BCH_JSET_ENTRY_btree_keys,
+ 0, 0, NULL, 0);
bch2_journal_buf_put(j, res->idx, false);
}
int bch2_journal_res_get_slowpath(struct journal *, struct journal_res *,
- unsigned, unsigned);
+ unsigned);
static inline int journal_res_get_fast(struct journal *j,
- struct journal_res *res,
- unsigned u64s_min,
- unsigned u64s_max)
+ struct journal_res *res)
{
union journal_res_state old, new;
u64 v = atomic64_read(&j->reservations.counter);
* Check if there is still room in the current journal
* entry:
*/
- if (old.cur_entry_offset + u64s_min > j->cur_entry_u64s)
+ if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s)
return 0;
- res->offset = old.cur_entry_offset;
- res->u64s = min(u64s_max, j->cur_entry_u64s -
- old.cur_entry_offset);
-
- journal_state_inc(&new);
new.cur_entry_offset += res->u64s;
+ journal_state_inc(&new);
} while ((v = atomic64_cmpxchg(&j->reservations.counter,
old.v, new.v)) != old.v);
- res->ref = true;
- res->idx = new.idx;
- res->seq = le64_to_cpu(j->buf[res->idx].data->seq);
+ res->ref = true;
+ res->idx = old.idx;
+ res->offset = old.cur_entry_offset;
+ res->seq = le64_to_cpu(j->buf[old.idx].data->seq);
return 1;
}
+#define JOURNAL_RES_GET_NONBLOCK (1 << 0)
+
static inline int bch2_journal_res_get(struct journal *j, struct journal_res *res,
- unsigned u64s_min, unsigned u64s_max)
+ unsigned u64s, unsigned flags)
{
int ret;
EBUG_ON(res->ref);
- EBUG_ON(u64s_max < u64s_min);
+ EBUG_ON(!test_bit(JOURNAL_STARTED, &j->flags));
+
+ res->u64s = u64s;
- if (journal_res_get_fast(j, res, u64s_min, u64s_max))
+ if (journal_res_get_fast(j, res))
goto out;
- ret = bch2_journal_res_get_slowpath(j, res, u64s_min, u64s_max);
+ ret = bch2_journal_res_get_slowpath(j, res, flags);
if (ret)
return ret;
out:
return 0;
}
+u64 bch2_journal_last_unwritten_seq(struct journal *);
+int bch2_journal_open_seq_async(struct journal *, u64, struct closure *);
+
void bch2_journal_wait_on_seq(struct journal *, u64, struct closure *);
void bch2_journal_flush_seq_async(struct journal *, u64, struct closure *);
void bch2_journal_flush_async(struct journal *, struct closure *);
? -EIO : 0;
}
+struct bch_dev;
+
static inline bool journal_flushes_device(struct bch_dev *ca)
{
return true;
}
-void bch2_journal_start(struct bch_fs *);
-int bch2_journal_mark(struct bch_fs *, struct list_head *);
-void bch2_journal_entries_free(struct list_head *);
-int bch2_journal_read(struct bch_fs *, struct list_head *);
-int bch2_journal_replay(struct bch_fs *, struct list_head *);
-
static inline void bch2_journal_set_replay_done(struct journal *j)
{
BUG_ON(!test_bit(JOURNAL_STARTED, &j->flags));
}
ssize_t bch2_journal_print_debug(struct journal *, char *);
+ssize_t bch2_journal_print_pins(struct journal *, char *);
+int bch2_set_nr_journal_buckets(struct bch_fs *, struct bch_dev *,
+ unsigned nr);
int bch2_dev_journal_alloc(struct bch_dev *);
-static inline unsigned bch2_nr_journal_buckets(struct bch_sb_field_journal *j)
-{
- return j
- ? (__le64 *) vstruct_end(&j->field) - j->buckets
- : 0;
-}
-
-int bch2_journal_move(struct bch_dev *);
-
+void bch2_dev_journal_stop(struct journal *, struct bch_dev *);
void bch2_fs_journal_stop(struct journal *);
+void bch2_fs_journal_start(struct journal *);
void bch2_dev_journal_exit(struct bch_dev *);
int bch2_dev_journal_init(struct bch_dev *, struct bch_sb *);
void bch2_fs_journal_exit(struct journal *);
int bch2_fs_journal_init(struct journal *);
-#endif /* _BCACHE_JOURNAL_H */
+#endif /* _BCACHEFS_JOURNAL_H */