2023-08-05 16:08:44 -04:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2024-01-22 14:25:00 -05:00
|
|
|
#include "bkey_buf.h"
|
2023-08-05 16:08:44 -04:00
|
|
|
#include "bset.h"
|
2024-01-22 14:25:00 -05:00
|
|
|
#include "btree_cache.h"
|
2023-08-05 16:08:44 -04:00
|
|
|
#include "btree_journal_iter.h"
|
|
|
|
#include "journal_io.h"
|
|
|
|
|
|
|
|
#include <linux/sort.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For managing keys we read from the journal: until journal replay works normal
|
|
|
|
* btree lookups need to be able to find and return keys from the journal where
|
|
|
|
* they overwrite what's in the btree, so we have a special iterator and
|
|
|
|
* operations for the regular btree iter code to use:
|
|
|
|
*/
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
static inline size_t pos_to_idx(struct journal_keys *keys, size_t pos)
|
|
|
|
{
|
|
|
|
size_t gap_size = keys->size - keys->nr;
|
|
|
|
|
|
|
|
BUG_ON(pos >= keys->gap && pos < keys->gap + gap_size);
|
|
|
|
|
|
|
|
if (pos >= keys->gap)
|
|
|
|
pos -= gap_size;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
static inline size_t idx_to_pos(struct journal_keys *keys, size_t idx)
|
|
|
|
{
|
|
|
|
size_t gap_size = keys->size - keys->nr;
|
|
|
|
|
|
|
|
if (idx >= keys->gap)
|
|
|
|
idx += gap_size;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct journal_key *idx_to_key(struct journal_keys *keys, size_t idx)
|
|
|
|
{
|
2024-02-24 00:15:56 -05:00
|
|
|
return keys->data + idx_to_pos(keys, idx);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t __bch2_journal_key_search(struct journal_keys *keys,
|
|
|
|
enum btree_id id, unsigned level,
|
|
|
|
struct bpos pos)
|
|
|
|
{
|
|
|
|
size_t l = 0, r = keys->nr, m;
|
|
|
|
|
|
|
|
while (l < r) {
|
|
|
|
m = l + ((r - l) >> 1);
|
|
|
|
if (__journal_key_cmp(id, level, pos, idx_to_key(keys, m)) > 0)
|
|
|
|
l = m + 1;
|
|
|
|
else
|
|
|
|
r = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG_ON(l < keys->nr &&
|
|
|
|
__journal_key_cmp(id, level, pos, idx_to_key(keys, l)) > 0);
|
|
|
|
|
|
|
|
BUG_ON(l &&
|
|
|
|
__journal_key_cmp(id, level, pos, idx_to_key(keys, l - 1)) <= 0);
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t bch2_journal_key_search(struct journal_keys *keys,
|
|
|
|
enum btree_id id, unsigned level,
|
|
|
|
struct bpos pos)
|
|
|
|
{
|
|
|
|
return idx_to_pos(keys, __bch2_journal_key_search(keys, id, level, pos));
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:38:09 -05:00
|
|
|
/* Returns first non-overwritten key >= search key: */
|
2024-10-24 18:39:59 -04:00
|
|
|
struct bkey_i *bch2_journal_keys_peek_max(struct bch_fs *c, enum btree_id btree_id,
|
2023-08-05 16:08:44 -04:00
|
|
|
unsigned level, struct bpos pos,
|
|
|
|
struct bpos end_pos, size_t *idx)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
unsigned iters = 0;
|
|
|
|
struct journal_key *k;
|
2023-11-17 23:13:49 -05:00
|
|
|
|
|
|
|
BUG_ON(*idx > keys->nr);
|
2023-08-05 16:08:44 -04:00
|
|
|
search:
|
|
|
|
if (!*idx)
|
|
|
|
*idx = __bch2_journal_key_search(keys, btree_id, level, pos);
|
|
|
|
|
2023-11-17 18:38:09 -05:00
|
|
|
while (*idx &&
|
|
|
|
__journal_key_cmp(btree_id, level, end_pos, idx_to_key(keys, *idx - 1)) <= 0) {
|
|
|
|
--(*idx);
|
|
|
|
iters++;
|
|
|
|
if (iters == 10) {
|
|
|
|
*idx = 0;
|
|
|
|
goto search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
struct bkey_i *ret = NULL;
|
|
|
|
rcu_read_lock(); /* for overwritten_ranges */
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
while ((k = *idx < keys->nr ? idx_to_key(keys, *idx) : NULL)) {
|
|
|
|
if (__journal_key_cmp(btree_id, level, end_pos, k) < 0)
|
2024-11-17 02:23:24 -05:00
|
|
|
break;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2023-11-17 18:38:09 -05:00
|
|
|
if (k->overwritten) {
|
2024-11-17 02:23:24 -05:00
|
|
|
if (k->overwritten_range)
|
|
|
|
*idx = rcu_dereference(k->overwritten_range)->end;
|
|
|
|
else
|
|
|
|
*idx += 1;
|
2023-11-17 18:38:09 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
if (__journal_key_cmp(btree_id, level, pos, k) <= 0) {
|
|
|
|
ret = k->k;
|
|
|
|
break;
|
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
(*idx)++;
|
|
|
|
iters++;
|
|
|
|
if (iters == 10) {
|
|
|
|
*idx = 0;
|
2024-11-17 02:23:24 -05:00
|
|
|
rcu_read_unlock();
|
2023-08-05 16:08:44 -04:00
|
|
|
goto search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
rcu_read_unlock();
|
|
|
|
return ret;
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2024-10-24 22:12:37 -04:00
|
|
|
struct bkey_i *bch2_journal_keys_peek_prev_min(struct bch_fs *c, enum btree_id btree_id,
|
|
|
|
unsigned level, struct bpos pos,
|
|
|
|
struct bpos end_pos, size_t *idx)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
unsigned iters = 0;
|
|
|
|
struct journal_key *k;
|
|
|
|
|
|
|
|
BUG_ON(*idx > keys->nr);
|
2025-06-24 16:04:32 -04:00
|
|
|
|
|
|
|
if (!keys->nr)
|
|
|
|
return NULL;
|
2024-10-24 22:12:37 -04:00
|
|
|
search:
|
|
|
|
if (!*idx)
|
|
|
|
*idx = __bch2_journal_key_search(keys, btree_id, level, pos);
|
|
|
|
|
2025-06-13 14:53:42 -04:00
|
|
|
while (*idx < keys->nr &&
|
2025-06-24 16:04:32 -04:00
|
|
|
__journal_key_cmp(btree_id, level, end_pos, idx_to_key(keys, *idx)) >= 0) {
|
2024-10-24 22:12:37 -04:00
|
|
|
(*idx)++;
|
|
|
|
iters++;
|
|
|
|
if (iters == 10) {
|
|
|
|
*idx = 0;
|
|
|
|
goto search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-24 16:04:32 -04:00
|
|
|
if (*idx == keys->nr)
|
|
|
|
--(*idx);
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
struct bkey_i *ret = NULL;
|
|
|
|
rcu_read_lock(); /* for overwritten_ranges */
|
|
|
|
|
2025-06-24 16:04:32 -04:00
|
|
|
while (true) {
|
|
|
|
k = idx_to_key(keys, *idx);
|
2024-10-24 22:12:37 -04:00
|
|
|
if (__journal_key_cmp(btree_id, level, end_pos, k) > 0)
|
2024-11-17 02:23:24 -05:00
|
|
|
break;
|
2024-10-24 22:12:37 -04:00
|
|
|
|
|
|
|
if (k->overwritten) {
|
2024-11-17 02:23:24 -05:00
|
|
|
if (k->overwritten_range)
|
2025-06-24 16:04:32 -04:00
|
|
|
*idx = rcu_dereference(k->overwritten_range)->start;
|
|
|
|
if (!*idx)
|
|
|
|
break;
|
|
|
|
--(*idx);
|
2024-10-24 22:12:37 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
if (__journal_key_cmp(btree_id, level, pos, k) >= 0) {
|
|
|
|
ret = k->k;
|
|
|
|
break;
|
|
|
|
}
|
2024-10-24 22:12:37 -04:00
|
|
|
|
2025-06-24 16:04:32 -04:00
|
|
|
if (!*idx)
|
|
|
|
break;
|
2024-10-24 22:12:37 -04:00
|
|
|
--(*idx);
|
|
|
|
iters++;
|
|
|
|
if (iters == 10) {
|
|
|
|
*idx = 0;
|
|
|
|
goto search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
rcu_read_unlock();
|
|
|
|
return ret;
|
2024-10-24 22:12:37 -04:00
|
|
|
}
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
struct bkey_i *bch2_journal_keys_peek_slot(struct bch_fs *c, enum btree_id btree_id,
|
|
|
|
unsigned level, struct bpos pos)
|
|
|
|
{
|
|
|
|
size_t idx = 0;
|
|
|
|
|
2024-10-24 18:39:59 -04:00
|
|
|
return bch2_journal_keys_peek_max(c, btree_id, level, pos, pos, &idx);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:45:46 -04:00
|
|
|
static void journal_iter_verify(struct journal_iter *iter)
|
|
|
|
{
|
2024-11-17 02:23:24 -05:00
|
|
|
#ifdef CONFIG_BCACHEFS_DEBUG
|
2024-04-06 21:45:46 -04:00
|
|
|
struct journal_keys *keys = iter->keys;
|
|
|
|
size_t gap_size = keys->size - keys->nr;
|
|
|
|
|
|
|
|
BUG_ON(iter->idx >= keys->gap &&
|
|
|
|
iter->idx < keys->gap + gap_size);
|
|
|
|
|
|
|
|
if (iter->idx < keys->size) {
|
|
|
|
struct journal_key *k = keys->data + iter->idx;
|
|
|
|
|
2024-11-17 14:20:35 -05:00
|
|
|
int cmp = __journal_key_btree_cmp(iter->btree_id, iter->level, k);
|
|
|
|
BUG_ON(cmp > 0);
|
2024-04-06 21:45:46 -04:00
|
|
|
}
|
2024-11-17 02:23:24 -05:00
|
|
|
#endif
|
2024-04-06 21:45:46 -04:00
|
|
|
}
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
static void journal_iters_fix(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
/* The key we just inserted is immediately before the gap: */
|
|
|
|
size_t gap_end = keys->gap + (keys->size - keys->nr);
|
2024-04-06 21:45:46 -04:00
|
|
|
struct journal_key *new_key = &keys->data[keys->gap - 1];
|
|
|
|
struct journal_iter *iter;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If an iterator points one after the key we just inserted, decrement
|
|
|
|
* the iterator so it points at the key we just inserted - if the
|
|
|
|
* decrement was unnecessary, bch2_btree_and_journal_iter_peek() will
|
|
|
|
* handle that:
|
|
|
|
*/
|
2024-04-06 21:45:46 -04:00
|
|
|
list_for_each_entry(iter, &c->journal_iters, list) {
|
|
|
|
journal_iter_verify(iter);
|
|
|
|
if (iter->idx == gap_end &&
|
|
|
|
new_key->btree_id == iter->btree_id &&
|
|
|
|
new_key->level == iter->level)
|
|
|
|
iter->idx = keys->gap - 1;
|
|
|
|
journal_iter_verify(iter);
|
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void journal_iters_move_gap(struct bch_fs *c, size_t old_gap, size_t new_gap)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
struct journal_iter *iter;
|
|
|
|
size_t gap_size = keys->size - keys->nr;
|
|
|
|
|
|
|
|
list_for_each_entry(iter, &c->journal_iters, list) {
|
|
|
|
if (iter->idx > old_gap)
|
|
|
|
iter->idx -= gap_size;
|
|
|
|
if (iter->idx >= new_gap)
|
|
|
|
iter->idx += gap_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_journal_key_insert_take(struct bch_fs *c, enum btree_id id,
|
|
|
|
unsigned level, struct bkey_i *k)
|
|
|
|
{
|
|
|
|
struct journal_key n = {
|
|
|
|
.btree_id = id,
|
|
|
|
.level = level,
|
|
|
|
.k = k,
|
|
|
|
.allocated = true,
|
|
|
|
/*
|
|
|
|
* Ensure these keys are done last by journal replay, to unblock
|
|
|
|
* journal reclaim:
|
|
|
|
*/
|
2024-11-30 23:27:45 -05:00
|
|
|
.journal_seq = U64_MAX,
|
2023-08-05 16:08:44 -04:00
|
|
|
};
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
size_t idx = bch2_journal_key_search(keys, id, level, k->k.p);
|
|
|
|
|
2023-11-26 17:05:02 -05:00
|
|
|
BUG_ON(test_bit(BCH_FS_rw, &c->flags));
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
if (idx < keys->size &&
|
2024-02-24 00:15:56 -05:00
|
|
|
journal_key_cmp(&n, &keys->data[idx]) == 0) {
|
|
|
|
if (keys->data[idx].allocated)
|
|
|
|
kfree(keys->data[idx].k);
|
|
|
|
keys->data[idx] = n;
|
2023-08-05 16:08:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx > keys->gap)
|
|
|
|
idx -= keys->size - keys->nr;
|
|
|
|
|
2024-04-06 21:45:46 -04:00
|
|
|
size_t old_gap = keys->gap;
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
if (keys->nr == keys->size) {
|
2024-04-06 21:45:46 -04:00
|
|
|
journal_iters_move_gap(c, old_gap, keys->size);
|
|
|
|
old_gap = keys->size;
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
struct journal_keys new_keys = {
|
|
|
|
.nr = keys->nr,
|
|
|
|
.size = max_t(size_t, keys->size, 8) * 2,
|
|
|
|
};
|
|
|
|
|
2025-04-28 20:28:58 -04:00
|
|
|
new_keys.data = bch2_kvmalloc(new_keys.size * sizeof(new_keys.data[0]), GFP_KERNEL);
|
2024-02-24 00:15:56 -05:00
|
|
|
if (!new_keys.data) {
|
2023-08-05 16:08:44 -04:00
|
|
|
bch_err(c, "%s: error allocating new key array (size %zu)",
|
|
|
|
__func__, new_keys.size);
|
2025-05-28 11:57:50 -04:00
|
|
|
return bch_err_throw(c, ENOMEM_journal_key_insert);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Since @keys was full, there was no gap: */
|
2024-02-24 00:15:56 -05:00
|
|
|
memcpy(new_keys.data, keys->data, sizeof(keys->data[0]) * keys->nr);
|
|
|
|
kvfree(keys->data);
|
|
|
|
keys->data = new_keys.data;
|
2023-11-17 23:13:49 -05:00
|
|
|
keys->nr = new_keys.nr;
|
|
|
|
keys->size = new_keys.size;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
/* And now the gap is at the end: */
|
2023-11-17 23:13:49 -05:00
|
|
|
keys->gap = keys->nr;
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2024-04-06 21:45:46 -04:00
|
|
|
journal_iters_move_gap(c, old_gap, idx);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-02-23 22:43:24 -05:00
|
|
|
move_gap(keys, idx);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
keys->nr++;
|
2024-02-24 00:15:56 -05:00
|
|
|
keys->data[keys->gap++] = n;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
journal_iters_fix(c);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can only be used from the recovery thread while we're still RO - can't be
|
|
|
|
* used once we've got RW, as journal_keys is at that point used by multiple
|
|
|
|
* threads:
|
|
|
|
*/
|
|
|
|
int bch2_journal_key_insert(struct bch_fs *c, enum btree_id id,
|
|
|
|
unsigned level, struct bkey_i *k)
|
|
|
|
{
|
|
|
|
struct bkey_i *n;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
n = kmalloc(bkey_bytes(&k->k), GFP_KERNEL);
|
|
|
|
if (!n)
|
2025-05-28 11:57:50 -04:00
|
|
|
return bch_err_throw(c, ENOMEM_journal_key_insert);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
bkey_copy(n, k);
|
|
|
|
ret = bch2_journal_key_insert_take(c, id, level, n);
|
|
|
|
if (ret)
|
|
|
|
kfree(n);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_journal_key_delete(struct bch_fs *c, enum btree_id id,
|
|
|
|
unsigned level, struct bpos pos)
|
|
|
|
{
|
|
|
|
struct bkey_i whiteout;
|
|
|
|
|
|
|
|
bkey_init(&whiteout.k);
|
|
|
|
whiteout.k.p = pos;
|
|
|
|
|
|
|
|
return bch2_journal_key_insert(c, id, level, &whiteout);
|
|
|
|
}
|
|
|
|
|
2024-03-26 17:14:43 -04:00
|
|
|
bool bch2_key_deleted_in_journal(struct btree_trans *trans, enum btree_id btree,
|
|
|
|
unsigned level, struct bpos pos)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &trans->c->journal_keys;
|
|
|
|
size_t idx = bch2_journal_key_search(keys, btree, level, pos);
|
|
|
|
|
|
|
|
if (!trans->journal_replay_not_finished)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (idx < keys->size &&
|
|
|
|
keys->data[idx].btree_id == btree &&
|
|
|
|
keys->data[idx].level == level &&
|
|
|
|
bpos_eq(keys->data[idx].k->k.p, pos) &&
|
|
|
|
bkey_deleted(&keys->data[idx].k->k));
|
|
|
|
}
|
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
static void __bch2_journal_key_overwritten(struct journal_keys *keys, size_t pos)
|
|
|
|
{
|
|
|
|
struct journal_key *k = keys->data + pos;
|
|
|
|
size_t idx = pos_to_idx(keys, pos);
|
|
|
|
|
|
|
|
k->overwritten = true;
|
|
|
|
|
|
|
|
struct journal_key *prev = idx > 0 ? keys->data + idx_to_pos(keys, idx - 1) : NULL;
|
|
|
|
struct journal_key *next = idx + 1 < keys->nr ? keys->data + idx_to_pos(keys, idx + 1) : NULL;
|
|
|
|
|
|
|
|
bool prev_overwritten = prev && prev->overwritten;
|
|
|
|
bool next_overwritten = next && next->overwritten;
|
|
|
|
|
|
|
|
struct journal_key_range_overwritten *prev_range =
|
|
|
|
prev_overwritten ? prev->overwritten_range : NULL;
|
|
|
|
struct journal_key_range_overwritten *next_range =
|
|
|
|
next_overwritten ? next->overwritten_range : NULL;
|
|
|
|
|
|
|
|
BUG_ON(prev_range && prev_range->end != idx);
|
|
|
|
BUG_ON(next_range && next_range->start != idx + 1);
|
|
|
|
|
|
|
|
if (prev_range && next_range) {
|
|
|
|
prev_range->end = next_range->end;
|
|
|
|
|
|
|
|
keys->data[pos].overwritten_range = prev_range;
|
|
|
|
for (size_t i = next_range->start; i < next_range->end; i++) {
|
|
|
|
struct journal_key *ip = keys->data + idx_to_pos(keys, i);
|
|
|
|
BUG_ON(ip->overwritten_range != next_range);
|
|
|
|
ip->overwritten_range = prev_range;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree_rcu_mightsleep(next_range);
|
|
|
|
} else if (prev_range) {
|
|
|
|
prev_range->end++;
|
|
|
|
k->overwritten_range = prev_range;
|
|
|
|
if (next_overwritten) {
|
|
|
|
prev_range->end++;
|
|
|
|
next->overwritten_range = prev_range;
|
|
|
|
}
|
|
|
|
} else if (next_range) {
|
|
|
|
next_range->start--;
|
|
|
|
k->overwritten_range = next_range;
|
|
|
|
if (prev_overwritten) {
|
|
|
|
next_range->start--;
|
|
|
|
prev->overwritten_range = next_range;
|
|
|
|
}
|
|
|
|
} else if (prev_overwritten || next_overwritten) {
|
|
|
|
struct journal_key_range_overwritten *r = kmalloc(sizeof(*r), GFP_KERNEL);
|
|
|
|
if (!r)
|
|
|
|
return;
|
|
|
|
|
|
|
|
r->start = idx - (size_t) prev_overwritten;
|
|
|
|
r->end = idx + 1 + (size_t) next_overwritten;
|
|
|
|
|
|
|
|
rcu_assign_pointer(k->overwritten_range, r);
|
|
|
|
if (prev_overwritten)
|
|
|
|
prev->overwritten_range = r;
|
|
|
|
if (next_overwritten)
|
|
|
|
next->overwritten_range = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
void bch2_journal_key_overwritten(struct bch_fs *c, enum btree_id btree,
|
|
|
|
unsigned level, struct bpos pos)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
size_t idx = bch2_journal_key_search(keys, btree, level, pos);
|
|
|
|
|
|
|
|
if (idx < keys->size &&
|
2024-02-24 00:15:56 -05:00
|
|
|
keys->data[idx].btree_id == btree &&
|
|
|
|
keys->data[idx].level == level &&
|
2024-11-17 02:23:24 -05:00
|
|
|
bpos_eq(keys->data[idx].k->k.p, pos) &&
|
|
|
|
!keys->data[idx].overwritten) {
|
|
|
|
mutex_lock(&keys->overwrite_lock);
|
|
|
|
__bch2_journal_key_overwritten(keys, idx);
|
|
|
|
mutex_unlock(&keys->overwrite_lock);
|
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_journal_iter_advance(struct journal_iter *iter)
|
|
|
|
{
|
|
|
|
if (iter->idx < iter->keys->size) {
|
|
|
|
iter->idx++;
|
|
|
|
if (iter->idx == iter->keys->gap)
|
|
|
|
iter->idx += iter->keys->size - iter->keys->nr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bkey_s_c bch2_journal_iter_peek(struct journal_iter *iter)
|
|
|
|
{
|
2024-04-06 21:45:46 -04:00
|
|
|
journal_iter_verify(iter);
|
|
|
|
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2024-04-06 21:45:46 -04:00
|
|
|
while (iter->idx < iter->keys->size) {
|
|
|
|
struct journal_key *k = iter->keys->data + iter->idx;
|
|
|
|
|
2024-11-17 14:20:35 -05:00
|
|
|
int cmp = __journal_key_btree_cmp(iter->btree_id, iter->level, k);
|
|
|
|
if (cmp < 0)
|
2024-04-06 21:45:46 -04:00
|
|
|
break;
|
|
|
|
BUG_ON(cmp);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2025-05-24 16:33:39 -04:00
|
|
|
if (!k->overwritten)
|
|
|
|
return bkey_i_to_s_c(k->k);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
if (k->overwritten_range)
|
|
|
|
iter->idx = idx_to_pos(iter->keys, rcu_dereference(k->overwritten_range)->end);
|
|
|
|
else
|
|
|
|
bch2_journal_iter_advance(iter);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2025-05-24 16:33:39 -04:00
|
|
|
return bkey_s_c_null;
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_journal_iter_exit(struct journal_iter *iter)
|
|
|
|
{
|
|
|
|
list_del(&iter->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_journal_iter_init(struct bch_fs *c,
|
|
|
|
struct journal_iter *iter,
|
|
|
|
enum btree_id id, unsigned level,
|
|
|
|
struct bpos pos)
|
|
|
|
{
|
|
|
|
iter->btree_id = id;
|
|
|
|
iter->level = level;
|
|
|
|
iter->keys = &c->journal_keys;
|
|
|
|
iter->idx = bch2_journal_key_search(&c->journal_keys, id, level, pos);
|
2024-04-06 21:45:46 -04:00
|
|
|
|
|
|
|
journal_iter_verify(iter);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct bkey_s_c bch2_journal_iter_peek_btree(struct btree_and_journal_iter *iter)
|
|
|
|
{
|
|
|
|
return bch2_btree_node_iter_peek_unpack(&iter->node_iter,
|
|
|
|
iter->b, &iter->unpacked);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bch2_journal_iter_advance_btree(struct btree_and_journal_iter *iter)
|
|
|
|
{
|
|
|
|
bch2_btree_node_iter_advance(&iter->node_iter, iter->b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_btree_and_journal_iter_advance(struct btree_and_journal_iter *iter)
|
|
|
|
{
|
|
|
|
if (bpos_eq(iter->pos, SPOS_MAX))
|
|
|
|
iter->at_end = true;
|
|
|
|
else
|
|
|
|
iter->pos = bpos_successor(iter->pos);
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:25:00 -05:00
|
|
|
static void btree_and_journal_iter_prefetch(struct btree_and_journal_iter *_iter)
|
|
|
|
{
|
|
|
|
struct btree_and_journal_iter iter = *_iter;
|
|
|
|
struct bch_fs *c = iter.trans->c;
|
|
|
|
unsigned level = iter.journal.level;
|
|
|
|
struct bkey_buf tmp;
|
|
|
|
unsigned nr = test_bit(BCH_FS_started, &c->flags)
|
|
|
|
? (level > 1 ? 0 : 2)
|
|
|
|
: (level > 1 ? 1 : 16);
|
|
|
|
|
|
|
|
iter.prefetch = false;
|
2024-11-17 14:39:46 -05:00
|
|
|
iter.fail_if_too_many_whiteouts = true;
|
2024-01-22 14:25:00 -05:00
|
|
|
bch2_bkey_buf_init(&tmp);
|
|
|
|
|
|
|
|
while (nr--) {
|
|
|
|
bch2_btree_and_journal_iter_advance(&iter);
|
|
|
|
struct bkey_s_c k = bch2_btree_and_journal_iter_peek(&iter);
|
|
|
|
if (!k.k)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bch2_bkey_buf_reassemble(&tmp, c, k);
|
|
|
|
bch2_btree_node_prefetch(iter.trans, NULL, tmp.k, iter.journal.btree_id, level - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bch2_bkey_buf_exit(&tmp, c);
|
|
|
|
}
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
struct bkey_s_c bch2_btree_and_journal_iter_peek(struct btree_and_journal_iter *iter)
|
|
|
|
{
|
2024-03-25 13:51:29 -04:00
|
|
|
struct bkey_s_c btree_k, journal_k = bkey_s_c_null, ret;
|
2024-11-17 14:39:46 -05:00
|
|
|
size_t iters = 0;
|
2024-01-22 14:25:00 -05:00
|
|
|
|
|
|
|
if (iter->prefetch && iter->journal.level)
|
|
|
|
btree_and_journal_iter_prefetch(iter);
|
2023-08-05 16:08:44 -04:00
|
|
|
again:
|
|
|
|
if (iter->at_end)
|
|
|
|
return bkey_s_c_null;
|
|
|
|
|
2024-11-17 14:39:46 -05:00
|
|
|
iters++;
|
|
|
|
|
|
|
|
if (iters > 20 && iter->fail_if_too_many_whiteouts)
|
|
|
|
return bkey_s_c_null;
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
while ((btree_k = bch2_journal_iter_peek_btree(iter)).k &&
|
|
|
|
bpos_lt(btree_k.k->p, iter->pos))
|
|
|
|
bch2_journal_iter_advance_btree(iter);
|
|
|
|
|
2024-03-25 13:51:29 -04:00
|
|
|
if (iter->trans->journal_replay_not_finished)
|
|
|
|
while ((journal_k = bch2_journal_iter_peek(&iter->journal)).k &&
|
|
|
|
bpos_lt(journal_k.k->p, iter->pos))
|
|
|
|
bch2_journal_iter_advance(&iter->journal);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
ret = journal_k.k &&
|
|
|
|
(!btree_k.k || bpos_le(journal_k.k->p, btree_k.k->p))
|
|
|
|
? journal_k
|
|
|
|
: btree_k;
|
|
|
|
|
|
|
|
if (ret.k && iter->b && bpos_gt(ret.k->p, iter->b->data->max_key))
|
|
|
|
ret = bkey_s_c_null;
|
|
|
|
|
|
|
|
if (ret.k) {
|
|
|
|
iter->pos = ret.k->p;
|
|
|
|
if (bkey_deleted(ret.k)) {
|
|
|
|
bch2_btree_and_journal_iter_advance(iter);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter->pos = SPOS_MAX;
|
|
|
|
iter->at_end = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_btree_and_journal_iter_exit(struct btree_and_journal_iter *iter)
|
|
|
|
{
|
|
|
|
bch2_journal_iter_exit(&iter->journal);
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:37:42 -05:00
|
|
|
void __bch2_btree_and_journal_iter_init_node_iter(struct btree_trans *trans,
|
|
|
|
struct btree_and_journal_iter *iter,
|
2023-08-05 16:08:44 -04:00
|
|
|
struct btree *b,
|
|
|
|
struct btree_node_iter node_iter,
|
|
|
|
struct bpos pos)
|
|
|
|
{
|
|
|
|
memset(iter, 0, sizeof(*iter));
|
|
|
|
|
2024-01-22 14:37:42 -05:00
|
|
|
iter->trans = trans;
|
2023-08-05 16:08:44 -04:00
|
|
|
iter->b = b;
|
|
|
|
iter->node_iter = node_iter;
|
|
|
|
iter->pos = b->data->min_key;
|
|
|
|
iter->at_end = false;
|
2024-04-10 01:30:22 -04:00
|
|
|
INIT_LIST_HEAD(&iter->journal.list);
|
|
|
|
|
|
|
|
if (trans->journal_replay_not_finished) {
|
|
|
|
bch2_journal_iter_init(trans->c, &iter->journal, b->c.btree_id, b->c.level, pos);
|
|
|
|
if (!test_bit(BCH_FS_may_go_rw, &trans->c->flags))
|
|
|
|
list_add(&iter->journal.list, &trans->c->journal_iters);
|
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this version is used by btree_gc before filesystem has gone RW and
|
|
|
|
* multithreaded, so uses the journal_iters list:
|
|
|
|
*/
|
2024-01-22 14:37:42 -05:00
|
|
|
void bch2_btree_and_journal_iter_init_node_iter(struct btree_trans *trans,
|
|
|
|
struct btree_and_journal_iter *iter,
|
2023-08-05 16:08:44 -04:00
|
|
|
struct btree *b)
|
|
|
|
{
|
|
|
|
struct btree_node_iter node_iter;
|
|
|
|
|
|
|
|
bch2_btree_node_iter_init_from_start(&node_iter, b);
|
2024-01-22 14:37:42 -05:00
|
|
|
__bch2_btree_and_journal_iter_init_node_iter(trans, iter, b, node_iter, b->data->min_key);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sort and dedup all keys in the journal: */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When keys compare equal, oldest compares first:
|
|
|
|
*/
|
|
|
|
static int journal_sort_key_cmp(const void *_l, const void *_r)
|
|
|
|
{
|
|
|
|
const struct journal_key *l = _l;
|
|
|
|
const struct journal_key *r = _r;
|
bcachefs: opts.journal_rewind
Add a mount option for rewinding the journal, bringing the entire
filesystem to where it was at a previous point in time.
This is for extreme disaster recovery scenarios - it's not intended as
an undelete operation.
The option takes a journal sequence number; the desired sequence number
can be determined with 'bcachefs list_journal'
Caveats:
- The 'journal_transaction_names' option must have been enabled (it's on
by default). The option controls emitting of extra debug info in the
journal, so we can see what individual transactions were doing;
It also enables journalling of keys being overwritten, which is what
we rely on here.
- A full fsck run will be automatically triggered since alloc info will
be inconsistent. Only leaf node updates to non-alloc btrees are
rewound, since rewinding interior btree updates isn't possible or
desirable.
- We can't do anything about data that was deleted and overwritten.
Lots of metadata updates after the point in time we're rewinding to
shouldn't cause a problem, since we segragate data and metadata
allocations (this is in order to make repair by btree node scan
practical on larger filesystems; there's a small 64-bit per device
bitmap in the superblock of device ranges with btree nodes, and we try
to keep this small).
However, having discards enabled will cause problems, since buckets
are discarded as soon as they become empty (this is why we don't
implement fstrim: we don't need it).
Hopefully, this feature will be a one-off thing that's never used
again: this was implemented for recovering from the "vfs i_nlink 0 ->
subvol deletion" bug, and that bug was unusually disastrous and
additional safeguards have since been implemented.
But if it does turn out that we need this more in the future, I'll
have to implement an option so that empty buckets aren't discarded
immediately - lagging by perhaps 1% of device capacity.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2025-06-07 19:16:12 -04:00
|
|
|
int rewind = l->rewind && r->rewind ? -1 : 1;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
|
|
|
return journal_key_cmp(l, r) ?:
|
bcachefs: opts.journal_rewind
Add a mount option for rewinding the journal, bringing the entire
filesystem to where it was at a previous point in time.
This is for extreme disaster recovery scenarios - it's not intended as
an undelete operation.
The option takes a journal sequence number; the desired sequence number
can be determined with 'bcachefs list_journal'
Caveats:
- The 'journal_transaction_names' option must have been enabled (it's on
by default). The option controls emitting of extra debug info in the
journal, so we can see what individual transactions were doing;
It also enables journalling of keys being overwritten, which is what
we rely on here.
- A full fsck run will be automatically triggered since alloc info will
be inconsistent. Only leaf node updates to non-alloc btrees are
rewound, since rewinding interior btree updates isn't possible or
desirable.
- We can't do anything about data that was deleted and overwritten.
Lots of metadata updates after the point in time we're rewinding to
shouldn't cause a problem, since we segragate data and metadata
allocations (this is in order to make repair by btree node scan
practical on larger filesystems; there's a small 64-bit per device
bitmap in the superblock of device ranges with btree nodes, and we try
to keep this small).
However, having discards enabled will cause problems, since buckets
are discarded as soon as they become empty (this is why we don't
implement fstrim: we don't need it).
Hopefully, this feature will be a one-off thing that's never used
again: this was implemented for recovering from the "vfs i_nlink 0 ->
subvol deletion" bug, and that bug was unusually disastrous and
additional safeguards have since been implemented.
But if it does turn out that we need this more in the future, I'll
have to implement an option so that empty buckets aren't discarded
immediately - lagging by perhaps 1% of device capacity.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2025-06-07 19:16:12 -04:00
|
|
|
((cmp_int(l->journal_seq, r->journal_seq) ?:
|
|
|
|
cmp_int(l->journal_offset, r->journal_offset)) * rewind);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:49 -05:00
|
|
|
void bch2_journal_keys_put(struct bch_fs *c)
|
2023-08-05 16:08:44 -04:00
|
|
|
{
|
2023-11-17 23:13:49 -05:00
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2023-11-17 23:13:49 -05:00
|
|
|
BUG_ON(atomic_read(&keys->ref) <= 0);
|
|
|
|
|
|
|
|
if (!atomic_dec_and_test(&keys->ref))
|
|
|
|
return;
|
|
|
|
|
2024-02-23 22:43:24 -05:00
|
|
|
move_gap(keys, keys->nr);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-11-17 02:23:24 -05:00
|
|
|
darray_for_each(*keys, i) {
|
|
|
|
if (i->overwritten_range &&
|
|
|
|
(i == &darray_last(*keys) ||
|
|
|
|
i->overwritten_range != i[1].overwritten_range))
|
|
|
|
kfree(i->overwritten_range);
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
if (i->allocated)
|
|
|
|
kfree(i->k);
|
2024-11-17 02:23:24 -05:00
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-02-24 00:15:56 -05:00
|
|
|
kvfree(keys->data);
|
|
|
|
keys->data = NULL;
|
2023-08-05 16:08:44 -04:00
|
|
|
keys->nr = keys->gap = keys->size = 0;
|
2023-11-17 23:13:49 -05:00
|
|
|
|
2024-11-16 23:54:19 -05:00
|
|
|
struct journal_replay **i;
|
|
|
|
struct genradix_iter iter;
|
|
|
|
|
|
|
|
genradix_for_each(&c->journal_entries, iter, i)
|
|
|
|
kvfree(*i);
|
|
|
|
genradix_free(&c->journal_entries);
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __journal_keys_sort(struct journal_keys *keys)
|
|
|
|
{
|
2025-03-26 11:44:30 -04:00
|
|
|
sort_nonatomic(keys->data, keys->nr, sizeof(keys->data[0]),
|
|
|
|
journal_sort_key_cmp, NULL);
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-09-05 15:43:03 -04:00
|
|
|
cond_resched();
|
|
|
|
|
2024-02-24 00:19:09 -05:00
|
|
|
struct journal_key *dst = keys->data;
|
|
|
|
|
|
|
|
darray_for_each(*keys, src) {
|
2023-12-27 20:59:01 -05:00
|
|
|
/*
|
|
|
|
* We don't accumulate accounting keys here because we have to
|
|
|
|
* compare each individual accounting key against the version in
|
|
|
|
* the btree during replay:
|
|
|
|
*/
|
|
|
|
if (src->k->k.type != KEY_TYPE_accounting &&
|
|
|
|
src + 1 < &darray_top(*keys) &&
|
2024-02-24 00:19:09 -05:00
|
|
|
!journal_key_cmp(src, src + 1))
|
|
|
|
continue;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
2024-02-24 00:19:09 -05:00
|
|
|
*dst++ = *src;
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
2024-02-24 00:15:56 -05:00
|
|
|
keys->nr = dst - keys->data;
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_journal_keys_sort(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct genradix_iter iter;
|
|
|
|
struct journal_replay *i, **_i;
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
2024-02-24 00:19:09 -05:00
|
|
|
size_t nr_read = 0;
|
2023-08-05 16:08:44 -04:00
|
|
|
|
bcachefs: opts.journal_rewind
Add a mount option for rewinding the journal, bringing the entire
filesystem to where it was at a previous point in time.
This is for extreme disaster recovery scenarios - it's not intended as
an undelete operation.
The option takes a journal sequence number; the desired sequence number
can be determined with 'bcachefs list_journal'
Caveats:
- The 'journal_transaction_names' option must have been enabled (it's on
by default). The option controls emitting of extra debug info in the
journal, so we can see what individual transactions were doing;
It also enables journalling of keys being overwritten, which is what
we rely on here.
- A full fsck run will be automatically triggered since alloc info will
be inconsistent. Only leaf node updates to non-alloc btrees are
rewound, since rewinding interior btree updates isn't possible or
desirable.
- We can't do anything about data that was deleted and overwritten.
Lots of metadata updates after the point in time we're rewinding to
shouldn't cause a problem, since we segragate data and metadata
allocations (this is in order to make repair by btree node scan
practical on larger filesystems; there's a small 64-bit per device
bitmap in the superblock of device ranges with btree nodes, and we try
to keep this small).
However, having discards enabled will cause problems, since buckets
are discarded as soon as they become empty (this is why we don't
implement fstrim: we don't need it).
Hopefully, this feature will be a one-off thing that's never used
again: this was implemented for recovering from the "vfs i_nlink 0 ->
subvol deletion" bug, and that bug was unusually disastrous and
additional safeguards have since been implemented.
But if it does turn out that we need this more in the future, I'll
have to implement an option so that empty buckets aren't discarded
immediately - lagging by perhaps 1% of device capacity.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2025-06-07 19:16:12 -04:00
|
|
|
u64 rewind_seq = c->opts.journal_rewind ?: U64_MAX;
|
|
|
|
|
2023-08-05 16:08:44 -04:00
|
|
|
genradix_for_each(&c->journal_entries, iter, _i) {
|
|
|
|
i = *_i;
|
|
|
|
|
2024-02-25 18:48:21 -05:00
|
|
|
if (journal_replay_ignore(i))
|
2023-08-05 16:08:44 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
cond_resched();
|
|
|
|
|
bcachefs: opts.journal_rewind
Add a mount option for rewinding the journal, bringing the entire
filesystem to where it was at a previous point in time.
This is for extreme disaster recovery scenarios - it's not intended as
an undelete operation.
The option takes a journal sequence number; the desired sequence number
can be determined with 'bcachefs list_journal'
Caveats:
- The 'journal_transaction_names' option must have been enabled (it's on
by default). The option controls emitting of extra debug info in the
journal, so we can see what individual transactions were doing;
It also enables journalling of keys being overwritten, which is what
we rely on here.
- A full fsck run will be automatically triggered since alloc info will
be inconsistent. Only leaf node updates to non-alloc btrees are
rewound, since rewinding interior btree updates isn't possible or
desirable.
- We can't do anything about data that was deleted and overwritten.
Lots of metadata updates after the point in time we're rewinding to
shouldn't cause a problem, since we segragate data and metadata
allocations (this is in order to make repair by btree node scan
practical on larger filesystems; there's a small 64-bit per device
bitmap in the superblock of device ranges with btree nodes, and we try
to keep this small).
However, having discards enabled will cause problems, since buckets
are discarded as soon as they become empty (this is why we don't
implement fstrim: we don't need it).
Hopefully, this feature will be a one-off thing that's never used
again: this was implemented for recovering from the "vfs i_nlink 0 ->
subvol deletion" bug, and that bug was unusually disastrous and
additional safeguards have since been implemented.
But if it does turn out that we need this more in the future, I'll
have to implement an option so that empty buckets aren't discarded
immediately - lagging by perhaps 1% of device capacity.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2025-06-07 19:16:12 -04:00
|
|
|
vstruct_for_each(&i->j, entry) {
|
|
|
|
bool rewind = !entry->level &&
|
|
|
|
!btree_id_is_alloc(entry->btree_id) &&
|
|
|
|
le64_to_cpu(i->j.seq) >= rewind_seq;
|
|
|
|
|
|
|
|
if (entry->type != (rewind
|
|
|
|
? BCH_JSET_ENTRY_overwrite
|
|
|
|
: BCH_JSET_ENTRY_btree_keys))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!rewind && le64_to_cpu(i->j.seq) < c->journal_replay_seq_start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
jset_entry_for_each_key(entry, k) {
|
|
|
|
struct journal_key n = (struct journal_key) {
|
|
|
|
.btree_id = entry->btree_id,
|
|
|
|
.level = entry->level,
|
|
|
|
.rewind = rewind,
|
|
|
|
.k = k,
|
|
|
|
.journal_seq = le64_to_cpu(i->j.seq),
|
|
|
|
.journal_offset = k->_data - i->j._data,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (darray_push(keys, n)) {
|
|
|
|
__journal_keys_sort(keys);
|
|
|
|
|
|
|
|
if (keys->nr * 8 > keys->size * 7) {
|
|
|
|
bch_err(c, "Too many journal keys for slowpath; have %zu compacted, buf size %zu, processed %zu keys at seq %llu",
|
|
|
|
keys->nr, keys->size, nr_read, le64_to_cpu(i->j.seq));
|
|
|
|
return bch_err_throw(c, ENOMEM_journal_keys_sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG_ON(darray_push(keys, n));
|
2024-02-24 00:19:09 -05:00
|
|
|
}
|
|
|
|
|
bcachefs: opts.journal_rewind
Add a mount option for rewinding the journal, bringing the entire
filesystem to where it was at a previous point in time.
This is for extreme disaster recovery scenarios - it's not intended as
an undelete operation.
The option takes a journal sequence number; the desired sequence number
can be determined with 'bcachefs list_journal'
Caveats:
- The 'journal_transaction_names' option must have been enabled (it's on
by default). The option controls emitting of extra debug info in the
journal, so we can see what individual transactions were doing;
It also enables journalling of keys being overwritten, which is what
we rely on here.
- A full fsck run will be automatically triggered since alloc info will
be inconsistent. Only leaf node updates to non-alloc btrees are
rewound, since rewinding interior btree updates isn't possible or
desirable.
- We can't do anything about data that was deleted and overwritten.
Lots of metadata updates after the point in time we're rewinding to
shouldn't cause a problem, since we segragate data and metadata
allocations (this is in order to make repair by btree node scan
practical on larger filesystems; there's a small 64-bit per device
bitmap in the superblock of device ranges with btree nodes, and we try
to keep this small).
However, having discards enabled will cause problems, since buckets
are discarded as soon as they become empty (this is why we don't
implement fstrim: we don't need it).
Hopefully, this feature will be a one-off thing that's never used
again: this was implemented for recovering from the "vfs i_nlink 0 ->
subvol deletion" bug, and that bug was unusually disastrous and
additional safeguards have since been implemented.
But if it does turn out that we need this more in the future, I'll
have to implement an option so that empty buckets aren't discarded
immediately - lagging by perhaps 1% of device capacity.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2025-06-07 19:16:12 -04:00
|
|
|
nr_read++;
|
2024-02-24 00:19:09 -05:00
|
|
|
}
|
2023-08-05 16:08:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__journal_keys_sort(keys);
|
|
|
|
keys->gap = keys->nr;
|
|
|
|
|
2024-02-24 00:19:09 -05:00
|
|
|
bch_verbose(c, "Journal keys: %zu read, %zu after sorting and compacting", nr_read, keys->nr);
|
2023-08-05 16:08:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2024-03-19 18:56:26 -04:00
|
|
|
|
|
|
|
void bch2_shoot_down_journal_keys(struct bch_fs *c, enum btree_id btree,
|
|
|
|
unsigned level_min, unsigned level_max,
|
|
|
|
struct bpos start, struct bpos end)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
size_t dst = 0;
|
|
|
|
|
|
|
|
move_gap(keys, keys->nr);
|
|
|
|
|
|
|
|
darray_for_each(*keys, i)
|
|
|
|
if (!(i->btree_id == btree &&
|
|
|
|
i->level >= level_min &&
|
|
|
|
i->level <= level_max &&
|
|
|
|
bpos_ge(i->k->k.p, start) &&
|
|
|
|
bpos_le(i->k->k.p, end)))
|
|
|
|
keys->data[dst++] = *i;
|
|
|
|
keys->nr = keys->gap = dst;
|
|
|
|
}
|
2024-04-09 00:04:31 -04:00
|
|
|
|
|
|
|
void bch2_journal_keys_dump(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
struct printbuf buf = PRINTBUF;
|
|
|
|
|
|
|
|
pr_info("%zu keys:", keys->nr);
|
|
|
|
|
|
|
|
move_gap(keys, keys->nr);
|
|
|
|
|
|
|
|
darray_for_each(*keys, i) {
|
|
|
|
printbuf_reset(&buf);
|
2024-10-09 23:02:04 -04:00
|
|
|
prt_printf(&buf, "btree=");
|
|
|
|
bch2_btree_id_to_text(&buf, i->btree_id);
|
|
|
|
prt_printf(&buf, " l=%u ", i->level);
|
2024-04-09 00:04:31 -04:00
|
|
|
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(i->k));
|
2024-10-09 23:02:04 -04:00
|
|
|
pr_err("%s", buf.buf);
|
2024-04-09 00:04:31 -04:00
|
|
|
}
|
|
|
|
printbuf_exit(&buf);
|
|
|
|
}
|
2024-11-17 02:23:24 -05:00
|
|
|
|
|
|
|
void bch2_fs_journal_keys_init(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct journal_keys *keys = &c->journal_keys;
|
|
|
|
|
|
|
|
atomic_set(&keys->ref, 1);
|
|
|
|
keys->initial_ref_held = true;
|
|
|
|
mutex_init(&keys->overwrite_lock);
|
|
|
|
}
|