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

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