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

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