mrc0mmand / rpms / lvm2

Forked from rpms/lvm2 2 years ago
Clone

Blame SOURCES/lvm2-2_02_181-dmeventd-base-vdo-plugin.patch

5e29a5
 WHATS_NEW_DM                                   |   1 +
5e29a5
 configure                                      |   3 +-
5e29a5
 configure.ac                                   |   1 +
5e29a5
 daemons/dmeventd/libdevmapper-event.c          |   1 +
5e29a5
 daemons/dmeventd/plugins/Makefile.in           |   9 +-
5e29a5
 daemons/dmeventd/plugins/vdo/.exported_symbols |   3 +
5e29a5
 daemons/dmeventd/plugins/vdo/Makefile.in       |  36 +++
5e29a5
 daemons/dmeventd/plugins/vdo/dmeventd_vdo.c    | 406 +++++++++++++++++++++++++
5e29a5
 8 files changed, 453 insertions(+), 7 deletions(-)
5e29a5
 create mode 100644 daemons/dmeventd/plugins/vdo/.exported_symbols
5e29a5
 create mode 100644 daemons/dmeventd/plugins/vdo/Makefile.in
5e29a5
 create mode 100644 daemons/dmeventd/plugins/vdo/dmeventd_vdo.c
5e29a5
5e29a5
diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM
5e29a5
index e77352a..c42ee17 100644
5e29a5
--- a/WHATS_NEW_DM
5e29a5
+++ b/WHATS_NEW_DM
5e29a5
@@ -1,5 +1,6 @@
5e29a5
 Version 1.02.150 - 
5e29a5
 =================================
5e29a5
+  Add vdo plugin for monitoring VDO devices.
5e29a5
 
5e29a5
 Version 1.02.149 - 19th July 2018
5e29a5
 =================================
5e29a5
diff --git a/configure b/configure
5e29a5
index 10b49c6..eb7d70b 100755
5e29a5
--- a/configure
5e29a5
+++ b/configure
5e29a5
@@ -15648,7 +15648,7 @@ _ACEOF
5e29a5
 
5e29a5
 
5e29a5
 ################################################################################
5e29a5
-ac_config_files="$ac_config_files Makefile make.tmpl daemons/Makefile daemons/clvmd/Makefile daemons/cmirrord/Makefile daemons/dmeventd/Makefile daemons/dmeventd/libdevmapper-event.pc daemons/dmeventd/plugins/Makefile daemons/dmeventd/plugins/lvm2/Makefile daemons/dmeventd/plugins/raid/Makefile daemons/dmeventd/plugins/mirror/Makefile daemons/dmeventd/plugins/snapshot/Makefile daemons/dmeventd/plugins/thin/Makefile daemons/dmfilemapd/Makefile daemons/lvmdbusd/Makefile daemons/lvmdbusd/lvmdbusd daemons/lvmdbusd/lvmdb.py daemons/lvmdbusd/lvm_shell_proxy.py daemons/lvmdbusd/path.py daemons/lvmetad/Makefile daemons/lvmpolld/Makefile daemons/lvmlockd/Makefile device_mapper/Makefile conf/Makefile conf/example.conf conf/lvmlocal.conf conf/command_profile_template.profile conf/metadata_profile_template.profile include/.symlinks include/Makefile lib/Makefile lib/format1/Makefile lib/format_pool/Makefile lib/locking/Makefile include/lvm-version.h libdaemon/Makefile libdaemon/client/Makefile libdaemon/server/Makefile libdm/Makefile libdm/libdevmapper.pc liblvm/Makefile liblvm/liblvm2app.pc man/Makefile po/Makefile python/Makefile python/setup.py scripts/blkdeactivate.sh scripts/blk_availability_init_red_hat scripts/blk_availability_systemd_red_hat.service scripts/clvmd_init_red_hat scripts/cmirrord_init_red_hat scripts/com.redhat.lvmdbus1.service scripts/dm_event_systemd_red_hat.service scripts/dm_event_systemd_red_hat.socket scripts/lvm2_cluster_activation_red_hat.sh scripts/lvm2_cluster_activation_systemd_red_hat.service scripts/lvm2_clvmd_systemd_red_hat.service scripts/lvm2_cmirrord_systemd_red_hat.service scripts/lvm2_lvmdbusd_systemd_red_hat.service scripts/lvm2_lvmetad_init_red_hat scripts/lvm2_lvmetad_systemd_red_hat.service scripts/lvm2_lvmetad_systemd_red_hat.socket scripts/lvm2_lvmpolld_init_red_hat scripts/lvm2_lvmpolld_systemd_red_hat.service scripts/lvm2_lvmpolld_systemd_red_hat.socket scripts/lvm2_lvmlockd_systemd_red_hat.service scripts/lvm2_lvmlocking_systemd_red_hat.service scripts/lvm2_monitoring_init_red_hat scripts/lvm2_monitoring_systemd_red_hat.service scripts/lvm2_pvscan_systemd_red_hat@.service scripts/lvm2_tmpfiles_red_hat.conf scripts/lvmdump.sh scripts/Makefile test/Makefile test/api/Makefile test/api/python_lvm_unit.py test/unit/Makefile tools/Makefile udev/Makefile"
5e29a5
+ac_config_files="$ac_config_files Makefile make.tmpl daemons/Makefile daemons/clvmd/Makefile daemons/cmirrord/Makefile daemons/dmeventd/Makefile daemons/dmeventd/libdevmapper-event.pc daemons/dmeventd/plugins/Makefile daemons/dmeventd/plugins/lvm2/Makefile daemons/dmeventd/plugins/raid/Makefile daemons/dmeventd/plugins/mirror/Makefile daemons/dmeventd/plugins/snapshot/Makefile daemons/dmeventd/plugins/thin/Makefile daemons/dmeventd/plugins/vdo/Makefile daemons/dmfilemapd/Makefile daemons/lvmdbusd/Makefile daemons/lvmdbusd/lvmdbusd daemons/lvmdbusd/lvmdb.py daemons/lvmdbusd/lvm_shell_proxy.py daemons/lvmdbusd/path.py daemons/lvmetad/Makefile daemons/lvmpolld/Makefile daemons/lvmlockd/Makefile device_mapper/Makefile conf/Makefile conf/example.conf conf/lvmlocal.conf conf/command_profile_template.profile conf/metadata_profile_template.profile include/.symlinks include/Makefile lib/Makefile lib/format1/Makefile lib/format_pool/Makefile lib/locking/Makefile include/lvm-version.h libdaemon/Makefile libdaemon/client/Makefile libdaemon/server/Makefile libdm/Makefile libdm/libdevmapper.pc liblvm/Makefile liblvm/liblvm2app.pc man/Makefile po/Makefile python/Makefile python/setup.py scripts/blkdeactivate.sh scripts/blk_availability_init_red_hat scripts/blk_availability_systemd_red_hat.service scripts/clvmd_init_red_hat scripts/cmirrord_init_red_hat scripts/com.redhat.lvmdbus1.service scripts/dm_event_systemd_red_hat.service scripts/dm_event_systemd_red_hat.socket scripts/lvm2_cluster_activation_red_hat.sh scripts/lvm2_cluster_activation_systemd_red_hat.service scripts/lvm2_clvmd_systemd_red_hat.service scripts/lvm2_cmirrord_systemd_red_hat.service scripts/lvm2_lvmdbusd_systemd_red_hat.service scripts/lvm2_lvmetad_init_red_hat scripts/lvm2_lvmetad_systemd_red_hat.service scripts/lvm2_lvmetad_systemd_red_hat.socket scripts/lvm2_lvmpolld_init_red_hat scripts/lvm2_lvmpolld_systemd_red_hat.service scripts/lvm2_lvmpolld_systemd_red_hat.socket scripts/lvm2_lvmlockd_systemd_red_hat.service scripts/lvm2_lvmlocking_systemd_red_hat.service scripts/lvm2_monitoring_init_red_hat scripts/lvm2_monitoring_systemd_red_hat.service scripts/lvm2_pvscan_systemd_red_hat@.service scripts/lvm2_tmpfiles_red_hat.conf scripts/lvmdump.sh scripts/Makefile test/Makefile test/api/Makefile test/api/python_lvm_unit.py test/unit/Makefile tools/Makefile udev/Makefile"
5e29a5
 
5e29a5
 cat >confcache <<\_ACEOF
5e29a5
 # This file is a shell script that caches the results of configure
5e29a5
@@ -16356,6 +16356,7 @@ do
5e29a5
     "daemons/dmeventd/plugins/mirror/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/dmeventd/plugins/mirror/Makefile" ;;
5e29a5
     "daemons/dmeventd/plugins/snapshot/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/dmeventd/plugins/snapshot/Makefile" ;;
5e29a5
     "daemons/dmeventd/plugins/thin/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/dmeventd/plugins/thin/Makefile" ;;
5e29a5
+    "daemons/dmeventd/plugins/vdo/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/dmeventd/plugins/vdo/Makefile" ;;
5e29a5
     "daemons/dmfilemapd/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/dmfilemapd/Makefile" ;;
5e29a5
     "daemons/lvmdbusd/Makefile") CONFIG_FILES="$CONFIG_FILES daemons/lvmdbusd/Makefile" ;;
5e29a5
     "daemons/lvmdbusd/lvmdbusd") CONFIG_FILES="$CONFIG_FILES daemons/lvmdbusd/lvmdbusd" ;;
5e29a5
diff --git a/configure.ac b/configure.ac
5e29a5
index 9fa0c76..24bae39 100644
5e29a5
--- a/configure.ac
5e29a5
+++ b/configure.ac
5e29a5
@@ -2155,6 +2155,7 @@ daemons/dmeventd/plugins/raid/Makefile
5e29a5
 daemons/dmeventd/plugins/mirror/Makefile
5e29a5
 daemons/dmeventd/plugins/snapshot/Makefile
5e29a5
 daemons/dmeventd/plugins/thin/Makefile
5e29a5
+daemons/dmeventd/plugins/vdo/Makefile
5e29a5
 daemons/dmfilemapd/Makefile
5e29a5
 daemons/lvmdbusd/Makefile
5e29a5
 daemons/lvmdbusd/lvmdbusd
5e29a5
diff --git a/daemons/dmeventd/libdevmapper-event.c b/daemons/dmeventd/libdevmapper-event.c
5e29a5
index 9aeb4c5..f9a8a2b 100644
5e29a5
--- a/daemons/dmeventd/libdevmapper-event.c
5e29a5
+++ b/daemons/dmeventd/libdevmapper-event.c
5e29a5
@@ -645,6 +645,7 @@ int dm_event_register_handler(const struct dm_event_handler *dmevh)
5e29a5
 	uuid = dm_task_get_uuid(dmt);
5e29a5
 
5e29a5
 	if (!strstr(dmevh->dso, "libdevmapper-event-lvm2thin.so") &&
5e29a5
+	    !strstr(dmevh->dso, "libdevmapper-event-lvm2vdo.so") &&
5e29a5
 	    !strstr(dmevh->dso, "libdevmapper-event-lvm2snapshot.so") &&
5e29a5
 	    !strstr(dmevh->dso, "libdevmapper-event-lvm2mirror.so") &&
5e29a5
 	    !strstr(dmevh->dso, "libdevmapper-event-lvm2raid.so"))
5e29a5
diff --git a/daemons/dmeventd/plugins/Makefile.in b/daemons/dmeventd/plugins/Makefile.in
5e29a5
index 27edf55..951dd2b 100644
5e29a5
--- a/daemons/dmeventd/plugins/Makefile.in
5e29a5
+++ b/daemons/dmeventd/plugins/Makefile.in
5e29a5
@@ -1,6 +1,6 @@
5e29a5
 #
5e29a5
 # Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5e29a5
-# Copyright (C) 2004-2005, 2011 Red Hat, Inc. All rights reserved.
5e29a5
+# Copyright (C) 2004-2018 Red Hat, Inc. All rights reserved.
5e29a5
 #
5e29a5
 # This file is part of LVM2.
5e29a5
 #
5e29a5
@@ -16,11 +16,7 @@ srcdir = @srcdir@
5e29a5
 top_srcdir = @top_srcdir@
5e29a5
 top_builddir = @top_builddir@
5e29a5
 
5e29a5
-SUBDIRS += lvm2 snapshot raid thin mirror
5e29a5
-
5e29a5
-ifeq ($(MAKECMDGOALS),distclean)
5e29a5
-  SUBDIRS = lvm2 mirror snapshot raid thin
5e29a5
-endif
5e29a5
+SUBDIRS += lvm2 snapshot raid thin mirror vdo
5e29a5
 
5e29a5
 include $(top_builddir)/make.tmpl
5e29a5
 
5e29a5
@@ -28,3 +24,4 @@ snapshot: lvm2
5e29a5
 mirror: lvm2
5e29a5
 raid: lvm2
5e29a5
 thin: lvm2
5e29a5
+vdo: lvm2
5e29a5
diff --git a/daemons/dmeventd/plugins/vdo/.exported_symbols b/daemons/dmeventd/plugins/vdo/.exported_symbols
5e29a5
new file mode 100644
5e29a5
index 0000000..b88c705
5e29a5
--- /dev/null
5e29a5
+++ b/daemons/dmeventd/plugins/vdo/.exported_symbols
5e29a5
@@ -0,0 +1,3 @@
5e29a5
+process_event
5e29a5
+register_device
5e29a5
+unregister_device
5e29a5
diff --git a/daemons/dmeventd/plugins/vdo/Makefile.in b/daemons/dmeventd/plugins/vdo/Makefile.in
5e29a5
new file mode 100644
5e29a5
index 0000000..bda738a
5e29a5
--- /dev/null
5e29a5
+++ b/daemons/dmeventd/plugins/vdo/Makefile.in
5e29a5
@@ -0,0 +1,36 @@
5e29a5
+#
5e29a5
+# Copyright (C) 2018 Red Hat, Inc. All rights reserved.
5e29a5
+#
5e29a5
+# This file is part of LVM2.
5e29a5
+#
5e29a5
+# This copyrighted material is made available to anyone wishing to use,
5e29a5
+# modify, copy, or redistribute it subject to the terms and conditions
5e29a5
+# of the GNU General Public License v.2.
5e29a5
+#
5e29a5
+# You should have received a copy of the GNU General Public License
5e29a5
+# along with this program; if not, write to the Free Software Foundation,
5e29a5
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5e29a5
+
5e29a5
+srcdir = @srcdir@
5e29a5
+top_srcdir = @top_srcdir@
5e29a5
+top_builddir = @top_builddir@
5e29a5
+
5e29a5
+INCLUDES += -I$(top_srcdir)/daemons/dmeventd/plugins/lvm2
5e29a5
+CLDFLAGS += -L$(top_builddir)/daemons/dmeventd/plugins/lvm2
5e29a5
+
5e29a5
+SOURCES = dmeventd_vdo.c
5e29a5
+
5e29a5
+LIB_NAME = libdevmapper-event-lvm2vdo
5e29a5
+LIB_SHARED = $(LIB_NAME).$(LIB_SUFFIX)
5e29a5
+LIB_VERSION = $(LIB_VERSION_LVM)
5e29a5
+
5e29a5
+CFLOW_LIST = $(SOURCES)
5e29a5
+CFLOW_LIST_TARGET = $(LIB_NAME).cflow
5e29a5
+
5e29a5
+include $(top_builddir)/make.tmpl
5e29a5
+
5e29a5
+LIBS += -ldevmapper-event-lvm2 $(INTERNAL_LIBS)
5e29a5
+
5e29a5
+install_lvm2: install_dm_plugin
5e29a5
+
5e29a5
+install: install_lvm2
5e29a5
diff --git a/daemons/dmeventd/plugins/vdo/dmeventd_vdo.c b/daemons/dmeventd/plugins/vdo/dmeventd_vdo.c
5e29a5
new file mode 100644
5e29a5
index 0000000..d77ca79
5e29a5
--- /dev/null
5e29a5
+++ b/daemons/dmeventd/plugins/vdo/dmeventd_vdo.c
5e29a5
@@ -0,0 +1,406 @@
5e29a5
+/*
5e29a5
+ * Copyright (C) 2018 Red Hat, Inc. All rights reserved.
5e29a5
+ *
5e29a5
+ * This file is part of LVM2.
5e29a5
+ *
5e29a5
+ * This copyrighted material is made available to anyone wishing to use,
5e29a5
+ * modify, copy, or redistribute it subject to the terms and conditions
5e29a5
+ * of the GNU Lesser General Public License v.2.1.
5e29a5
+ *
5e29a5
+ * You should have received a copy of the GNU Lesser General Public License
5e29a5
+ * along with this program; if not, write to the Free Software Foundation,
5e29a5
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5e29a5
+ */
5e29a5
+
5e29a5
+#include "lib/misc/lib.h"
5e29a5
+#include "dmeventd_lvm.h"
5e29a5
+#include "daemons/dmeventd/libdevmapper-event.h"
5e29a5
+#include "device_mapper/vdo/target.h"
5e29a5
+
5e29a5
+#include <sys/wait.h>
5e29a5
+#include <stdarg.h>
5e29a5
+
5e29a5
+/* First warning when VDO pool is 80% full. */
5e29a5
+#define WARNING_THRESH	(DM_PERCENT_1 * 80)
5e29a5
+/* Run a check every 5%. */
5e29a5
+#define CHECK_STEP	(DM_PERCENT_1 *  5)
5e29a5
+/* Do not bother checking VDO pool is less than 50% full. */
5e29a5
+#define CHECK_MINIMUM	(DM_PERCENT_1 * 50)
5e29a5
+
5e29a5
+#define MAX_FAILS	(256)  /* ~42 mins between cmd call retry with 10s delay */
5e29a5
+
5e29a5
+#define VDO_DEBUG 0
5e29a5
+
5e29a5
+struct dso_state {
5e29a5
+	struct dm_pool *mem;
5e29a5
+	int percent_check;
5e29a5
+	int percent;
5e29a5
+	uint64_t known_data_size;
5e29a5
+	unsigned fails;
5e29a5
+	unsigned max_fails;
5e29a5
+	int restore_sigset;
5e29a5
+	sigset_t old_sigset;
5e29a5
+	pid_t pid;
5e29a5
+	char *argv[3];
5e29a5
+	const char *cmd_str;
5e29a5
+	const char *name;
5e29a5
+};
5e29a5
+
5e29a5
+DM_EVENT_LOG_FN("vdo")
5e29a5
+
5e29a5
+static int _run_command(struct dso_state *state)
5e29a5
+{
5e29a5
+	char val[16];
5e29a5
+	int i;
5e29a5
+
5e29a5
+	/* Mark for possible lvm2 command we are running from dmeventd
5e29a5
+	 * lvm2 will not try to talk back to dmeventd while processing it */
5e29a5
+	(void) setenv("LVM_RUN_BY_DMEVENTD", "1", 1);
5e29a5
+
5e29a5
+	if (state->percent) {
5e29a5
+		/* Prepare some known data to env vars for easy use */
5e29a5
+		if (dm_snprintf(val, sizeof(val), "%d",
5e29a5
+				state->percent / DM_PERCENT_1) != -1)
5e29a5
+			(void) setenv("DMEVENTD_VDO_POOL", val, 1);
5e29a5
+	} else {
5e29a5
+		/* For an error event it's for a user to check status and decide */
5e29a5
+		log_debug("Error event processing.");
5e29a5
+	}
5e29a5
+
5e29a5
+	log_verbose("Executing command: %s", state->cmd_str);
5e29a5
+
5e29a5
+	/* TODO:
5e29a5
+	 *   Support parallel run of 'task' and it's waitpid maintainence
5e29a5
+	 *   ATM we can't handle signaling of  SIGALRM
5e29a5
+	 *   as signalling is not allowed while 'process_event()' is running
5e29a5
+	 */
5e29a5
+	if (!(state->pid = fork())) {
5e29a5
+		/* child */
5e29a5
+		(void) close(0);
5e29a5
+		for (i = 3; i < 255; ++i) (void) close(i);
5e29a5
+		execvp(state->argv[0], state->argv);
5e29a5
+		_exit(errno);
5e29a5
+	} else if (state->pid == -1) {
5e29a5
+		log_error("Can't fork command %s.", state->cmd_str);
5e29a5
+		state->fails = 1;
5e29a5
+		return 0;
5e29a5
+	}
5e29a5
+
5e29a5
+	return 1;
5e29a5
+}
5e29a5
+
5e29a5
+static int _use_policy(struct dm_task *dmt, struct dso_state *state)
5e29a5
+{
5e29a5
+#if VDO_DEBUG
5e29a5
+	log_debug("dmeventd executes: %s.", state->cmd_str);
5e29a5
+#endif
5e29a5
+	if (state->argv[0])
5e29a5
+		return _run_command(state);
5e29a5
+
5e29a5
+	if (!dmeventd_lvm2_run_with_lock(state->cmd_str)) {
5e29a5
+		log_error("Failed command for %s.", dm_task_get_name(dmt));
5e29a5
+		state->fails = 1;
5e29a5
+		return 0;
5e29a5
+	}
5e29a5
+
5e29a5
+	state->fails = 0;
5e29a5
+
5e29a5
+	return 1;
5e29a5
+}
5e29a5
+
5e29a5
+/* Check if executed command has finished
5e29a5
+ * Only 1 command may run */
5e29a5
+static int _wait_for_pid(struct dso_state *state)
5e29a5
+{
5e29a5
+	int status = 0;
5e29a5
+
5e29a5
+	if (state->pid == -1)
5e29a5
+		return 1;
5e29a5
+
5e29a5
+	if (!waitpid(state->pid, &status, WNOHANG))
5e29a5
+		return 0;
5e29a5
+
5e29a5
+	/* Wait for finish */
5e29a5
+	if (WIFEXITED(status)) {
5e29a5
+		log_verbose("Child %d exited with status %d.",
5e29a5
+			    state->pid, WEXITSTATUS(status));
5e29a5
+		state->fails = WEXITSTATUS(status) ? 1 : 0;
5e29a5
+	} else {
5e29a5
+		if (WIFSIGNALED(status))
5e29a5
+			log_verbose("Child %d was terminated with status %d.",
5e29a5
+				    state->pid, WTERMSIG(status));
5e29a5
+		state->fails = 1;
5e29a5
+	}
5e29a5
+
5e29a5
+	state->pid = -1;
5e29a5
+
5e29a5
+	return 1;
5e29a5
+}
5e29a5
+
5e29a5
+void process_event(struct dm_task *dmt,
5e29a5
+		   enum dm_event_mask event __attribute__((unused)),
5e29a5
+		   void **user)
5e29a5
+{
5e29a5
+	const char *device = dm_task_get_name(dmt);
5e29a5
+	struct dso_state *state = *user;
5e29a5
+	void *next = NULL;
5e29a5
+	uint64_t start, length;
5e29a5
+	char *target_type = NULL;
5e29a5
+	char *params;
5e29a5
+	int needs_policy = 0;
5e29a5
+	struct dm_task *new_dmt = NULL;
5e29a5
+	struct dm_vdo_status_parse_result vdop = { .status = NULL };
5e29a5
+
5e29a5
+#if VDO_DEBUG
5e29a5
+	log_debug("Watch for VDO %s:%.2f%%.", state->name,
5e29a5
+		  dm_percent_to_round_float(state->percent_check, 2));
5e29a5
+#endif
5e29a5
+	if (!_wait_for_pid(state)) {
5e29a5
+		log_warn("WARNING: Skipping event, child %d is still running (%s).",
5e29a5
+			 state->pid, state->cmd_str);
5e29a5
+		return;
5e29a5
+	}
5e29a5
+
5e29a5
+	if (event & DM_EVENT_DEVICE_ERROR) {
5e29a5
+#if VDO_DEBUG
5e29a5
+		log_debug("VDO event error.");
5e29a5
+#endif
5e29a5
+		/* Error -> no need to check and do instant resize */
5e29a5
+		state->percent = 0;
5e29a5
+		if (_use_policy(dmt, state))
5e29a5
+			goto out;
5e29a5
+
5e29a5
+		stack;
5e29a5
+
5e29a5
+		if (!(new_dmt = dm_task_create(DM_DEVICE_STATUS)))
5e29a5
+			goto_out;
5e29a5
+
5e29a5
+		if (!dm_task_set_uuid(new_dmt, dm_task_get_uuid(dmt)))
5e29a5
+			goto_out;
5e29a5
+
5e29a5
+		/* Non-blocking status read */
5e29a5
+		if (!dm_task_no_flush(new_dmt))
5e29a5
+			log_warn("WARNING: Can't set no_flush for dm status.");
5e29a5
+
5e29a5
+		if (!dm_task_run(new_dmt))
5e29a5
+			goto_out;
5e29a5
+
5e29a5
+		dmt = new_dmt;
5e29a5
+	}
5e29a5
+
5e29a5
+	dm_get_next_target(dmt, next, &start, &length, &target_type, &params);
5e29a5
+
5e29a5
+	if (!target_type || (strcmp(target_type, "vdo") != 0)) {
5e29a5
+		log_error("Invalid target type.");
5e29a5
+		goto out;
5e29a5
+	}
5e29a5
+
5e29a5
+	if (!dm_vdo_status_parse(state->mem, params, &vdop)) {
5e29a5
+		log_error("Failed to parse status.");
5e29a5
+		goto out;
5e29a5
+	}
5e29a5
+
5e29a5
+	state->percent = dm_make_percent(vdop.status->used_blocks,
5e29a5
+					 vdop.status->total_blocks);
5e29a5
+
5e29a5
+#if VDO_DEBUG
5e29a5
+	log_debug("VDO %s status  %.2f%% " FMTu64 "/" FMTu64 ".",
5e29a5
+		  state->name, dm_percent_to_round_float(state->percent, 2),
5e29a5
+		  vdop.status->used_blocks, vdop.status->total_blocks);
5e29a5
+#endif
5e29a5
+
5e29a5
+	/* VDO pool size had changed. Clear the threshold. */
5e29a5
+	if (state->known_data_size != vdop.status->total_blocks) {
5e29a5
+		state->percent_check = CHECK_MINIMUM;
5e29a5
+		state->known_data_size = vdop.status->total_blocks;
5e29a5
+		state->fails = 0;
5e29a5
+	}
5e29a5
+
5e29a5
+	/*
5e29a5
+	 * Trigger action when threshold boundary is exceeded.
5e29a5
+	 * Report 80% threshold warning when it's used above 80%.
5e29a5
+	 * Only 100% is exception as it cannot be surpased so policy
5e29a5
+	 * action is called for:  >50%, >55% ... >95%, 100%
5e29a5
+	 */
5e29a5
+	if ((state->percent > WARNING_THRESH) &&
5e29a5
+	    (state->percent > state->percent_check))
5e29a5
+		log_warn("WARNING: VDO %s %s is now %.2f%% full.",
5e29a5
+			 state->name, device,
5e29a5
+			 dm_percent_to_round_float(state->percent, 2));
5e29a5
+	if (state->percent > CHECK_MINIMUM) {
5e29a5
+		/* Run action when usage raised more than CHECK_STEP since the last time */
5e29a5
+		if (state->percent > state->percent_check)
5e29a5
+			needs_policy = 1;
5e29a5
+		state->percent_check = (state->percent / CHECK_STEP + 1) * CHECK_STEP;
5e29a5
+		if (state->percent_check == DM_PERCENT_100)
5e29a5
+			state->percent_check--; /* Can't get bigger then 100% */
5e29a5
+	} else
5e29a5
+		state->percent_check = CHECK_MINIMUM;
5e29a5
+
5e29a5
+	/* Reduce number of _use_policy() calls by power-of-2 factor till frequency of MAX_FAILS is reached.
5e29a5
+	 * Avoids too high number of error retries, yet shows some status messages in log regularly.
5e29a5
+	 * i.e. PV could have been pvmoved and VG/LV was locked for a while...
5e29a5
+	 */
5e29a5
+	if (state->fails) {
5e29a5
+		if (state->fails++ <= state->max_fails) {
5e29a5
+			log_debug("Postponing frequently failing policy (%u <= %u).",
5e29a5
+				  state->fails - 1, state->max_fails);
5e29a5
+			return;
5e29a5
+		}
5e29a5
+		if (state->max_fails < MAX_FAILS)
5e29a5
+			state->max_fails <<= 1;
5e29a5
+		state->fails = needs_policy = 1; /* Retry failing command */
5e29a5
+	} else
5e29a5
+		state->max_fails = 1; /* Reset on success */
5e29a5
+
5e29a5
+	/* FIXME: ATM nothing can be done, drop 0, once it becomes useful */
5e29a5
+	if (0 && needs_policy)
5e29a5
+		_use_policy(dmt, state);
5e29a5
+out:
5e29a5
+	if (vdop.status)
5e29a5
+		dm_pool_free(state->mem, vdop.status);
5e29a5
+
5e29a5
+	if (new_dmt)
5e29a5
+		dm_task_destroy(new_dmt);
5e29a5
+}
5e29a5
+
5e29a5
+/* Handle SIGCHLD for a thread */
5e29a5
+static void _sig_child(int signum __attribute__((unused)))
5e29a5
+{
5e29a5
+	/* empty SIG_IGN */;
5e29a5
+}
5e29a5
+
5e29a5
+/* Setup handler for SIGCHLD when executing external command
5e29a5
+ * to get quick 'waitpid()' reaction
5e29a5
+ * It will interrupt syscall just like SIGALRM and
5e29a5
+ * invoke process_event().
5e29a5
+ */
5e29a5
+static void _init_thread_signals(struct dso_state *state)
5e29a5
+{
5e29a5
+	struct sigaction act = { .sa_handler = _sig_child };
5e29a5
+	sigset_t my_sigset;
5e29a5
+
5e29a5
+	sigemptyset(&my_sigset);
5e29a5
+
5e29a5
+	if (sigaction(SIGCHLD, &act, NULL))
5e29a5
+		log_warn("WARNING: Failed to set SIGCHLD action.");
5e29a5
+	else if (sigaddset(&my_sigset, SIGCHLD))
5e29a5
+		log_warn("WARNING: Failed to add SIGCHLD to set.");
5e29a5
+	else if (pthread_sigmask(SIG_UNBLOCK, &my_sigset, &state->old_sigset))
5e29a5
+		log_warn("WARNING: Failed to unblock SIGCHLD.");
5e29a5
+	else
5e29a5
+		state->restore_sigset = 1;
5e29a5
+}
5e29a5
+
5e29a5
+static void _restore_thread_signals(struct dso_state *state)
5e29a5
+{
5e29a5
+	if (state->restore_sigset &&
5e29a5
+	    pthread_sigmask(SIG_SETMASK, &state->old_sigset, NULL))
5e29a5
+		log_warn("WARNING: Failed to block SIGCHLD.");
5e29a5
+}
5e29a5
+
5e29a5
+int register_device(const char *device,
5e29a5
+		    const char *uuid,
5e29a5
+		    int major __attribute__((unused)),
5e29a5
+		    int minor __attribute__((unused)),
5e29a5
+		    void **user)
5e29a5
+{
5e29a5
+	struct dso_state *state;
5e29a5
+	const char *cmd;
5e29a5
+	char *str;
5e29a5
+	char cmd_str[PATH_MAX + 128 + 2]; /* cmd ' ' vg/lv \0 */
5e29a5
+        const char *name = "pool";
5e29a5
+
5e29a5
+	if (!dmeventd_lvm2_init_with_pool("vdo_pool_state", state))
5e29a5
+		goto_bad;
5e29a5
+
5e29a5
+	state->cmd_str = "";
5e29a5
+
5e29a5
+	/* Search for command for LVM- prefixed devices only */
5e29a5
+	cmd = (strncmp(uuid, "LVM-", 4) == 0) ? "_dmeventd_vdo_command" : "";
5e29a5
+
5e29a5
+	if (!dmeventd_lvm2_command(state->mem, cmd_str, sizeof(cmd_str), cmd, device))
5e29a5
+		goto_bad;
5e29a5
+
5e29a5
+	if (strncmp(cmd_str, "lvm ", 4) == 0) {
5e29a5
+		if (!(state->cmd_str = dm_pool_strdup(state->mem, cmd_str + 4))) {
5e29a5
+			log_error("Failed to copy lvm VDO command.");
5e29a5
+				goto bad;
5e29a5
+		}
5e29a5
+	} else if (cmd_str[0] == '/') {
5e29a5
+		if (!(state->cmd_str = dm_pool_strdup(state->mem, cmd_str))) {
5e29a5
+			log_error("Failed to copy VDO command.");
5e29a5
+			goto bad;
5e29a5
+		}
5e29a5
+
5e29a5
+		/* Find last space before 'vg/lv' */
5e29a5
+		if (!(str = strrchr(state->cmd_str, ' ')))
5e29a5
+			goto inval;
5e29a5
+
5e29a5
+		if (!(state->argv[0] = dm_pool_strndup(state->mem, state->cmd_str,
5e29a5
+						       str - state->cmd_str))) {
5e29a5
+			log_error("Failed to copy command.");
5e29a5
+			goto bad;
5e29a5
+		}
5e29a5
+
5e29a5
+		state->argv[1] = str + 1;  /* 1 argument - vg/lv */
5e29a5
+		_init_thread_signals(state);
5e29a5
+	} else if (cmd[0] == 0) {
5e29a5
+		state->name = "volume"; /* What to use with 'others?' */
5e29a5
+	} else/* Unuspported command format */
5e29a5
+		goto inval;
5e29a5
+
5e29a5
+	state->pid = -1;
5e29a5
+	state->name = name;
5e29a5
+	*user = state;
5e29a5
+
5e29a5
+	log_info("Monitoring VDO %s %s.", name, device);
5e29a5
+
5e29a5
+	return 1;
5e29a5
+inval:
5e29a5
+	log_error("Invalid command for monitoring: %s.", cmd_str);
5e29a5
+bad:
5e29a5
+	log_error("Failed to monitor VDO %s %s.", name, device);
5e29a5
+
5e29a5
+	if (state)
5e29a5
+		dmeventd_lvm2_exit_with_pool(state);
5e29a5
+
5e29a5
+	return 0;
5e29a5
+}
5e29a5
+
5e29a5
+int unregister_device(const char *device,
5e29a5
+		      const char *uuid __attribute__((unused)),
5e29a5
+		      int major __attribute__((unused)),
5e29a5
+		      int minor __attribute__((unused)),
5e29a5
+		      void **user)
5e29a5
+{
5e29a5
+	struct dso_state *state = *user;
5e29a5
+	const char *name = state->name;
5e29a5
+	int i;
5e29a5
+
5e29a5
+	for (i = 0; !_wait_for_pid(state) && (i < 6); ++i) {
5e29a5
+		if (i == 0)
5e29a5
+			/* Give it 2 seconds, then try to terminate & kill it */
5e29a5
+			log_verbose("Child %d still not finished (%s) waiting.",
5e29a5
+				    state->pid, state->cmd_str);
5e29a5
+		else if (i == 3) {
5e29a5
+			log_warn("WARNING: Terminating child %d.", state->pid);
5e29a5
+			kill(state->pid, SIGINT);
5e29a5
+			kill(state->pid, SIGTERM);
5e29a5
+		} else if (i == 5) {
5e29a5
+			log_warn("WARNING: Killing child %d.", state->pid);
5e29a5
+			kill(state->pid, SIGKILL);
5e29a5
+		}
5e29a5
+		sleep(1);
5e29a5
+	}
5e29a5
+
5e29a5
+	if (state->pid != -1)
5e29a5
+		log_warn("WARNING: Cannot kill child %d!", state->pid);
5e29a5
+
5e29a5
+	_restore_thread_signals(state);
5e29a5
+
5e29a5
+	dmeventd_lvm2_exit_with_pool(state);
5e29a5
+	log_info("No longer monitoring VDO %s %s.", name, device);
5e29a5
+
5e29a5
+	return 1;
5e29a5
+}