Commit e5a66496 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Journal reclaim refactoring

Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 2d3b5810
...@@ -84,17 +84,12 @@ void bch2_journal_halt(struct journal *j) ...@@ -84,17 +84,12 @@ void bch2_journal_halt(struct journal *j)
journal_wake(j); journal_wake(j);
closure_wake_up(&journal_cur_buf(j)->wait); closure_wake_up(&journal_cur_buf(j)->wait);
closure_wake_up(&journal_prev_buf(j)->wait);
} }
/* journal entry close/open: */ /* journal entry close/open: */
void __bch2_journal_buf_put(struct journal *j, bool need_write_just_set) void __bch2_journal_buf_put(struct journal *j, bool need_write_just_set)
{ {
struct journal_buf *w = journal_prev_buf(j);
atomic_dec_bug(&journal_seq_pin(j, le64_to_cpu(w->data->seq))->count);
if (!need_write_just_set && if (!need_write_just_set &&
test_bit(JOURNAL_NEED_WRITE, &j->flags)) test_bit(JOURNAL_NEED_WRITE, &j->flags))
bch2_time_stats_update(j->delay_time, bch2_time_stats_update(j->delay_time,
...@@ -175,7 +170,6 @@ static bool __journal_entry_close(struct journal *j) ...@@ -175,7 +170,6 @@ static bool __journal_entry_close(struct journal *j)
* Hence, we want update/set last_seq on the current journal entry right * Hence, we want update/set last_seq on the current journal entry right
* before we open a new one: * before we open a new one:
*/ */
bch2_journal_reclaim_fast(j);
buf->data->last_seq = cpu_to_le64(journal_last_seq(j)); buf->data->last_seq = cpu_to_le64(journal_last_seq(j));
if (journal_entry_empty(buf->data)) if (journal_entry_empty(buf->data))
...@@ -189,8 +183,8 @@ static bool __journal_entry_close(struct journal *j) ...@@ -189,8 +183,8 @@ static bool __journal_entry_close(struct journal *j)
cancel_delayed_work(&j->write_work); cancel_delayed_work(&j->write_work);
/* ugh - might be called from __journal_res_get() under wait_event() */ bch2_journal_space_available(j);
__set_current_state(TASK_RUNNING);
bch2_journal_buf_put(j, old.idx, set_need_write); bch2_journal_buf_put(j, old.idx, set_need_write);
return true; return true;
} }
...@@ -220,7 +214,7 @@ static int journal_entry_open(struct journal *j) ...@@ -220,7 +214,7 @@ static int journal_entry_open(struct journal *j)
{ {
struct journal_buf *buf = journal_cur_buf(j); struct journal_buf *buf = journal_cur_buf(j);
union journal_res_state old, new; union journal_res_state old, new;
int u64s, ret; int u64s;
u64 v; u64 v;
lockdep_assert_held(&j->lock); lockdep_assert_held(&j->lock);
...@@ -229,12 +223,10 @@ static int journal_entry_open(struct journal *j) ...@@ -229,12 +223,10 @@ static int journal_entry_open(struct journal *j)
if (j->blocked) if (j->blocked)
return -EAGAIN; return -EAGAIN;
if (!fifo_free(&j->pin)) if (j->cur_entry_error)
return -ENOSPC; return j->cur_entry_error;
ret = bch2_journal_space_available(j); BUG_ON(!j->cur_entry_sectors);
if (ret)
return ret;
buf->u64s_reserved = j->entry_u64s_reserved; buf->u64s_reserved = j->entry_u64s_reserved;
buf->disk_sectors = j->cur_entry_sectors; buf->disk_sectors = j->cur_entry_sectors;
...@@ -411,7 +403,7 @@ int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res, ...@@ -411,7 +403,7 @@ int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
{ {
int ret; int ret;
wait_event(j->wait, closure_wait_event(&j->async_wait,
(ret = __journal_res_get(j, res, flags)) != -EAGAIN || (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
(flags & JOURNAL_RES_GET_NONBLOCK)); (flags & JOURNAL_RES_GET_NONBLOCK));
return ret; return ret;
...@@ -969,6 +961,7 @@ void bch2_fs_journal_start(struct journal *j) ...@@ -969,6 +961,7 @@ void bch2_fs_journal_start(struct journal *j)
c->last_bucket_seq_cleanup = journal_cur_seq(j); c->last_bucket_seq_cleanup = journal_cur_seq(j);
bch2_journal_space_available(j);
spin_unlock(&j->lock); spin_unlock(&j->lock);
/* /*
...@@ -1144,9 +1137,12 @@ ssize_t bch2_journal_print_debug(struct journal *j, char *buf) ...@@ -1144,9 +1137,12 @@ ssize_t bch2_journal_print_debug(struct journal *j, char *buf)
pr_buf(&out, pr_buf(&out,
"dev %u:\n" "dev %u:\n"
"\tnr\t\t%u\n" "\tnr\t\t%u\n"
"\tavailable\t%u:%u\n"
"\tcur_idx\t\t%u (seq %llu)\n" "\tcur_idx\t\t%u (seq %llu)\n"
"\tlast_idx\t%u (seq %llu)\n", "\tlast_idx\t%u (seq %llu)\n",
iter, ja->nr, iter, ja->nr,
bch2_journal_dev_buckets_available(j, ja),
ja->sectors_free,
ja->cur_idx, ja->bucket_seq[ja->cur_idx], ja->cur_idx, ja->bucket_seq[ja->cur_idx],
ja->last_idx, ja->bucket_seq[ja->last_idx]); ja->last_idx, ja->bucket_seq[ja->last_idx]);
} }
......
...@@ -825,7 +825,6 @@ int bch2_journal_read(struct bch_fs *c, struct list_head *list) ...@@ -825,7 +825,6 @@ int bch2_journal_read(struct bch_fs *c, struct list_head *list)
int bch2_journal_replay(struct bch_fs *c, struct list_head *list) int bch2_journal_replay(struct bch_fs *c, struct list_head *list)
{ {
struct journal *j = &c->journal; struct journal *j = &c->journal;
struct journal_entry_pin_list *pin_list;
struct bkey_i *k, *_n; struct bkey_i *k, *_n;
struct jset_entry *entry; struct jset_entry *entry;
struct journal_replay *i, *n; struct journal_replay *i, *n;
...@@ -867,10 +866,7 @@ int bch2_journal_replay(struct bch_fs *c, struct list_head *list) ...@@ -867,10 +866,7 @@ int bch2_journal_replay(struct bch_fs *c, struct list_head *list)
cond_resched(); cond_resched();
} }
pin_list = journal_seq_pin(j, j->replay_journal_seq); bch2_journal_pin_put(j, j->replay_journal_seq);
if (atomic_dec_and_test(&pin_list->count))
journal_wake(j);
} }
j->replay_journal_seq = 0; j->replay_journal_seq = 0;
...@@ -885,101 +881,6 @@ int bch2_journal_replay(struct bch_fs *c, struct list_head *list) ...@@ -885,101 +881,6 @@ int bch2_journal_replay(struct bch_fs *c, struct list_head *list)
/* journal write: */ /* journal write: */
static unsigned journal_dev_buckets_available(struct journal *j,
struct journal_device *ja)
{
struct bch_fs *c = container_of(j, struct bch_fs, journal);
unsigned next = (ja->cur_idx + 1) % ja->nr;
unsigned available = (ja->last_idx + ja->nr - next) % ja->nr;
/*
* Allocator startup needs some journal space before we can do journal
* replay:
*/
if (available &&
test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags))
available--;
/*
* Don't use the last bucket unless writing the new last_seq
* will make another bucket available:
*/
if (available &&
journal_last_seq(j) <= ja->bucket_seq[ja->last_idx])
--available;
return available;
}
int bch2_journal_space_available(struct journal *j)
{
struct bch_fs *c = container_of(j, struct bch_fs, journal);
struct bch_dev *ca;
unsigned sectors_next_entry = UINT_MAX;
unsigned i, nr_online = 0, nr_devs = 0;
unsigned unwritten_sectors = j->reservations.prev_buf_unwritten
? journal_prev_buf(j)->sectors
: 0;
int ret = 0;
lockdep_assert_held(&j->lock);
rcu_read_lock();
for_each_member_device_rcu(ca, c, i,
&c->rw_devs[BCH_DATA_JOURNAL]) {
struct journal_device *ja = &ca->journal;
unsigned buckets_this_device, sectors_this_device;
if (!ja->nr)
continue;
nr_online++;
buckets_this_device = journal_dev_buckets_available(j, ja);
sectors_this_device = ja->sectors_free;
/*
* We that we don't allocate the space for a journal entry
* until we write it out - thus, account for it here:
*/
if (unwritten_sectors >= sectors_this_device) {
if (!buckets_this_device)
continue;
buckets_this_device--;
sectors_this_device = ca->mi.bucket_size;
}
sectors_this_device -= unwritten_sectors;
if (buckets_this_device)
sectors_this_device = ca->mi.bucket_size;
if (!sectors_this_device)
continue;
sectors_next_entry = min(sectors_next_entry,
sectors_this_device);
nr_devs++;
}
rcu_read_unlock();
if (nr_online < c->opts.metadata_replicas_required) {
ret = -EROFS;
sectors_next_entry = 0;
} else if (!sectors_next_entry ||
nr_devs < min_t(unsigned, nr_online,
c->opts.metadata_replicas)) {
ret = -ENOSPC;
sectors_next_entry = 0;
}
WRITE_ONCE(j->cur_entry_sectors, sectors_next_entry);
return ret;
}
static void __journal_write_alloc(struct journal *j, static void __journal_write_alloc(struct journal *j,
struct journal_buf *w, struct journal_buf *w,
struct dev_alloc_list *devs_sorted, struct dev_alloc_list *devs_sorted,
...@@ -1053,7 +954,6 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w, ...@@ -1053,7 +954,6 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w,
devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe,
&c->rw_devs[BCH_DATA_JOURNAL]); &c->rw_devs[BCH_DATA_JOURNAL]);
spin_lock(&j->lock);
__journal_write_alloc(j, w, &devs_sorted, __journal_write_alloc(j, w, &devs_sorted,
sectors, &replicas, replicas_want); sectors, &replicas, replicas_want);
...@@ -1069,7 +969,7 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w, ...@@ -1069,7 +969,7 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w,
if (sectors > ja->sectors_free && if (sectors > ja->sectors_free &&
sectors <= ca->mi.bucket_size && sectors <= ca->mi.bucket_size &&
journal_dev_buckets_available(j, ja)) { bch2_journal_dev_buckets_available(j, ja)) {
ja->cur_idx = (ja->cur_idx + 1) % ja->nr; ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
ja->sectors_free = ca->mi.bucket_size; ja->sectors_free = ca->mi.bucket_size;
} }
...@@ -1078,7 +978,6 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w, ...@@ -1078,7 +978,6 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w,
__journal_write_alloc(j, w, &devs_sorted, __journal_write_alloc(j, w, &devs_sorted,
sectors, &replicas, replicas_want); sectors, &replicas, replicas_want);
done: done:
spin_unlock(&j->lock);
rcu_read_unlock(); rcu_read_unlock();
return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS; return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
...@@ -1237,6 +1136,9 @@ void bch2_journal_write(struct closure *cl) ...@@ -1237,6 +1136,9 @@ void bch2_journal_write(struct closure *cl)
struct bch_extent_ptr *ptr; struct bch_extent_ptr *ptr;
bool validate_before_checksum = false; bool validate_before_checksum = false;
unsigned i, sectors, bytes, u64s; unsigned i, sectors, bytes, u64s;
int ret;
bch2_journal_pin_put(j, le64_to_cpu(w->data->seq));
journal_buf_realloc(j, w); journal_buf_realloc(j, w);
jset = w->data; jset = w->data;
...@@ -1293,7 +1195,23 @@ void bch2_journal_write(struct closure *cl) ...@@ -1293,7 +1195,23 @@ void bch2_journal_write(struct closure *cl)
bytes = vstruct_bytes(jset); bytes = vstruct_bytes(jset);
memset((void *) jset + bytes, 0, (sectors << 9) - bytes); memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
if (journal_write_alloc(j, w, sectors)) { spin_lock(&j->lock);
ret = journal_write_alloc(j, w, sectors);
/*
* write is allocated, no longer need to account for it in
* bch2_journal_space_available():
*/
w->sectors = 0;
/*
* journal entry has been compacted and allocated, recalculate space
* available:
*/
bch2_journal_space_available(j);
spin_unlock(&j->lock);
if (ret) {
bch2_journal_halt(j); bch2_journal_halt(j);
bch_err(c, "Unable to allocate journal write"); bch_err(c, "Unable to allocate journal write");
bch2_fatal_error(c); bch2_fatal_error(c);
...@@ -1301,12 +1219,6 @@ void bch2_journal_write(struct closure *cl) ...@@ -1301,12 +1219,6 @@ void bch2_journal_write(struct closure *cl)
return; return;
} }
/*
* write is allocated, no longer need to account for it in
* bch2_journal_entry_sectors:
*/
w->sectors = 0;
/* /*
* XXX: we really should just disable the entire journal in nochanges * XXX: we really should just disable the entire journal in nochanges
* mode * mode
......
...@@ -40,7 +40,6 @@ int bch2_journal_read(struct bch_fs *, struct list_head *); ...@@ -40,7 +40,6 @@ int bch2_journal_read(struct bch_fs *, struct list_head *);
void bch2_journal_entries_free(struct list_head *); void bch2_journal_entries_free(struct list_head *);
int bch2_journal_replay(struct bch_fs *, struct list_head *); int bch2_journal_replay(struct bch_fs *, struct list_head *);
int bch2_journal_space_available(struct journal *);
void bch2_journal_write(struct closure *); void bch2_journal_write(struct closure *);
#endif /* _BCACHEFS_JOURNAL_IO_H */ #endif /* _BCACHEFS_JOURNAL_IO_H */
This diff is collapsed.
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
#define JOURNAL_PIN (32 * 1024) #define JOURNAL_PIN (32 * 1024)
unsigned bch2_journal_dev_buckets_available(struct journal *,
struct journal_device *);
void bch2_journal_space_available(struct journal *);
static inline bool journal_pin_active(struct journal_entry_pin *pin) static inline bool journal_pin_active(struct journal_entry_pin *pin)
{ {
return pin->seq != 0; return pin->seq != 0;
...@@ -17,6 +21,8 @@ journal_seq_pin(struct journal *j, u64 seq) ...@@ -17,6 +21,8 @@ journal_seq_pin(struct journal *j, u64 seq)
return &j->pin.data[seq & j->pin.mask]; return &j->pin.data[seq & j->pin.mask];
} }
void bch2_journal_pin_put(struct journal *, u64);
void bch2_journal_pin_add(struct journal *, u64, struct journal_entry_pin *, void bch2_journal_pin_add(struct journal *, u64, struct journal_entry_pin *,
journal_pin_flush_fn); journal_pin_flush_fn);
void bch2_journal_pin_update(struct journal *, u64, struct journal_entry_pin *, void bch2_journal_pin_update(struct journal *, u64, struct journal_entry_pin *,
...@@ -28,7 +34,6 @@ void bch2_journal_pin_add_if_older(struct journal *, ...@@ -28,7 +34,6 @@ void bch2_journal_pin_add_if_older(struct journal *,
journal_pin_flush_fn); journal_pin_flush_fn);
void bch2_journal_pin_flush(struct journal *, struct journal_entry_pin *); void bch2_journal_pin_flush(struct journal *, struct journal_entry_pin *);
void bch2_journal_reclaim_fast(struct journal *);
void bch2_journal_reclaim_work(struct work_struct *); void bch2_journal_reclaim_work(struct work_struct *);
void bch2_journal_flush_pins(struct journal *, u64); void bch2_journal_flush_pins(struct journal *, u64);
......
...@@ -136,6 +136,12 @@ struct journal { ...@@ -136,6 +136,12 @@ struct journal {
unsigned cur_entry_u64s; unsigned cur_entry_u64s;
unsigned cur_entry_sectors; unsigned cur_entry_sectors;
/*
* 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if
* insufficient devices:
*/
int cur_entry_error;
/* Reserved space in journal entry to be used just prior to write */ /* Reserved space in journal entry to be used just prior to write */
unsigned entry_u64s_reserved; unsigned entry_u64s_reserved;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment