bcachefs: Add a mechanism for running callbacks at trans commit time
authorKent Overstreet <kent.overstreet@gmail.com>
Thu, 4 Feb 2021 02:51:56 +0000 (21:51 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:57 +0000 (17:08 -0400)
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_iter.c
fs/bcachefs/btree_types.h
fs/bcachefs/btree_update.h
fs/bcachefs/btree_update_leaf.c

index fb7614367e1cc0baf1895944beba351f6bc7d3a9..74c4cacb9aa74eeb024f3d9271962aa152d9a0f6 100644 (file)
@@ -2144,6 +2144,7 @@ void bch2_trans_reset(struct btree_trans *trans, unsigned flags)
        trans->nr_updates2              = 0;
        trans->mem_top                  = 0;
 
+       trans->hooks                    = NULL;
        trans->extra_journal_entries    = NULL;
        trans->extra_journal_entry_u64s = 0;
 
index d218d883225b01625b5ebba9cabce234845b34a7..bcd8db34d7ee50eaac9dd14898f2f547d0f65a90 100644 (file)
@@ -343,6 +343,14 @@ struct btree_insert_entry {
 #define BTREE_ITER_MAX         32
 #endif
 
+struct btree_trans_commit_hook;
+typedef int (btree_trans_commit_hook_fn)(struct btree_trans *, struct btree_trans_commit_hook *);
+
+struct btree_trans_commit_hook {
+       btree_trans_commit_hook_fn      *fn;
+       struct btree_trans_commit_hook  *next;
+};
+
 #define BTREE_TRANS_MEM_MAX    (1U << 14)
 
 struct btree_trans {
@@ -379,6 +387,7 @@ struct btree_trans {
        struct btree_insert_entry *updates2;
 
        /* update path: */
+       struct btree_trans_commit_hook *hooks;
        struct jset_entry       *extra_journal_entries;
        unsigned                extra_journal_entry_u64s;
        struct journal_entry_pin *journal_pin;
index a251380801692fc232ff2944e7d9d127e6b415e1..4ce12ae29a556ff3edcfe881c40787c071996dd8 100644 (file)
@@ -77,6 +77,8 @@ int bch2_btree_node_update_key(struct bch_fs *, struct btree_iter *,
 
 int bch2_trans_update(struct btree_trans *, struct btree_iter *,
                      struct bkey_i *, enum btree_trigger_flags);
+void bch2_trans_commit_hook(struct btree_trans *,
+                           struct btree_trans_commit_hook *);
 int __bch2_trans_commit(struct btree_trans *);
 
 /**
index 62fa0d59242ab9b528403360ba76297d5bf5d3fd..178a9369880787103f3fd94ef4272f75cbf014d0 100644 (file)
@@ -369,6 +369,7 @@ bch2_trans_commit_write_locked(struct btree_trans *trans,
        struct bch_fs *c = trans->c;
        struct bch_fs_usage_online *fs_usage = NULL;
        struct btree_insert_entry *i;
+       struct btree_trans_commit_hook *h;
        unsigned u64s = 0;
        bool marking = false;
        int ret;
@@ -386,6 +387,14 @@ bch2_trans_commit_write_locked(struct btree_trans *trans,
 
        prefetch(&trans->c->journal.flags);
 
+       h = trans->hooks;
+       while (h) {
+               ret = h->fn(trans, h);
+               if (ret)
+                       return ret;
+               h = h->next;
+       }
+
        trans_for_each_update2(trans, i) {
                /* Multiple inserts might go to same leaf: */
                if (!same_leaf_as_prev(trans, i))
@@ -1057,6 +1066,13 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
        return 0;
 }
 
+void bch2_trans_commit_hook(struct btree_trans *trans,
+                           struct btree_trans_commit_hook *h)
+{
+       h->next = trans->hooks;
+       trans->hooks = h;
+}
+
 int __bch2_btree_insert(struct btree_trans *trans,
                        enum btree_id id, struct bkey_i *k)
 {