All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: qemu-devel@nongnu.org, armbru@redhat.com, fam@euphon.net,
	stefanha@redhat.com, vsementsov@virtuozzo.com, jsnow@redhat.com,
	mreitz@redhat.com, kwolf@redhat.com
Subject: [PATCH v4 08/36] util: add transactions.c
Date: Wed, 28 Apr 2021 18:17:36 +0300	[thread overview]
Message-ID: <20210428151804.439460-9-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20210428151804.439460-1-vsementsov@virtuozzo.com>

Add simple transaction API to use in further update of block graph
operations.

Supposed usage is:

- "prepare" is main function of the action and it should make the main
  effect of the action to be visible for the following actions, keeping
  possibility of roll-back, saving necessary things in action state,
  which is prepended to the action list (to do that, prepare func
  should call tran_add()). So, driver struct doesn't include "prepare"
  field, as it is supposed to be called directly.

- commit/rollback is supposed to be called for the list of action
  states, to commit/rollback all the actions in reverse order

- When possible "commit" should not make visible effect for other
  actions, which make possible transparent logical interaction between
  actions.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
---
 include/qemu/transactions.h | 63 ++++++++++++++++++++++++
 util/transactions.c         | 96 +++++++++++++++++++++++++++++++++++++
 MAINTAINERS                 |  6 +++
 util/meson.build            |  1 +
 4 files changed, 166 insertions(+)
 create mode 100644 include/qemu/transactions.h
 create mode 100644 util/transactions.c

diff --git a/include/qemu/transactions.h b/include/qemu/transactions.h
new file mode 100644
index 0000000000..92c5965235
--- /dev/null
+++ b/include/qemu/transactions.h
@@ -0,0 +1,63 @@
+/*
+ * Simple transactions API
+ *
+ * Copyright (c) 2021 Virtuozzo International GmbH.
+ *
+ * Author:
+ *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * = Generic transaction API =
+ *
+ * The intended usage is the following: you create "prepare" functions, which
+ * represents the actions. They will usually have Transaction* argument, and
+ * call tran_add() to register finalization callbacks. For finalization
+ * callbacks, prepare corresponding TransactionActionDrv structures.
+ *
+ * Then, when you need to make a transaction, create an empty Transaction by
+ * tran_create(), call your "prepare" functions on it, and finally call
+ * tran_abort() or tran_commit() to finalize the transaction by corresponding
+ * finalization actions in reverse order.
+ */
+
+#ifndef QEMU_TRANSACTIONS_H
+#define QEMU_TRANSACTIONS_H
+
+#include <gmodule.h>
+
+typedef struct TransactionActionDrv {
+    void (*abort)(void *opaque);
+    void (*commit)(void *opaque);
+    void (*clean)(void *opaque);
+} TransactionActionDrv;
+
+typedef struct Transaction Transaction;
+
+Transaction *tran_new(void);
+void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque);
+void tran_abort(Transaction *tran);
+void tran_commit(Transaction *tran);
+
+static inline void tran_finalize(Transaction *tran, int ret)
+{
+    if (ret < 0) {
+        tran_abort(tran);
+    } else {
+        tran_commit(tran);
+    }
+}
+
+#endif /* QEMU_TRANSACTIONS_H */
diff --git a/util/transactions.c b/util/transactions.c
new file mode 100644
index 0000000000..d0bc9a3e73
--- /dev/null
+++ b/util/transactions.c
@@ -0,0 +1,96 @@
+/*
+ * Simple transactions API
+ *
+ * Copyright (c) 2021 Virtuozzo International GmbH.
+ *
+ * Author:
+ *  Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qemu/transactions.h"
+#include "qemu/queue.h"
+
+typedef struct TransactionAction {
+    TransactionActionDrv *drv;
+    void *opaque;
+    QSLIST_ENTRY(TransactionAction) entry;
+} TransactionAction;
+
+struct Transaction {
+    QSLIST_HEAD(, TransactionAction) actions;
+};
+
+Transaction *tran_new(void)
+{
+    Transaction *tran = g_new(Transaction, 1);
+
+    QSLIST_INIT(&tran->actions);
+
+    return tran;
+}
+
+void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque)
+{
+    TransactionAction *act;
+
+    act = g_new(TransactionAction, 1);
+    *act = (TransactionAction) {
+        .drv = drv,
+        .opaque = opaque
+    };
+
+    QSLIST_INSERT_HEAD(&tran->actions, act, entry);
+}
+
+void tran_abort(Transaction *tran)
+{
+    TransactionAction *act, *next;
+
+    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
+        if (act->drv->abort) {
+            act->drv->abort(act->opaque);
+        }
+
+        if (act->drv->clean) {
+            act->drv->clean(act->opaque);
+        }
+
+        g_free(act);
+    }
+
+    g_free(tran);
+}
+
+void tran_commit(Transaction *tran)
+{
+    TransactionAction *act, *next;
+
+    QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
+        if (act->drv->commit) {
+            act->drv->commit(act->opaque);
+        }
+
+        if (act->drv->clean) {
+            act->drv->clean(act->opaque);
+        }
+
+        g_free(act);
+    }
+
+    g_free(tran);
+}
diff --git a/MAINTAINERS b/MAINTAINERS
index 36055f14c5..4c05ff8bba 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2532,6 +2532,12 @@ M: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
 S: Maintained
 F: scripts/simplebench/
 
+Transactions helper
+M: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
+S: Maintained
+F: include/qemu/transactions.h
+F: util/transactions.c
+
 QAPI
 M: Markus Armbruster <armbru@redhat.com>
 M: Michael Roth <michael.roth@amd.com>
diff --git a/util/meson.build b/util/meson.build
index 510765cde4..97fad44105 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -41,6 +41,7 @@ util_ss.add(files('qsp.c'))
 util_ss.add(files('range.c'))
 util_ss.add(files('stats64.c'))
 util_ss.add(files('systemd.c'))
+util_ss.add(files('transactions.c'))
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('drm.c'))
 util_ss.add(files('guest-random.c'))
 util_ss.add(files('yank.c'))
-- 
2.29.2



  parent reply	other threads:[~2021-04-28 15:51 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-28 15:17 [PATCH v4 00/36] block: update graph permissions update Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 01/36] tests/test-bdrv-graph-mod: add test_parallel_exclusive_write Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 02/36] tests/test-bdrv-graph-mod: add test_parallel_perm_update Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 03/36] tests/test-bdrv-graph-mod: add test_append_greedy_filter Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 04/36] block: bdrv_append(): don't consume reference Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 05/36] block: BdrvChildClass: add .get_parent_aio_context handler Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 06/36] block: drop ctx argument from bdrv_root_attach_child Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 07/36] block: make bdrv_reopen_{prepare, commit, abort} private Vladimir Sementsov-Ogievskiy via
2021-04-28 15:17 ` Vladimir Sementsov-Ogievskiy [this message]
2021-04-28 15:17 ` [PATCH v4 09/36] block: bdrv_refresh_perms: check for parents permissions conflict Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 10/36] block: refactor bdrv_child* permission functions Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 11/36] block: rewrite bdrv_child_try_set_perm() using bdrv_refresh_perms() Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 12/36] block: inline bdrv_child_*() permission functions calls Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 13/36] block: use topological sort for permission update Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 14/36] block: add bdrv_drv_set_perm transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 15/36] block: add bdrv_list_* permission update functions Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 16/36] block: add bdrv_replace_child_safe() transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 17/36] block: fix bdrv_replace_node_common Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 18/36] block: add bdrv_attach_child_common() transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 19/36] block: add bdrv_attach_child_noperm() " Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 20/36] block: split out bdrv_replace_node_noperm() Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 21/36] block: adapt bdrv_append() for inserting filters Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 22/36] block: add bdrv_remove_filter_or_cow transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 23/36] block: introduce bdrv_drop_filter() Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 24/36] block/backup-top: drop .active Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 25/36] block: drop ignore_children for permission update functions Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 26/36] block: make bdrv_unset_inherits_from to be a transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 27/36] block: make bdrv_refresh_limits() " Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 28/36] block: add bdrv_set_backing_noperm() " Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 29/36] block: bdrv_reopen_multiple(): move bdrv_flush to separate pre-prepare Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 30/36] block: bdrv_reopen_multiple: refresh permissions on updated graph Vladimir Sementsov-Ogievskiy
2021-04-28 15:17 ` [PATCH v4 31/36] block: drop unused permission update functions Vladimir Sementsov-Ogievskiy
2021-04-28 15:18 ` [PATCH v4 32/36] block: inline bdrv_check_perm_common() Vladimir Sementsov-Ogievskiy
2021-04-28 15:18 ` [PATCH v4 33/36] block: inline bdrv_replace_child() Vladimir Sementsov-Ogievskiy
2021-04-28 15:18 ` [PATCH v4 34/36] block: refactor bdrv_child_set_perm_safe() transaction action Vladimir Sementsov-Ogievskiy
2021-04-28 15:18 ` [PATCH v4 35/36] block: rename bdrv_replace_child_safe() to bdrv_replace_child() Vladimir Sementsov-Ogievskiy
2021-04-28 15:18 ` [PATCH v4 36/36] block: refactor bdrv_node_check_perm() Vladimir Sementsov-Ogievskiy
2021-04-28 16:15 ` [PATCH v4 00/36] block: update graph permissions update no-reply
2021-04-28 17:03 ` Kevin Wolf
2021-04-28 19:50   ` Vladimir Sementsov-Ogievskiy
2021-05-05  7:24     ` Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210428151804.439460-9-vsementsov@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=armbru@redhat.com \
    --cc=fam@euphon.net \
    --cc=jsnow@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.