/* update path: */
struct btree_trans_commit_hook *hooks;
- DARRAY(u64) extra_journal_entries;
+ darray_u64 extra_journal_entries;
struct journal_entry_pin *journal_pin;
struct journal_res journal_res;
struct btree_trans_commit_hook *);
int __bch2_trans_commit(struct btree_trans *);
-int bch2_trans_log_msg(struct btree_trans *, const char *);
+int bch2_trans_log_msg(struct btree_trans *, const char *, ...);
+int bch2_fs_log_msg(struct bch_fs *, const char *, ...);
/**
* bch2_trans_commit - insert keys at given iterator positions
return ret;
}
-int bch2_trans_log_msg(struct btree_trans *trans, const char *msg)
+static int __bch2_trans_log_msg(darray_u64 *entries, const char *fmt, va_list args)
{
- unsigned len = strlen(msg);
- unsigned u64s = DIV_ROUND_UP(len, sizeof(u64));
+ struct printbuf buf = PRINTBUF;
struct jset_entry_log *l;
+ unsigned u64s;
int ret;
- ret = darray_make_room(&trans->extra_journal_entries, jset_u64s(u64s));
+ prt_vprintf(&buf, fmt, args);
+ ret = buf.allocation_failure ? -ENOMEM : 0;
if (ret)
- return ret;
+ goto err;
+
+ u64s = DIV_ROUND_UP(buf.pos, sizeof(u64));
+
+ ret = darray_make_room(entries, jset_u64s(u64s));
+ if (ret)
+ goto err;
- l = (void *) &darray_top(trans->extra_journal_entries);
+ l = (void *) &darray_top(*entries);
l->entry.u64s = cpu_to_le16(u64s);
l->entry.btree_id = 0;
l->entry.level = 1;
l->entry.pad[0] = 0;
l->entry.pad[1] = 0;
l->entry.pad[2] = 0;
- memcpy(l->d, msg, len);
- while (len & 7)
- l->d[len++] = '\0';
+ memcpy(l->d, buf.buf, buf.pos);
+ while (buf.pos & 7)
+ l->d[buf.pos++] = '\0';
+
+ entries->nr += jset_u64s(u64s);
+err:
+ printbuf_exit(&buf);
+ return ret;
+}
+
+int bch2_trans_log_msg(struct btree_trans *trans, const char *fmt, ...)
+{
+ va_list args;
+ int ret;
+
+ va_start(args, fmt);
+ ret = __bch2_trans_log_msg(&trans->extra_journal_entries, fmt, args);
+ va_end(args);
+
+ return ret;
+}
+
+int bch2_fs_log_msg(struct bch_fs *c, const char *fmt, ...)
+{
+ va_list args;
+ int ret;
+
+ va_start(args, fmt);
+
+ if (!test_bit(JOURNAL_STARTED, &c->journal.flags)) {
+ ret = __bch2_trans_log_msg(&c->journal.early_journal_entries, fmt, args);
+ } else {
+ ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_LAZY_RW,
+ __bch2_trans_log_msg(&trans.extra_journal_entries, fmt, args));
+ }
+
+ va_end(args);
+
+ return ret;
- trans->extra_journal_entries.nr += jset_u64s(u64s);
- return 0;
}
journal_entry_overhead(j);
u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
- if (u64s <= 0)
+ if (u64s <= (ssize_t) j->early_journal_entries.nr)
return JOURNAL_ERR_journal_full;
if (fifo_empty(&j->pin) && j->reclaim_thread)
buf->data->seq = cpu_to_le64(journal_cur_seq(j));
buf->data->u64s = 0;
+ if (j->early_journal_entries.nr) {
+ memcpy(buf->data->_data, j->early_journal_entries.data,
+ j->early_journal_entries.nr * sizeof(u64));
+ le32_add_cpu(&buf->data->u64s, j->early_journal_entries.nr);
+ }
+
/*
* Must be set before marking the journal entry as open:
*/
BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
journal_state_inc(&new);
- new.cur_entry_offset = 0;
+
+ /* Handle any already added entries */
+ new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
} while ((v = atomic64_cmpxchg(&j->reservations.counter,
old.v, new.v)) != old.v);
&j->write_work,
msecs_to_jiffies(c->opts.journal_flush_delay));
journal_wake(j);
+
+ if (j->early_journal_entries.nr)
+ darray_exit(&j->early_journal_entries);
return 0;
}
return bch2_journal_flush_seq(j, res.seq);
}
-int bch2_journal_log_msg(struct journal *j, const char *fmt, ...)
-{
- struct jset_entry_log *entry;
- struct journal_res res = { 0 };
- unsigned msglen, u64s;
- va_list args;
- int ret;
-
- va_start(args, fmt);
- msglen = vsnprintf(NULL, 0, fmt, args) + 1;
- va_end(args);
-
- u64s = jset_u64s(DIV_ROUND_UP(msglen, sizeof(u64)));
-
- ret = bch2_journal_res_get(j, &res, u64s, 0);
- if (ret)
- return ret;
-
- entry = container_of(journal_res_entry(j, &res),
- struct jset_entry_log, entry);
- memset(entry, 0, u64s * sizeof(u64));
- entry->entry.type = BCH_JSET_ENTRY_log;
- entry->entry.u64s = u64s - 1;
-
- va_start(args, fmt);
- vsnprintf(entry->d, INT_MAX, fmt, args);
- va_end(args);
-
- bch2_journal_res_put(j, &res);
-
- return bch2_journal_flush_seq(j, res.seq);
-}
-
/* block/unlock the journal: */
void bch2_journal_unblock(struct journal *j)
{
unsigned i;
+ darray_exit(&j->early_journal_entries);
+
for (i = 0; i < ARRAY_SIZE(j->buf); i++)
kvpfree(j->buf[i].data, j->buf[i].buf_size);
free_fifo(&j->pin);
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 *);
}
}
-int bch2_journal_read(struct bch_fs *c, u64 *blacklist_seq, u64 *start_seq)
+int bch2_journal_read(struct bch_fs *c,
+ u64 *last_seq,
+ u64 *blacklist_seq,
+ u64 *start_seq)
{
struct journal_list jlist;
struct journal_replay *i, **_i, *prev = NULL;
unsigned iter;
struct printbuf buf = PRINTBUF;
bool degraded = false, last_write_torn = false;
- u64 seq, last_seq = 0;
+ u64 seq;
int ret = 0;
closure_init_stack(&jlist.cl);
if (jlist.ret)
return jlist.ret;
+ *last_seq = 0;
*start_seq = 0;
*blacklist_seq = 0;
le64_to_cpu(i->j.seq)))
i->j.last_seq = i->j.seq;
- last_seq = le64_to_cpu(i->j.last_seq);
+ *last_seq = le64_to_cpu(i->j.last_seq);
*blacklist_seq = le64_to_cpu(i->j.seq) + 1;
break;
}
return 0;
}
- if (!last_seq) {
+ if (!*last_seq) {
fsck_err(c, "journal read done, but no entries found after dropping non-flushes");
return 0;
}
bch_info(c, "journal read done, replaying entries %llu-%llu",
- last_seq, *blacklist_seq - 1);
+ *last_seq, *blacklist_seq - 1);
if (*start_seq != *blacklist_seq)
bch_info(c, "dropped unflushed entries %llu-%llu",
continue;
seq = le64_to_cpu(i->j.seq);
- if (seq < last_seq) {
+ if (seq < *last_seq) {
journal_replay_free(c, i);
continue;
}
}
/* Check for missing entries: */
- seq = last_seq;
+ seq = *last_seq;
genradix_for_each(&c->journal_entries, radix_iter, _i) {
i = *_i;
" prev at %s\n"
" next at %s",
missing_start, missing_end,
- last_seq, *blacklist_seq - 1,
+ *last_seq, *blacklist_seq - 1,
buf1.buf, buf2.buf);
printbuf_exit(&buf1);
void bch2_journal_ptrs_to_text(struct printbuf *, struct bch_fs *,
struct journal_replay *);
-int bch2_journal_read(struct bch_fs *, u64 *, u64 *);
+int bch2_journal_read(struct bch_fs *, u64 *, u64 *, u64 *);
void bch2_journal_write(struct closure *);
#undef x
};
+typedef DARRAY(u64) darray_u64;
+
/* Embedded in struct bch_fs */
struct journal {
/* Fastpath stuff up front: */
enum journal_errors cur_entry_error;
unsigned buf_size_want;
+ /*
+ * We may queue up some things to be journalled (log messages) before
+ * the journal has actually started - stash them here:
+ */
+ darray_u64 early_journal_entries;
+
/*
* Two journal entries -- one is currently open for new entries, the
* other is possibly being written out.
return cmp_int(l->journal_seq, r->journal_seq);
}
-static int bch2_journal_replay(struct bch_fs *c)
+static int bch2_journal_replay(struct bch_fs *c, u64 start_seq, u64 end_seq)
{
struct journal_keys *keys = &c->journal_keys;
struct journal_key **keys_sorted, *k;
sizeof(keys_sorted[0]),
journal_sort_seq_cmp, NULL);
+ if (keys->nr) {
+ ret = bch2_fs_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)",
+ keys->nr, start_seq, end_seq);
+ if (ret)
+ goto err;
+ }
+
for (i = 0; i < keys->nr; i++) {
k = keys_sorted[i];
ret = bch2_journal_error(j);
if (keys->nr && !ret)
- bch2_journal_log_msg(&c->journal, "journal replay finished");
+ bch2_fs_log_msg(c, "journal replay finished");
err:
kvfree(keys_sorted);
return ret;
const char *err = "cannot allocate memory";
struct bch_sb_field_clean *clean = NULL;
struct jset *last_journal_entry = NULL;
- u64 blacklist_seq, journal_seq;
+ u64 last_seq, blacklist_seq, journal_seq;
bool write_sb = false;
int ret = 0;
struct journal_replay **i;
bch_verbose(c, "starting journal read");
- ret = bch2_journal_read(c, &blacklist_seq, &journal_seq);
+ ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq);
if (ret)
goto err;
journal_seq += 8;
if (blacklist_seq != journal_seq) {
- ret = bch2_journal_seq_blacklist_add(c,
+ ret = bch2_fs_log_msg(c, "blacklisting entries %llu-%llu",
+ blacklist_seq, journal_seq) ?:
+ bch2_journal_seq_blacklist_add(c,
blacklist_seq, journal_seq);
if (ret) {
bch_err(c, "error creating new journal seq blacklist entry");
}
}
- ret = bch2_fs_journal_start(&c->journal, journal_seq);
+ ret = bch2_fs_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu",
+ journal_seq, last_seq, blacklist_seq - 1) ?:
+ bch2_fs_journal_start(&c->journal, journal_seq);
if (ret)
goto err;
if (c->opts.reconstruct_alloc)
- bch2_journal_log_msg(&c->journal, "dropping alloc info");
+ bch2_fs_log_msg(c, "dropping alloc info");
/*
* Skip past versions that might have possibly been used (as nonces),
bch_info(c, "starting journal replay, %zu keys", c->journal_keys.nr);
err = "journal replay failed";
- ret = bch2_journal_replay(c);
+ ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
if (ret)
goto err;
if (c->opts.verbose || !c->sb.clean)
bch_verbose(c, "starting journal replay, %zu keys", c->journal_keys.nr);
err = "journal replay failed";
- ret = bch2_journal_replay(c);
+ ret = bch2_journal_replay(c, last_seq, blacklist_seq - 1);
if (ret)
goto err;
if (c->opts.verbose || !c->sb.clean)