Blame SOURCES/0102-RHBZ-631009-deferred-remove.patch

4ae388
---
4ae388
 libmultipath/Makefile      |    6 +
4ae388
 libmultipath/config.c      |    3 
4ae388
 libmultipath/config.h      |    3 
4ae388
 libmultipath/configure.c   |    1 
4ae388
 libmultipath/defaults.h    |    1 
4ae388
 libmultipath/devmapper.c   |  142 ++++++++++++++++++++++++++++++++++++++++-----
4ae388
 libmultipath/devmapper.h   |   11 ++-
4ae388
 libmultipath/dict.c        |  116 ++++++++++++++++++++++++++++++++++++
4ae388
 libmultipath/propsel.c     |   28 ++++++++
4ae388
 libmultipath/propsel.h     |    1 
4ae388
 libmultipath/structs.h     |    8 ++
4ae388
 libmultipath/structs_vec.c |    3 
4ae388
 multipath/multipath.conf.5 |   14 ++++
4ae388
 multipathd/main.c          |   23 +++++--
4ae388
 14 files changed, 333 insertions(+), 27 deletions(-)
4ae388
4ae388
Index: multipath-tools-130222/libmultipath/config.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/config.c
4ae388
+++ multipath-tools-130222/libmultipath/config.c
4ae388
@@ -337,6 +337,7 @@ merge_hwe (struct hwentry * dst, struct
4ae388
 	merge_num(user_friendly_names);
4ae388
 	merge_num(retain_hwhandler);
4ae388
 	merge_num(detect_prio);
4ae388
+	merge_num(deferred_remove);
4ae388
 
4ae388
 	/*
4ae388
 	 * Make sure features is consistent with
4ae388
@@ -394,6 +395,7 @@ overwrite_hwe (struct hwentry * dst, str
4ae388
 	overwrite_num(user_friendly_names);
4ae388
 	overwrite_num(retain_hwhandler);
4ae388
 	overwrite_num(detect_prio);
4ae388
+	overwrite_num(deferred_remove);
4ae388
 
4ae388
 	/*
4ae388
 	 * Make sure features is consistent with
4ae388
@@ -617,6 +619,7 @@ load_config (char * file, struct udev *u
4ae388
 	conf->fast_io_fail = DEFAULT_FAST_IO_FAIL;
4ae388
 	conf->retain_hwhandler = DEFAULT_RETAIN_HWHANDLER;
4ae388
 	conf->detect_prio = DEFAULT_DETECT_PRIO;
4ae388
+	conf->deferred_remove = DEFAULT_DEFERRED_REMOVE;
4ae388
 	conf->hw_strmatch = 0;
4ae388
 	conf->force_sync = 0;
4ae388
 
4ae388
Index: multipath-tools-130222/libmultipath/config.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/config.h
4ae388
+++ multipath-tools-130222/libmultipath/config.h
4ae388
@@ -61,6 +61,7 @@ struct hwentry {
4ae388
 	int user_friendly_names;
4ae388
 	int retain_hwhandler;
4ae388
 	int detect_prio;
4ae388
+	int deferred_remove;
4ae388
 	char * bl_product;
4ae388
 };
4ae388
 
4ae388
@@ -84,6 +85,7 @@ struct mpentry {
4ae388
 	int flush_on_last_del;
4ae388
 	int attribute_flags;
4ae388
 	int user_friendly_names;
4ae388
+	int deferred_remove;
4ae388
 	uid_t uid;
4ae388
 	gid_t gid;
4ae388
 	mode_t mode;
4ae388
@@ -128,6 +130,7 @@ struct config {
4ae388
 	int retain_hwhandler;
4ae388
 	int detect_prio;
4ae388
 	int force_sync;
4ae388
+	int deferred_remove;
4ae388
 	unsigned int version[3];
4ae388
 
4ae388
 	char * dev;
4ae388
Index: multipath-tools-130222/libmultipath/configure.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/configure.c
4ae388
+++ multipath-tools-130222/libmultipath/configure.c
4ae388
@@ -290,6 +290,7 @@ setup_map (struct multipath * mpp, char
4ae388
 	select_dev_loss(mpp);
4ae388
 	select_reservation_key(mpp);
4ae388
 	select_retain_hwhandler(mpp);
4ae388
+	select_deferred_remove(mpp);
4ae388
 
4ae388
 	sysfs_set_scsi_tmo(mpp);
4ae388
 	/*
4ae388
Index: multipath-tools-130222/libmultipath/defaults.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/defaults.h
4ae388
+++ multipath-tools-130222/libmultipath/defaults.h
4ae388
@@ -19,6 +19,7 @@
4ae388
 #define DEFAULT_FAST_IO_FAIL	5
4ae388
 #define DEFAULT_RETAIN_HWHANDLER RETAIN_HWHANDLER_OFF
4ae388
 #define DEFAULT_DETECT_PRIO DETECT_PRIO_OFF
4ae388
+#define DEFAULT_DEFERRED_REMOVE DEFERRED_REMOVE_OFF
4ae388
 
4ae388
 #define DEFAULT_CHECKINT	5
4ae388
 #define MAX_CHECKINT(a)		(a << 2)
4ae388
Index: multipath-tools-130222/libmultipath/devmapper.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/devmapper.c
4ae388
+++ multipath-tools-130222/libmultipath/devmapper.c
4ae388
@@ -32,6 +32,8 @@
4ae388
 #define UUID_PREFIX "mpath-"
4ae388
 #define UUID_PREFIX_LEN 6
4ae388
 
4ae388
+static int dm_cancel_remove_partmaps(const char * mapname);
4ae388
+
4ae388
 #ifndef LIBDM_API_COOKIE
4ae388
 static inline int dm_task_set_cookie(struct dm_task *dmt, uint32_t *c, int a)
4ae388
 {
4ae388
@@ -103,7 +105,9 @@ dm_lib_prereq (void)
4ae388
 {
4ae388
 	char version[64];
4ae388
 	int v[3];
4ae388
-#if defined(DM_SUBSYSTEM_UDEV_FLAG0)
4ae388
+#if defined(LIBDM_API_DEFERRED)
4ae388
+	int minv[3] = {1, 2, 89};
4ae388
+#elif defined(DM_SUBSYSTEM_UDEV_FLAG0)
4ae388
 	int minv[3] = {1, 2, 82};
4ae388
 #elif defined(LIBDM_API_COOKIE)
4ae388
 	int minv[3] = {1, 2, 38};
4ae388
@@ -201,8 +205,10 @@ dm_prereq (void)
4ae388
 	return dm_drv_prereq();
4ae388
 }
4ae388
 
4ae388
+#define do_deferred(x) ((x) == DEFERRED_REMOVE_ON || (x) == DEFERRED_REMOVE_IN_PROGRESS)
4ae388
+
4ae388
 static int
4ae388
-dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t udev_flags) {
4ae388
+dm_simplecmd (int task, const char *name, int no_flush, int need_sync, uint16_t udev_flags, int deferred_remove) {
4ae388
 	int r = 0;
4ae388
 	int udev_wait_flag = (need_sync && (task == DM_DEVICE_RESUME ||
4ae388
 					    task == DM_DEVICE_REMOVE));
4ae388
@@ -220,7 +226,10 @@ dm_simplecmd (int task, const char *name
4ae388
 	if (no_flush)
4ae388
 		dm_task_no_flush(dmt);		/* for DM_DEVICE_SUSPEND/RESUME */
4ae388
 #endif
4ae388
-
4ae388
+#ifdef LIBDM_API_DEFERRED
4ae388
+	if (do_deferred(deferred_remove))
4ae388
+		dm_task_deferred_remove(dmt);
4ae388
+#endif
4ae388
 	if (udev_wait_flag && !dm_task_set_cookie(dmt, &conf->cookie, ((conf->daemon)? DM_UDEV_DISABLE_LIBRARY_FALLBACK : 0) | udev_flags))
4ae388
 		goto out;
4ae388
 	r = dm_task_run (dmt);
4ae388
@@ -232,12 +241,18 @@ dm_simplecmd (int task, const char *name
4ae388
 
4ae388
 extern int
4ae388
 dm_simplecmd_flush (int task, const char *name, int needsync, uint16_t udev_flags) {
4ae388
-	return dm_simplecmd(task, name, 0, needsync, udev_flags);
4ae388
+	return dm_simplecmd(task, name, 0, needsync, udev_flags, 0);
4ae388
 }
4ae388
 
4ae388
 extern int
4ae388
 dm_simplecmd_noflush (int task, const char *name, uint16_t udev_flags) {
4ae388
-	return dm_simplecmd(task, name, 1, 1, udev_flags);
4ae388
+	return dm_simplecmd(task, name, 1, 1, udev_flags, 0);
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
+dm_device_remove (const char *name, int needsync, int deferred_remove) {
4ae388
+	return dm_simplecmd(DM_DEVICE_REMOVE, name, 0, needsync, 0,
4ae388
+			    deferred_remove);
4ae388
 }
4ae388
 
4ae388
 extern int
4ae388
@@ -653,7 +668,7 @@ out:
4ae388
 }
4ae388
 
4ae388
 extern int
4ae388
-_dm_flush_map (const char * mapname, int need_sync)
4ae388
+_dm_flush_map (const char * mapname, int need_sync, int deferred_remove)
4ae388
 {
4ae388
 	int r;
4ae388
 
4ae388
@@ -663,23 +678,46 @@ _dm_flush_map (const char * mapname, int
4ae388
 	if (dm_type(mapname, TGT_MPATH) <= 0)
4ae388
 		return 0; /* nothing to do */
4ae388
 
4ae388
-	if (dm_remove_partmaps(mapname, need_sync))
4ae388
+	if (dm_remove_partmaps(mapname, need_sync, deferred_remove))
4ae388
 		return 1;
4ae388
 
4ae388
-	if (dm_get_opencount(mapname)) {
4ae388
+	if (!do_deferred(deferred_remove) && dm_get_opencount(mapname)) {
4ae388
 		condlog(2, "%s: map in use", mapname);
4ae388
 		return 1;
4ae388
 	}
4ae388
 
4ae388
-	r = dm_simplecmd_flush(DM_DEVICE_REMOVE, mapname, need_sync, 0);
4ae388
+	r = dm_device_remove(mapname, need_sync, deferred_remove);
4ae388
 
4ae388
 	if (r) {
4ae388
+		if (do_deferred(deferred_remove) && dm_map_present(mapname)) {
4ae388
+			condlog(4, "multipath map %s remove deferred",
4ae388
+				mapname);
4ae388
+			return 2;
4ae388
+		}
4ae388
 		condlog(4, "multipath map %s removed", mapname);
4ae388
 		return 0;
4ae388
 	}
4ae388
 	return 1;
4ae388
 }
4ae388
 
4ae388
+#ifdef LIBDM_API_DEFERRED
4ae388
+
4ae388
+int
4ae388
+dm_flush_map_nopaths(const char * mapname, int deferred_remove)
4ae388
+{
4ae388
+	return _dm_flush_map(mapname, 1, deferred_remove);
4ae388
+}
4ae388
+
4ae388
+#else
4ae388
+
4ae388
+int
4ae388
+dm_flush_map_nopaths(const char * mapname, int deferred_remove)
4ae388
+{
4ae388
+	return _dm_flush_map(mapname, 1, 0);
4ae388
+}
4ae388
+
4ae388
+#endif
4ae388
+
4ae388
 extern int
4ae388
 dm_suspend_and_flush_map (const char * mapname)
4ae388
 {
4ae388
@@ -1076,6 +1114,7 @@ out:
4ae388
 
4ae388
 struct remove_data {
4ae388
 	int need_sync;
4ae388
+	int deferred_remove;
4ae388
 };
4ae388
 
4ae388
 static int
4ae388
@@ -1084,25 +1123,98 @@ remove_partmap(char *name, void *data)
4ae388
 	struct remove_data *rd = (struct remove_data *)data;
4ae388
 
4ae388
 	if (dm_get_opencount(name)) {
4ae388
-		dm_remove_partmaps(name, rd->need_sync);
4ae388
-		if (dm_get_opencount(name)) {
4ae388
+		dm_remove_partmaps(name, rd->need_sync, rd->deferred_remove);
4ae388
+		if (!do_deferred(rd->deferred_remove) &&
4ae388
+		    dm_get_opencount(name)) {
4ae388
 			condlog(2, "%s: map in use", name);
4ae388
 			return 1;
4ae388
 		}
4ae388
 	}
4ae388
 	condlog(4, "partition map %s removed", name);
4ae388
-	dm_simplecmd_flush(DM_DEVICE_REMOVE, name,
4ae388
-			   rd->need_sync, 0);
4ae388
+	dm_device_remove(name, rd->need_sync, rd->deferred_remove);
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
 int
4ae388
-dm_remove_partmaps (const char * mapname, int need_sync)
4ae388
+dm_remove_partmaps (const char * mapname, int need_sync, int deferred_remove)
4ae388
 {
4ae388
-	struct remove_data rd = { need_sync };
4ae388
+	struct remove_data rd = { need_sync, deferred_remove };
4ae388
 	return do_foreach_partmaps(mapname, remove_partmap, &rd);
4ae388
 }
4ae388
 
4ae388
+#ifdef LIBDM_API_DEFERRED
4ae388
+
4ae388
+static int
4ae388
+cancel_remove_partmap (char *name, void *unused)
4ae388
+{
4ae388
+	if (dm_get_opencount(name))
4ae388
+		dm_cancel_remove_partmaps(name);
4ae388
+	if (dm_message(name, "@cancel_deferred_remove") != 0)
4ae388
+		condlog(0, "%s: can't cancel deferred remove: %s", name,
4ae388
+			strerror(errno));
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
+dm_get_deferred_remove (char * mapname)
4ae388
+{
4ae388
+	int r = -1;
4ae388
+	struct dm_task *dmt;
4ae388
+	struct dm_info info;
4ae388
+
4ae388
+	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
4ae388
+		return -1;
4ae388
+
4ae388
+	if (!dm_task_set_name(dmt, mapname))
4ae388
+		goto out;
4ae388
+
4ae388
+	if (!dm_task_run(dmt))
4ae388
+		goto out;
4ae388
+
4ae388
+	if (!dm_task_get_info(dmt, &info))
4ae388
+		goto out;
4ae388
+
4ae388
+	r = info.deferred_remove;
4ae388
+out:
4ae388
+	dm_task_destroy(dmt);
4ae388
+	return r;
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
+dm_cancel_remove_partmaps(const char * mapname) {
4ae388
+	return do_foreach_partmaps(mapname, cancel_remove_partmap, NULL);
4ae388
+}
4ae388
+
4ae388
+int
4ae388
+dm_cancel_deferred_remove (struct multipath *mpp)
4ae388
+{
4ae388
+	int r = 0;
4ae388
+
4ae388
+	if (!dm_get_deferred_remove(mpp->alias))
4ae388
+		return 0;
4ae388
+	if (mpp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS)
4ae388
+		mpp->deferred_remove = DEFERRED_REMOVE_ON;
4ae388
+
4ae388
+	dm_cancel_remove_partmaps(mpp->alias);
4ae388
+	r = dm_message(mpp->alias, "@cancel_deferred_remove");
4ae388
+	if (r)
4ae388
+		condlog(0, "%s: can't cancel deferred remove: %s", mpp->alias,
4ae388
+			strerror(errno));
4ae388
+	else
4ae388
+		condlog(2, "%s: canceled deferred remove", mpp->alias);
4ae388
+	return r;
4ae388
+}
4ae388
+
4ae388
+#else
4ae388
+
4ae388
+int
4ae388
+dm_cancel_deferred_remove (struct multipath *mpp)
4ae388
+{
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+#endif
4ae388
+
4ae388
 static struct dm_info *
4ae388
 alloc_dminfo (void)
4ae388
 {
4ae388
Index: multipath-tools-130222/libmultipath/devmapper.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/devmapper.h
4ae388
+++ multipath-tools-130222/libmultipath/devmapper.h
4ae388
@@ -23,9 +23,11 @@ int dm_map_present (const char *);
4ae388
 int dm_get_map(const char *, unsigned long long *, char *);
4ae388
 int dm_get_status(char *, char *);
4ae388
 int dm_type(const char *, char *);
4ae388
-int _dm_flush_map (const char *, int);
4ae388
-#define dm_flush_map(mapname) _dm_flush_map(mapname, 1)
4ae388
-#define dm_flush_map_nosync(mapname) _dm_flush_map(mapname, 0)
4ae388
+int _dm_flush_map (const char *, int, int);
4ae388
+int dm_flush_map_nopaths(const char * mapname, int deferred_remove);
4ae388
+#define dm_flush_map(mapname) _dm_flush_map(mapname, 1, 0)
4ae388
+#define dm_flush_map_nosync(mapname) _dm_flush_map(mapname, 0, 0)
4ae388
+int dm_cancel_deferred_remove(struct multipath *mpp);
4ae388
 int dm_suspend_and_flush_map(const char * mapname);
4ae388
 int dm_flush_maps (void);
4ae388
 int dm_fail_path(char * mapname, char * path);
4ae388
@@ -40,7 +42,8 @@ int dm_geteventnr (char *name);
4ae388
 int dm_get_major (char *name);
4ae388
 int dm_get_minor (char *name);
4ae388
 char * dm_mapname(int major, int minor);
4ae388
-int dm_remove_partmaps (const char * mapname, int need_sync);
4ae388
+int dm_remove_partmaps (const char * mapname, int need_sync,
4ae388
+			int deferred_remove);
4ae388
 int dm_get_uuid(char *name, char *uuid);
4ae388
 int dm_get_info (char * mapname, struct dm_info ** dmi);
4ae388
 int dm_rename (char * old, char * new);
4ae388
Index: multipath-tools-130222/libmultipath/dict.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/dict.c
4ae388
+++ multipath-tools-130222/libmultipath/dict.c
4ae388
@@ -738,6 +738,29 @@ def_force_sync_handler(vector strvec)
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
+static int
4ae388
+def_deferred_remove_handler(vector strvec)
4ae388
+{
4ae388
+	char * buff;
4ae388
+
4ae388
+	buff = set_value(strvec);
4ae388
+
4ae388
+	if (!buff)
4ae388
+		return 1;
4ae388
+
4ae388
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
4ae388
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
4ae388
+		conf->deferred_remove = DEFERRED_REMOVE_OFF;
4ae388
+	else if ((strlen(buff) == 3 && !strcmp(buff, "yes")) ||
4ae388
+		 (strlen(buff) == 1 && !strcmp(buff, "1")))
4ae388
+		conf->deferred_remove = DEFERRED_REMOVE_ON;
4ae388
+	else
4ae388
+		conf->deferred_remove = DEFAULT_DEFERRED_REMOVE;
4ae388
+
4ae388
+	FREE(buff);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
 /*
4ae388
  * blacklist block handlers
4ae388
  */
4ae388
@@ -1445,6 +1468,33 @@ hw_detect_prio_handler(vector strvec)
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
+static int
4ae388
+hw_deferred_remove_handler(vector strvec)
4ae388
+{
4ae388
+	struct hwentry *hwe = VECTOR_LAST_SLOT(conf->hwtable);
4ae388
+	char * buff;
4ae388
+
4ae388
+	if (!hwe)
4ae388
+		return 1;
4ae388
+
4ae388
+	buff = set_value(strvec);
4ae388
+
4ae388
+	if (!buff)
4ae388
+		return 1;
4ae388
+
4ae388
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
4ae388
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
4ae388
+		hwe->deferred_remove = DEFERRED_REMOVE_OFF;
4ae388
+	else if ((strlen(buff) == 3 && !strcmp(buff, "yes")) ||
4ae388
+		 (strlen(buff) == 1 && !strcmp(buff, "1")))
4ae388
+		hwe->deferred_remove = DEFERRED_REMOVE_ON;
4ae388
+	else
4ae388
+		hwe->deferred_remove = DEFERRED_REMOVE_UNDEF;
4ae388
+
4ae388
+	FREE(buff);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
 /*
4ae388
  * multipaths block handlers
4ae388
  */
4ae388
@@ -1920,6 +1970,32 @@ mp_names_handler(vector strvec)
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
+static int
4ae388
+mp_deferred_remove_handler(vector strvec)
4ae388
+{
4ae388
+	struct mpentry *mpe = VECTOR_LAST_SLOT(conf->mptable);
4ae388
+	char * buff;
4ae388
+
4ae388
+	if (!mpe)
4ae388
+		return 1;
4ae388
+
4ae388
+	buff = set_value(strvec);
4ae388
+	if (!buff)
4ae388
+		return 1;
4ae388
+
4ae388
+	if ((strlen(buff) == 2 && strcmp(buff, "no") == 0) ||
4ae388
+	    (strlen(buff) == 1 && strcmp(buff, "0") == 0))
4ae388
+		mpe->deferred_remove = DEFERRED_REMOVE_OFF;
4ae388
+	else if ((strlen(buff) == 3 && strcmp(buff, "yes") == 0) ||
4ae388
+		 (strlen(buff) == 1 && strcmp(buff, "1") == 0))
4ae388
+		mpe->deferred_remove = DEFERRED_REMOVE_ON;
4ae388
+	else
4ae388
+		mpe->deferred_remove = DEFERRED_REMOVE_UNDEF;
4ae388
+
4ae388
+	FREE(buff);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
 /*
4ae388
  * config file keywords printing
4ae388
  */
4ae388
@@ -2165,7 +2241,7 @@ snprint_mp_reservation_key (char * buff,
4ae388
 	return snprintf(buff, len, "0x%" PRIx64, prkey);
4ae388
 }
4ae388
 
4ae388
-	static int
4ae388
+static int
4ae388
 snprint_mp_user_friendly_names (char * buff, int len, void * data)
4ae388
 {
4ae388
 	struct mpentry * mpe = (struct mpentry *)data;
4ae388
@@ -2179,6 +2255,19 @@ snprint_mp_user_friendly_names (char * b
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
+snprint_mp_deferred_remove (char * buff, int len, void * data)
4ae388
+{
4ae388
+	struct mpentry * mpe = (struct mpentry *)data;
4ae388
+
4ae388
+	if (mpe->deferred_remove == DEFERRED_REMOVE_UNDEF)
4ae388
+		return 0;
4ae388
+	else if (mpe->deferred_remove == DEFERRED_REMOVE_OFF)
4ae388
+		return snprintf(buff, len, "no");
4ae388
+	else
4ae388
+		return snprintf(buff, len, "yes");
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
 snprint_hw_fast_io_fail(char * buff, int len, void * data)
4ae388
 {
4ae388
 	struct hwentry * hwe = (struct hwentry *)data;
4ae388
@@ -2507,6 +2596,19 @@ snprint_hw_retain_hwhandler_handler(char
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
+snprint_hw_deferred_remove(char * buff, int len, void * data)
4ae388
+{
4ae388
+	struct hwentry * hwe = (struct hwentry *)data;
4ae388
+
4ae388
+	if (hwe->deferred_remove == DEFERRED_REMOVE_ON)
4ae388
+		return snprintf(buff, len, "yes");
4ae388
+	else if (hwe->deferred_remove == DEFERRED_REMOVE_OFF)
4ae388
+		return snprintf(buff, len, "no");
4ae388
+	else
4ae388
+		return 0;
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
 snprint_detect_prio(char * buff, int len, void * data)
4ae388
 {
4ae388
 	struct hwentry * hwe = (struct hwentry *)data;
4ae388
@@ -2900,6 +3002,15 @@ snprint_def_force_sync(char * buff, int
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
+snprint_def_deferred_remove(char * buff, int len, void * data)
4ae388
+{
4ae388
+	if (conf->deferred_remove == DEFERRED_REMOVE_ON)
4ae388
+		return snprintf(buff, len, "yes");
4ae388
+	else
4ae388
+		return snprintf(buff, len, "no");
4ae388
+}
4ae388
+
4ae388
+static int
4ae388
 snprint_ble_simple (char * buff, int len, void * data)
4ae388
 {
4ae388
 	struct blentry * ble = (struct blentry *)data;
4ae388
@@ -2968,6 +3079,7 @@ init_keywords(void)
4ae388
 	install_keyword("detect_prio", &def_detect_prio_handler, &snprint_def_detect_prio);
4ae388
 	install_keyword("hw_str_match", &def_hw_strmatch_handler, &snprint_def_hw_strmatch);
4ae388
 	install_keyword("force_sync", &def_force_sync_handler, &snprint_def_force_sync);
4ae388
+	install_keyword("deferred_remove", &def_deferred_remove_handler, &snprint_def_deferred_remove);
4ae388
 	__deprecated install_keyword("default_selector", &def_selector_handler, NULL);
4ae388
 	__deprecated install_keyword("default_path_grouping_policy", &def_pgpolicy_handler, NULL);
4ae388
 	__deprecated install_keyword("default_uid_attribute", &def_uid_attribute_handler, NULL);
4ae388
@@ -3032,6 +3144,7 @@ init_keywords(void)
4ae388
 	install_keyword("user_friendly_names", &hw_names_handler, &snprint_hw_user_friendly_names);
4ae388
 	install_keyword("retain_attached_hw_handler", &hw_retain_hwhandler_handler, &snprint_hw_retain_hwhandler_handler);
4ae388
 	install_keyword("detect_prio", &hw_detect_prio_handler, &snprint_detect_prio);
4ae388
+	install_keyword("deferred_remove", &hw_deferred_remove_handler, &snprint_hw_deferred_remove);
4ae388
 	install_sublevel_end();
4ae388
 
4ae388
 	install_keyword_root("multipaths", &multipaths_handler);
4ae388
@@ -3056,5 +3169,6 @@ init_keywords(void)
4ae388
 	install_keyword("gid", &mp_gid_handler, &snprint_mp_gid);
4ae388
 	install_keyword("reservation_key", &mp_reservation_key_handler, &snprint_mp_reservation_key);
4ae388
 	install_keyword("user_friendly_names", &mp_names_handler, &snprint_mp_user_friendly_names);
4ae388
+	install_keyword("deferred_remove", &mp_deferred_remove_handler, &snprint_mp_deferred_remove);
4ae388
 	install_sublevel_end();
4ae388
 }
4ae388
Index: multipath-tools-130222/libmultipath/propsel.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/propsel.c
4ae388
+++ multipath-tools-130222/libmultipath/propsel.c
4ae388
@@ -744,6 +744,34 @@ select_retain_hwhandler (struct multipat
4ae388
 }
4ae388
 
4ae388
 extern int
4ae388
+select_deferred_remove (struct multipath *mp)
4ae388
+{
4ae388
+	if (mp->deferred_remove == DEFERRED_REMOVE_IN_PROGRESS) {
4ae388
+		condlog(3, "%s: deferred_remove in progress", mp->alias);
4ae388
+		return 0;
4ae388
+	}
4ae388
+	if (mp->mpe && mp->mpe->deferred_remove) {
4ae388
+		mp->deferred_remove = mp->mpe->deferred_remove;
4ae388
+		condlog(3, "%s: deferred_remove = %i (multipath setting)",
4ae388
+			mp->alias, mp->deferred_remove);
4ae388
+		return 0;
4ae388
+	}
4ae388
+	if (mp->hwe && mp->hwe->deferred_remove) {
4ae388
+		mp->deferred_remove = mp->hwe->deferred_remove;
4ae388
+		condlog(3, "%s: deferred_remove = %d (controller default)", mp->alias, mp->deferred_remove);
4ae388
+		return 0;
4ae388
+	}
4ae388
+	if (conf->deferred_remove) {
4ae388
+		mp->deferred_remove = conf->deferred_remove;
4ae388
+		condlog(3, "%s: deferred_remove = %d (config file default)", mp->alias, mp->deferred_remove);
4ae388
+		return 0;
4ae388
+	}
4ae388
+	mp->deferred_remove = DEFAULT_DEFERRED_REMOVE;
4ae388
+	condlog(3, "%s: deferred_remove = %d (compiled in default)", mp->alias, mp->deferred_remove);
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+extern int
4ae388
 select_detect_prio (struct path * pp)
4ae388
 {
4ae388
 	if (pp->hwe && pp->hwe->detect_prio) {
4ae388
Index: multipath-tools-130222/libmultipath/propsel.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/propsel.h
4ae388
+++ multipath-tools-130222/libmultipath/propsel.h
4ae388
@@ -20,3 +20,4 @@ int select_dev_loss(struct multipath *mp
4ae388
 int select_reservation_key(struct multipath *mp);
4ae388
 int select_retain_hwhandler (struct multipath * mp);
4ae388
 int select_detect_prio(struct path * pp);
4ae388
+int select_deferred_remove(struct multipath *mp);
4ae388
Index: multipath-tools-130222/libmultipath/structs.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/structs.h
4ae388
+++ multipath-tools-130222/libmultipath/structs.h
4ae388
@@ -114,6 +114,13 @@ enum detect_prio_states {
4ae388
 	DETECT_PRIO_ON,
4ae388
 };
4ae388
 
4ae388
+enum deferred_remove_states {
4ae388
+	DEFERRED_REMOVE_UNDEF,
4ae388
+	DEFERRED_REMOVE_OFF,
4ae388
+	DEFERRED_REMOVE_ON,
4ae388
+	DEFERRED_REMOVE_IN_PROGRESS,
4ae388
+};
4ae388
+
4ae388
 enum scsi_protocol {
4ae388
 	SCSI_PROTOCOL_FCP = 0,	/* Fibre Channel */
4ae388
 	SCSI_PROTOCOL_SPI = 1,	/* parallel SCSI */
4ae388
@@ -207,6 +214,7 @@ struct multipath {
4ae388
 	int attribute_flags;
4ae388
 	int fast_io_fail;
4ae388
 	int retain_hwhandler;
4ae388
+	int deferred_remove;
4ae388
 	unsigned int dev_loss;
4ae388
 	uid_t uid;
4ae388
 	gid_t gid;
4ae388
Index: multipath-tools-130222/multipathd/main.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipathd/main.c
4ae388
+++ multipath-tools-130222/multipathd/main.c
4ae388
@@ -214,19 +214,30 @@ sync_maps_state(vector mpvec)
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
-flush_map(struct multipath * mpp, struct vectors * vecs)
4ae388
+flush_map(struct multipath * mpp, struct vectors * vecs, int nopaths)
4ae388
 {
4ae388
+	int r;
4ae388
+
4ae388
+	if (nopaths)
4ae388
+		r = dm_flush_map_nopaths(mpp->alias, mpp->deferred_remove);
4ae388
+	else
4ae388
+		r = dm_flush_map(mpp->alias);
4ae388
 	/*
4ae388
 	 * clear references to this map before flushing so we can ignore
4ae388
 	 * the spurious uevent we may generate with the dm_flush_map call below
4ae388
 	 */
4ae388
-	if (dm_flush_map(mpp->alias)) {
4ae388
+	if (r) {
4ae388
 		/*
4ae388
 		 * May not really be an error -- if the map was already flushed
4ae388
 		 * from the device mapper by dmsetup(8) for instance.
4ae388
 		 */
4ae388
-		condlog(0, "%s: can't flush", mpp->alias);
4ae388
-		return 1;
4ae388
+		if (r == 1)
4ae388
+			condlog(0, "%s: can't flush", mpp->alias);
4ae388
+		else {
4ae388
+			condlog(2, "%s: devmap deferred remove", mpp->alias);
4ae388
+			mpp->deferred_remove = DEFERRED_REMOVE_IN_PROGRESS;
4ae388
+		}
4ae388
+		return r;
4ae388
 	}
4ae388
 	else {
4ae388
 		dm_lib_release();
4ae388
@@ -372,7 +383,7 @@ ev_remove_map (char * devname, char * al
4ae388
 			mpp->alias, mpp->dmi->minor, minor);
4ae388
 		return 0;
4ae388
 	}
4ae388
-	return flush_map(mpp, vecs);
4ae388
+	return flush_map(mpp, vecs, 0);
4ae388
 }
4ae388
 
4ae388
 static int
4ae388
@@ -628,7 +639,7 @@ ev_remove_path (struct path *pp, struct
4ae388
 				mpp->flush_on_last_del = FLUSH_IN_PROGRESS;
4ae388
 				dm_queue_if_no_path(mpp->alias, 0);
4ae388
 			}
4ae388
-			if (!flush_map(mpp, vecs)) {
4ae388
+			if (!flush_map(mpp, vecs, 1)) {
4ae388
 				condlog(2, "%s: removed map after"
4ae388
 					" removing all paths",
4ae388
 					alias);
4ae388
Index: multipath-tools-130222/libmultipath/structs_vec.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/structs_vec.c
4ae388
+++ multipath-tools-130222/libmultipath/structs_vec.c
4ae388
@@ -392,6 +392,8 @@ __setup_multipath (struct vectors * vecs
4ae388
 		set_no_path_retry(mpp);
4ae388
 		select_pg_timeout(mpp);
4ae388
 		select_flush_on_last_del(mpp);
4ae388
+		if (VECTOR_SIZE(mpp->paths) != 0)
4ae388
+			dm_cancel_deferred_remove(mpp);
4ae388
 	}
4ae388
 
4ae388
 	return 0;
4ae388
@@ -565,7 +567,6 @@ int update_multipath (struct vectors *ve
4ae388
 			}
4ae388
 		}
4ae388
 	}
4ae388
-
4ae388
 	return 0;
4ae388
 }
4ae388
 
4ae388
Index: multipath-tools-130222/multipath/multipath.conf.5
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipath/multipath.conf.5
4ae388
+++ multipath-tools-130222/multipath/multipath.conf.5
4ae388
@@ -420,6 +420,16 @@ only one checker will run at a time.  Th
4ae388
 multipathd checkers running in parallel causes significant CPU pressure. The
4ae388
 Default is
4ae388
 .I no
4ae388
+.TP
4ae388
+.B deferred_remove
4ae388
+If set to
4ae388
+.I yes
4ae388
+, multipathd will do a deferred remove instead of a regular remove when the
4ae388
+last path device has been deleted.  This means that if the multipath device is
4ae388
+still in use, it will be freed when the last user closes it.  If path is added
4ae388
+to the multipath device before the last user closes it, the deferred remove
4ae388
+will be canceled. Default is
4ae388
+.I no
4ae388
 .
4ae388
 .SH "blacklist section"
4ae388
 The
4ae388
@@ -521,6 +531,8 @@ section:
4ae388
 .B features
4ae388
 .TP
4ae388
 .B reservation_key
4ae388
+.TP
4ae388
+.B deferred_remove
4ae388
 .RE
4ae388
 .PD
4ae388
 .LP
4ae388
@@ -611,6 +623,8 @@ section:
4ae388
 .B retain_attached_hw_handler
4ae388
 .TP
4ae388
 .B detect_prio
4ae388
+.TP
4ae388
+.B deferred_remove
4ae388
 .RE
4ae388
 .PD
4ae388
 .LP
4ae388
Index: multipath-tools-130222/libmultipath/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/Makefile
4ae388
+++ multipath-tools-130222/libmultipath/Makefile
4ae388
@@ -36,6 +36,12 @@ ifneq ($(strip $(LIBUDEV_API_RECVBUF)),0
4ae388
 	CFLAGS += -DLIBUDEV_API_RECVBUF
4ae388
 endif
4ae388
 
4ae388
+LIBDM_API_DEFERRED = $(shell grep -Ecs '^[a-z]*[[:space:]]+dm_task_deferred_remove' /usr/include/libdevmapper.h)
4ae388
+
4ae388
+ifneq ($(strip $(LIBDM_API_DEFERRED)),0)
4ae388
+	CFLAGS += -DLIBDM_API_DEFERRED
4ae388
+endif
4ae388
+
4ae388
 
4ae388
 all: $(LIBS)
4ae388