Blame SOURCES/cryptsetup-1.7.6-crypt_deactivate-fail-earlier-when-holders-detected.patch

8af939
From 2e4aaa1adad2d0838593b13efbf5efe79f58255c Mon Sep 17 00:00:00 2001
8af939
From: Ondrej Kozina <okozina@redhat.com>
8af939
Date: Mon, 16 Oct 2017 16:41:43 +0200
8af939
Subject: [PATCH] crypt_deactivate: fail earlier when holders detected
8af939
8af939
crypt_deactivate fails earlier without noisy dm retries
8af939
when other device holders detected. The early detection
8af939
works if:
8af939
8af939
a) other device-mapper device has a hold reference on the
8af939
   device
8af939
8af939
- or -
8af939
8af939
b) mounted fs is detected on the device
8af939
8af939
diff -rupN cryptsetup-1.7.4.old/config.h.in cryptsetup-1.7.4/config.h.in
8af939
--- cryptsetup-1.7.4.old/config.h.in	2017-03-15 10:43:26.000000000 +0100
8af939
+++ cryptsetup-1.7.4/config.h.in	2017-10-19 09:37:17.000000000 +0200
8af939
@@ -97,6 +97,14 @@
8af939
    */
8af939
 #undef HAVE_DCGETTEXT
8af939
 
8af939
+/* Define to 1 if you have the declaration of `dm_device_has_holders', and to
8af939
+   0 if you don't. */
8af939
+#undef HAVE_DECL_DM_DEVICE_HAS_HOLDERS
8af939
+
8af939
+/* Define to 1 if you have the declaration of `dm_device_has_mounted_fs', and
8af939
+   to 0 if you don't. */
8af939
+#undef HAVE_DECL_DM_DEVICE_HAS_MOUNTED_FS
8af939
+
8af939
 /* Define to 1 if you have the declaration of `dm_task_retry_remove', and to 0
8af939
    if you don't. */
8af939
 #undef HAVE_DECL_DM_TASK_RETRY_REMOVE
8af939
diff -rupN cryptsetup-1.7.4.old/configure cryptsetup-1.7.4/configure
8af939
--- cryptsetup-1.7.4.old/configure	2017-03-15 10:43:13.000000000 +0100
8af939
+++ cryptsetup-1.7.4/configure	2017-10-19 09:37:18.590530138 +0200
8af939
@@ -16735,6 +16735,30 @@ cat >>confdefs.h <<_ACEOF
8af939
 #define HAVE_DECL_DM_TASK_RETRY_REMOVE $ac_have_decl
8af939
 _ACEOF
8af939
 
8af939
+ac_fn_c_check_decl "$LINENO" "dm_device_has_mounted_fs" "ac_cv_have_decl_dm_device_has_mounted_fs" "#include <libdevmapper.h>
8af939
+"
8af939
+if test "x$ac_cv_have_decl_dm_device_has_mounted_fs" = xyes; then :
8af939
+  ac_have_decl=1
8af939
+else
8af939
+  ac_have_decl=0
8af939
+fi
8af939
+
8af939
+cat >>confdefs.h <<_ACEOF
8af939
+#define HAVE_DECL_DM_DEVICE_HAS_MOUNTED_FS $ac_have_decl
8af939
+_ACEOF
8af939
+
8af939
+ac_fn_c_check_decl "$LINENO" "dm_device_has_holders" "ac_cv_have_decl_dm_device_has_holders" "#include <libdevmapper.h>
8af939
+"
8af939
+if test "x$ac_cv_have_decl_dm_device_has_holders" = xyes; then :
8af939
+  ac_have_decl=1
8af939
+else
8af939
+  ac_have_decl=0
8af939
+fi
8af939
+
8af939
+cat >>confdefs.h <<_ACEOF
8af939
+#define HAVE_DECL_DM_DEVICE_HAS_HOLDERS $ac_have_decl
8af939
+_ACEOF
8af939
+
8af939
 ac_fn_c_check_decl "$LINENO" "DM_UDEV_DISABLE_DISK_RULES_FLAG" "ac_cv_have_decl_DM_UDEV_DISABLE_DISK_RULES_FLAG" "#include <libdevmapper.h>
8af939
 "
8af939
 if test "x$ac_cv_have_decl_DM_UDEV_DISABLE_DISK_RULES_FLAG" = xyes; then :
8af939
diff --git a/lib/libdevmapper.c b/lib/libdevmapper.c
8af939
index a0d6872..d6017b1 100644
8af939
--- a/lib/libdevmapper.c
8af939
+++ b/lib/libdevmapper.c
8af939
@@ -1181,6 +1181,13 @@ int dm_query_device(struct crypt_device *cd, const char *name,
8af939
 			dmd->uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN);
8af939
 	}
8af939
 
8af939
+	dmd->holders = 0;
8af939
+#if (HAVE_DECL_DM_DEVICE_HAS_HOLDERS && HAVE_DECL_DM_DEVICE_HAS_MOUNTED_FS)
8af939
+	if (get_flags & DM_ACTIVE_HOLDERS)
8af939
+		dmd->holders = (dm_device_has_mounted_fs(dmi.major, dmi.minor) ||
8af939
+				dm_device_has_holders(dmi.major, dmi.minor));
8af939
+#endif
8af939
+
8af939
 	r = (dmi.open_count > 0);
8af939
 out:
8af939
 	if (dmt)
8af939
diff --git a/lib/setup.c b/lib/setup.c
8af939
index b2e4396..93e8079 100644
8af939
--- a/lib/setup.c
8af939
+++ b/lib/setup.c
8af939
@@ -2249,6 +2249,7 @@ int crypt_activate_by_volume_key(struct crypt_device *cd,
8af939
 int crypt_deactivate(struct crypt_device *cd, const char *name)
8af939
 {
8af939
 	struct crypt_device *fake_cd = NULL;
8af939
+	struct crypt_dm_active_device dmd = {};
8af939
 	int r;
8af939
 
8af939
 	if (!name)
8af939
@@ -2266,6 +2267,13 @@ int crypt_deactivate(struct crypt_device *cd, const char *name)
8af939
 	switch (crypt_status(cd, name)) {
8af939
 		case CRYPT_ACTIVE:
8af939
 		case CRYPT_BUSY:
8af939
+			r = dm_query_device(cd, name, DM_ACTIVE_HOLDERS, &dmd);
8af939
+			if (r >= 0 && dmd.holders) {
8af939
+				log_err(cd, _("Device %s is still in use.\n"), name);
8af939
+				r = -EBUSY;
8af939
+				break;
8af939
+			}
8af939
+
8af939
 			if (isTCRYPT(cd->type))
8af939
 				r = TCRYPT_deactivate(cd, name);
8af939
 			else
8af939
diff --git a/lib/utils_dm.h b/lib/utils_dm.h
8af939
index c87e9aa..cf22e12 100644
8af939
--- a/lib/utils_dm.h
8af939
+++ b/lib/utils_dm.h
8af939
@@ -48,14 +48,16 @@ uint32_t dm_flags(void);
8af939
 
8af939
 #define DM_ACTIVE_DEVICE	(1 << 0)
8af939
 #define DM_ACTIVE_UUID		(1 << 1)
8af939
+#define DM_ACTIVE_HOLDERS	(1 << 2)
8af939
 
8af939
-#define DM_ACTIVE_CRYPT_CIPHER	(1 << 2)
8af939
-#define DM_ACTIVE_CRYPT_KEYSIZE	(1 << 3)
8af939
-#define DM_ACTIVE_CRYPT_KEY	(1 << 4)
8af939
+#define DM_ACTIVE_CRYPT_CIPHER	(1 << 3)
8af939
+#define DM_ACTIVE_CRYPT_KEYSIZE	(1 << 4)
8af939
+#define DM_ACTIVE_CRYPT_KEY	(1 << 5)
8af939
+
8af939
+#define DM_ACTIVE_VERITY_ROOT_HASH	(1 << 6)
8af939
+#define DM_ACTIVE_VERITY_HASH_DEVICE	(1 << 7)
8af939
+#define DM_ACTIVE_VERITY_PARAMS		(1 << 8)
8af939
 
8af939
-#define DM_ACTIVE_VERITY_ROOT_HASH	(1 << 5)
8af939
-#define DM_ACTIVE_VERITY_HASH_DEVICE	(1 << 6)
8af939
-#define DM_ACTIVE_VERITY_PARAMS		(1 << 7)
8af939
 
8af939
 struct crypt_dm_active_device {
8af939
 	enum { DM_CRYPT = 0, DM_VERITY } target;
8af939
@@ -63,6 +65,7 @@ struct crypt_dm_active_device {
8af939
 	uint32_t flags;		/* activation flags */
8af939
 	const char *uuid;
8af939
 	struct device *data_device;
8af939
+	unsigned holders:1;
8af939
 	union {
8af939
 	struct {
8af939
 		const char *cipher;
8af939
-- 
8af939
1.8.3.1
8af939