1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
8 #include "logged_ops.h"
11 struct bch_logged_op_fn {
13 int (*resume)(struct btree_trans *, struct bkey_i *);
16 static const struct bch_logged_op_fn logged_op_fns[] = {
18 .type = KEY_TYPE_logged_op_##n, \
19 .resume = bch2_resume_logged_op_##n, \
25 static const struct bch_logged_op_fn *logged_op_fn(enum bch_bkey_type type)
27 for (unsigned i = 0; i < ARRAY_SIZE(logged_op_fns); i++)
28 if (logged_op_fns[i].type == type)
29 return logged_op_fns + i;
33 static int resume_logged_op(struct btree_trans *trans, struct btree_iter *iter,
36 struct bch_fs *c = trans->c;
37 const struct bch_logged_op_fn *fn = logged_op_fn(k.k->type);
39 u32 restart_count = trans->restart_count;
45 bch2_bkey_buf_init(&sk);
46 bch2_bkey_buf_reassemble(&sk, c, k);
48 ret = drop_locks_do(trans, (bch2_fs_lazy_rw(c), 0)) ?:
49 fn->resume(trans, sk.k) ?: trans_was_restarted(trans, restart_count);
51 bch2_bkey_buf_exit(&sk, c);
55 int bch2_resume_logged_ops(struct bch_fs *c)
57 int ret = bch2_trans_run(c,
58 for_each_btree_key(trans, iter,
59 BTREE_ID_logged_ops, POS_MIN,
60 BTREE_ITER_PREFETCH, k,
61 resume_logged_op(trans, &iter, k)));
66 static int __bch2_logged_op_start(struct btree_trans *trans, struct bkey_i *k)
68 struct btree_iter iter;
71 ret = bch2_bkey_get_empty_slot(trans, &iter, BTREE_ID_logged_ops, POS_MAX);
77 ret = bch2_trans_update(trans, &iter, k, 0);
78 bch2_trans_iter_exit(trans, &iter);
82 int bch2_logged_op_start(struct btree_trans *trans, struct bkey_i *k)
84 return commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
85 __bch2_logged_op_start(trans, k));
88 void bch2_logged_op_finish(struct btree_trans *trans, struct bkey_i *k)
90 int ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
91 bch2_btree_delete(trans, BTREE_ID_logged_ops, k->k.p, 0));
93 * This needs to be a fatal error because we've left an unfinished
94 * operation in the logged ops btree.
96 * We should only ever see an error here if the filesystem has already
97 * been shut down, but make sure of that here:
100 struct bch_fs *c = trans->c;
101 struct printbuf buf = PRINTBUF;
103 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(k));
104 bch2_fs_fatal_error(c, "%s: error deleting logged operation %s: %s",
105 __func__, buf.buf, bch2_err_str(ret));