[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 03/21] util: add transactions.c
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH 03/21] util: add transactions.c |
Date: |
Mon, 23 Nov 2020 23:12:15 +0300 |
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 list. 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>
---
include/qemu/transactions.h | 46 +++++++++++++++++++++
util/transactions.c | 81 +++++++++++++++++++++++++++++++++++++
util/meson.build | 1 +
3 files changed, 128 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..db8fced65f
--- /dev/null
+++ b/include/qemu/transactions.h
@@ -0,0 +1,46 @@
+/*
+ * Simple transactions API
+ *
+ * Copyright (c) 2020 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/>.
+ */
+
+#ifndef QEMU_TRANSACTIONS_H
+#define QEMU_TRANSACTIONS_H
+
+#include <gmodule.h>
+
+typedef struct BdrvActionDrv {
+ void (*abort)(void *opeque);
+ void (*commit)(void *opeque);
+ void (*clean)(void *opeque);
+} BdrvActionDrv;
+
+void tran_prepend(GSList **list, BdrvActionDrv *drv, void *opaque);
+void tran_abort(GSList *backup);
+void tran_commit(GSList *backup);
+static inline void tran_finalize(GSList *backup, int ret)
+{
+ if (ret < 0) {
+ tran_abort(backup);
+ } else {
+ tran_commit(backup);
+ }
+}
+
+#endif /* QEMU_TRANSACTIONS_H */
diff --git a/util/transactions.c b/util/transactions.c
new file mode 100644
index 0000000000..845d77fc16
--- /dev/null
+++ b/util/transactions.c
@@ -0,0 +1,81 @@
+/*
+ * Simple transactions API
+ *
+ * Copyright (c) 2020 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"
+
+typedef struct BdrvAction {
+ BdrvActionDrv *drv;
+ void *opaque;
+} BdrvAction;
+
+void tran_prepend(GSList **list, BdrvActionDrv *drv, void *opaque)
+{
+ BdrvAction *act;
+
+ act = g_new(BdrvAction, 1);
+ *act = (BdrvAction) {
+ .drv = drv,
+ .opaque = opaque
+ };
+
+ *list = g_slist_prepend(*list, act);
+}
+
+void tran_abort(GSList *list)
+{
+ GSList *p;
+
+ for (p = list; p != NULL; p = p->next) {
+ BdrvAction *act = p->data;
+
+ if (act->drv->abort) {
+ act->drv->abort(act->opaque);
+ }
+
+ if (act->drv->clean) {
+ act->drv->clean(act->opaque);
+ }
+ }
+
+ g_slist_free_full(list, g_free);
+}
+
+void tran_commit(GSList *list)
+{
+ GSList *p;
+
+ for (p = list; p != NULL; p = p->next) {
+ BdrvAction *act = p->data;
+
+ if (act->drv->commit) {
+ act->drv->commit(act->opaque);
+ }
+
+ if (act->drv->clean) {
+ act->drv->clean(act->opaque);
+ }
+ }
+
+ g_slist_free_full(list, g_free);
+}
diff --git a/util/meson.build b/util/meson.build
index f359af0d46..8c7c28bd40 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'))
--
2.21.3
- [PATCH RFC 00/21] block: update graph permissions update, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 01/21] tests/test-bdrv-graph-mod: add test_parallel_exclusive_write, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 2/2] block: assert that permission commit sets same permissions, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 02/21] tests/test-bdrv-graph-mod: add test_parallel_perm_update, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 03/21] util: add transactions.c,
Vladimir Sementsov-Ogievskiy <=
- [PATCH 04/21] block: bdrv_refresh_perms: check parents compliance, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 06/21] block: rewrite bdrv_child_try_set_perm() using bdrv_refresh_perms(), Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 07/21] block: inline bdrv_child_*() permission functions calls, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 08/21] block: use topological sort for permission update, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 05/21] block: refactor bdrv_child* permission functions, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 09/21] block: add bdrv_drv_set_perm transaction action, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 10/21] block: add bdrv_list_* permission update functions, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 11/21] block: add bdrv_replace_child_safe() transaction action, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 12/21] block: return value from bdrv_replace_node(), Vladimir Sementsov-Ogievskiy, 2020/11/23