a385ba
---
a385ba
 libmultipath/Makefile      |    7 
a385ba
 libmultipath/config.h      |   12 
a385ba
 libmultipath/configure.c   |   18 -
a385ba
 libmultipath/configure.h   |    3 
a385ba
 libmultipath/defaults.h    |    1 
a385ba
 libmultipath/dict.c        |  410 ++++++++++++++++++++++++
a385ba
 libmultipath/io_err_stat.c |  763 +++++++++++++++++++++++++++++++++++++++++++++
a385ba
 libmultipath/io_err_stat.h |   15 
a385ba
 libmultipath/propsel.c     |   98 +++++
a385ba
 libmultipath/propsel.h     |    4 
a385ba
 libmultipath/structs.h     |   14 
a385ba
 libmultipath/time-util.c   |   42 ++
a385ba
 libmultipath/time-util.h   |   13 
a385ba
 libmultipath/uevent.c      |   38 ++
a385ba
 libmultipath/uevent.h      |    2 
a385ba
 multipath/multipath.conf.5 |  108 ++++++
a385ba
 multipathd/cli_handlers.c  |    2 
a385ba
 multipathd/main.c          |   64 +++
a385ba
 18 files changed, 1599 insertions(+), 15 deletions(-)
a385ba
a385ba
Index: multipath-tools-130222/libmultipath/Makefile
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/Makefile
a385ba
+++ multipath-tools-130222/libmultipath/Makefile
a385ba
@@ -7,16 +7,17 @@ include ../Makefile.inc
a385ba
 SONAME=0
a385ba
 DEVLIB = libmultipath.so
a385ba
 LIBS = $(DEVLIB).$(SONAME)
a385ba
-LIBDEPS = -lpthread -ldl -ldevmapper -ludev -L$(mpathcmddir) -lmpathcmd
a385ba
+LIBDEPS = -lpthread -ldl -ldevmapper -ludev -L$(mpathcmddir) -lmpathcmd -laio
a385ba
 CFLAGS += -fPIC -I$(mpathcmddir) -I$(mpathpersistdir)
a385ba
 
a385ba
 OBJS = memory.o parser.o vector.o devmapper.o \
a385ba
        hwtable.o blacklist.o util.o dmparser.o config.o \
a385ba
        structs.o discovery.o propsel.o dict.o \
a385ba
-       pgpolicies.o debug.o regex.o defaults.o uevent.o \
a385ba
+       pgpolicies.o debug.o regex.o defaults.o uevent.o time-util.o \
a385ba
        switchgroup.o uxsock.o print.o alias.o log_pthread.o \
a385ba
        log.o configure.o structs_vec.o sysfs.o prio.o checkers.o \
a385ba
-       lock.o waiter.o file.o wwids.o prioritizers/alua_rtpg.o prkey.o
a385ba
+       lock.o waiter.o file.o wwids.o prioritizers/alua_rtpg.o prkey.o \
a385ba
+       io_err_stat.o
a385ba
 
a385ba
 LIBDM_API_FLUSH = $(shell grep -Ecs '^[a-z]*[[:space:]]+dm_task_no_flush' /usr/include/libdevmapper.h)
a385ba
 
a385ba
Index: multipath-tools-130222/libmultipath/config.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/config.h
a385ba
+++ multipath-tools-130222/libmultipath/config.h
a385ba
@@ -67,6 +67,10 @@ struct hwentry {
a385ba
 	int deferred_remove;
a385ba
 	int delay_watch_checks;
a385ba
 	int delay_wait_checks;
a385ba
+	int marginal_path_err_sample_time;
a385ba
+	int marginal_path_err_rate_threshold;
a385ba
+	int marginal_path_err_recheck_gap_time;
a385ba
+	int marginal_path_double_failed_time;
a385ba
 	int skip_kpartx;
a385ba
 	int max_sectors_kb;
a385ba
 	int unpriv_sgio;
a385ba
@@ -100,6 +104,10 @@ struct mpentry {
a385ba
 	int deferred_remove;
a385ba
 	int delay_watch_checks;
a385ba
 	int delay_wait_checks;
a385ba
+	int marginal_path_err_sample_time;
a385ba
+	int marginal_path_err_rate_threshold;
a385ba
+	int marginal_path_err_recheck_gap_time;
a385ba
+	int marginal_path_double_failed_time;
a385ba
 	int skip_kpartx;
a385ba
 	int max_sectors_kb;
a385ba
 	int unpriv_sgio;
a385ba
@@ -153,6 +161,10 @@ struct config {
a385ba
 	int processed_main_config;
a385ba
 	int delay_watch_checks;
a385ba
 	int delay_wait_checks;
a385ba
+	int marginal_path_err_sample_time;
a385ba
+	int marginal_path_err_rate_threshold;
a385ba
+	int marginal_path_err_recheck_gap_time;
a385ba
+	int marginal_path_double_failed_time;
a385ba
 	int retrigger_tries;
a385ba
 	int retrigger_delay;
a385ba
 	int new_bindings_in_boot;
a385ba
Index: multipath-tools-130222/libmultipath/configure.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/configure.c
a385ba
+++ multipath-tools-130222/libmultipath/configure.c
a385ba
@@ -42,6 +42,7 @@
a385ba
 #include "uxsock.h"
a385ba
 #include "wwids.h"
a385ba
 #include "sysfs.h"
a385ba
+#include "io_err_stat.h"
a385ba
 
a385ba
 /* group paths in pg by host adapter
a385ba
  */
a385ba
@@ -257,7 +258,8 @@ int rr_optimize_path_order(struct pathgr
a385ba
 }
a385ba
 
a385ba
 extern int
a385ba
-setup_map (struct multipath * mpp, char * params, int params_size)
a385ba
+setup_map (struct multipath * mpp, char * params, int params_size,
a385ba
+	   struct vectors *vecs)
a385ba
 {
a385ba
 	struct pathgroup * pgp;
a385ba
 	int i, old_nr_active;
a385ba
@@ -297,11 +299,21 @@ setup_map (struct multipath * mpp, char
a385ba
 	select_deferred_remove(mpp);
a385ba
 	select_delay_watch_checks(mpp);
a385ba
 	select_delay_wait_checks(mpp);
a385ba
+	select_marginal_path_err_sample_time(mpp);
a385ba
+	select_marginal_path_err_rate_threshold(mpp);
a385ba
+	select_marginal_path_err_recheck_gap_time(mpp);
a385ba
+	select_marginal_path_double_failed_time(mpp);
a385ba
 	select_skip_kpartx(mpp);
a385ba
 	select_max_sectors_kb(mpp);
a385ba
 	select_unpriv_sgio(mpp);
a385ba
 
a385ba
 	sysfs_set_scsi_tmo(mpp);
a385ba
+
a385ba
+	if (mpp->marginal_path_double_failed_time > 0 &&
a385ba
+	    mpp->marginal_path_err_sample_time > 0 &&
a385ba
+	    mpp->marginal_path_err_recheck_gap_time > 0 &&
a385ba
+	    mpp->marginal_path_err_rate_threshold >= 0)
a385ba
+		start_io_err_stat_thread(vecs);
a385ba
 	/*
a385ba
 	 * assign paths to path groups -- start with no groups and all paths
a385ba
 	 * in mpp->paths
a385ba
@@ -867,7 +879,7 @@ coalesce_paths (struct vectors * vecs, v
a385ba
 		verify_paths(mpp, vecs, NULL);
a385ba
 
a385ba
 		params[0] = '\0';
a385ba
-		if (setup_map(mpp, params, PARAMS_SIZE)) {
a385ba
+		if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
a385ba
 			remove_map(mpp, vecs, 0);
a385ba
 			continue;
a385ba
 		}
a385ba
@@ -1118,7 +1130,7 @@ extern int reload_map(struct vectors *ve
a385ba
 		vector_foreach_slot (mpp->paths, pp, i)
a385ba
 			pathinfo(pp, conf->hwtable, DI_PRIO);
a385ba
 	}
a385ba
-	if (setup_map(mpp, params, PARAMS_SIZE)) {
a385ba
+	if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
a385ba
 		condlog(0, "%s: failed to setup map", mpp->alias);
a385ba
 		return 1;
a385ba
 	}
a385ba
Index: multipath-tools-130222/libmultipath/configure.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/configure.h
a385ba
+++ multipath-tools-130222/libmultipath/configure.h
a385ba
@@ -24,7 +24,8 @@ enum actions {
a385ba
 #define FLUSH_ONE 1
a385ba
 #define FLUSH_ALL 2
a385ba
 
a385ba
-int setup_map (struct multipath * mpp, char * params, int params_size );
a385ba
+int setup_map (struct multipath * mpp, char * params, int params_size,
a385ba
+	       struct vectors *vecs);
a385ba
 int domap (struct multipath * mpp, char * params);
a385ba
 int reinstate_paths (struct multipath *mpp);
a385ba
 int check_daemon(void);
a385ba
Index: multipath-tools-130222/libmultipath/defaults.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/defaults.h
a385ba
+++ multipath-tools-130222/libmultipath/defaults.h
a385ba
@@ -22,6 +22,7 @@
a385ba
 #define DEFAULT_DETECT_CHECKER DETECT_CHECKER_OFF
a385ba
 #define DEFAULT_DEFERRED_REMOVE DEFERRED_REMOVE_OFF
a385ba
 #define DEFAULT_DELAY_CHECKS DELAY_CHECKS_OFF
a385ba
+#define DEFAULT_MARGINAL_PATH MARGINAL_PATH_OFF
a385ba
 #define DEFAULT_RETRIGGER_DELAY 10
a385ba
 #define DEFAULT_RETRIGGER_TRIES 3
a385ba
 #define DEFAULT_UEV_WAIT_TIMEOUT 30
a385ba
Index: multipath-tools-130222/libmultipath/dict.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/dict.c
a385ba
+++ multipath-tools-130222/libmultipath/dict.c
a385ba
@@ -1077,6 +1077,81 @@ def_all_tg_pt_handler(vector strvec)
a385ba
         return 0;
a385ba
 }
a385ba
 
a385ba
+static int
a385ba
+def_marginal_path_err_sample_time_handler(vector strvec)
a385ba
+{
a385ba
+	char * buff;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		conf->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((conf->marginal_path_err_sample_time = atoi(buff)) < 1)
a385ba
+		conf->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+def_marginal_path_err_rate_threshold_handler(vector strvec)
a385ba
+{
a385ba
+	char * buff;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		conf->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+	else if ((conf->marginal_path_err_rate_threshold = atoi(buff)) < 1)
a385ba
+		conf->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+def_marginal_path_err_recheck_gap_time_handler(vector strvec)
a385ba
+{
a385ba
+	char * buff;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		conf->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((conf->marginal_path_err_recheck_gap_time = atoi(buff)) < 1)
a385ba
+		conf->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+def_marginal_path_double_failed_time_handler(vector strvec)
a385ba
+{
a385ba
+	char * buff;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		conf->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((conf->marginal_path_double_failed_time = atoi(buff)) < 1)
a385ba
+		conf->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
 
a385ba
 /*
a385ba
  * blacklist block handlers
a385ba
@@ -2055,6 +2130,98 @@ hw_all_tg_pt_handler(vector strvec)
a385ba
 	return 0;
a385ba
 }
a385ba
 
a385ba
+static int
a385ba
+hw_marginal_path_err_sample_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct hwentry *hwe = VECTOR_LAST_SLOT(conf->hwtable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!hwe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		hwe->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((hwe->marginal_path_err_sample_time = atoi(buff)) < 1)
a385ba
+		hwe->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+hw_marginal_path_err_rate_threshold_handler(vector strvec)
a385ba
+{
a385ba
+	struct hwentry *hwe = VECTOR_LAST_SLOT(conf->hwtable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!hwe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		hwe->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+	else if ((hwe->marginal_path_err_rate_threshold = atoi(buff)) < 1)
a385ba
+		hwe->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+hw_marginal_path_err_recheck_gap_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct hwentry *hwe = VECTOR_LAST_SLOT(conf->hwtable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!hwe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		hwe->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((hwe->marginal_path_err_recheck_gap_time = atoi(buff)) < 1)
a385ba
+		hwe->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+hw_marginal_path_double_failed_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct hwentry *hwe = VECTOR_LAST_SLOT(conf->hwtable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!hwe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		hwe->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((hwe->marginal_path_double_failed_time = atoi(buff)) < 1)
a385ba
+		hwe->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
 /*
a385ba
  * multipaths block handlers
a385ba
  */
a385ba
@@ -2659,6 +2826,98 @@ mp_ghost_delay_handler(vector strvec)
a385ba
 	return 0;
a385ba
 }
a385ba
 
a385ba
+static int
a385ba
+mp_marginal_path_err_sample_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct mpentry *mpe = VECTOR_LAST_SLOT(conf->mptable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!mpe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		mpe->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((mpe->marginal_path_err_sample_time = atoi(buff)) < 1)
a385ba
+		mpe->marginal_path_err_sample_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+mp_marginal_path_err_rate_threshold_handler(vector strvec)
a385ba
+{
a385ba
+	struct mpentry *mpe = VECTOR_LAST_SLOT(conf->mptable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!mpe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		mpe->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+	else if ((mpe->marginal_path_err_rate_threshold = atoi(buff)) < 1)
a385ba
+		mpe->marginal_path_err_rate_threshold = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+mp_marginal_path_err_recheck_gap_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct mpentry *mpe = VECTOR_LAST_SLOT(conf->mptable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!mpe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		mpe->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((mpe->marginal_path_err_recheck_gap_time = atoi(buff)) < 1)
a385ba
+		mpe->marginal_path_err_recheck_gap_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+mp_marginal_path_double_failed_time_handler(vector strvec)
a385ba
+{
a385ba
+	struct mpentry *mpe = VECTOR_LAST_SLOT(conf->mptable);
a385ba
+	char * buff;
a385ba
+
a385ba
+	if (!mpe)
a385ba
+		return 1;
a385ba
+
a385ba
+	buff = set_value(strvec);
a385ba
+	if (!buff)
a385ba
+		return 1;
a385ba
+
a385ba
+	if ((strlen(buff) == 2 && !strcmp(buff, "no")) ||
a385ba
+	    (strlen(buff) == 1 && !strcmp(buff, "0")))
a385ba
+		mpe->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+	else if ((mpe->marginal_path_double_failed_time = atoi(buff)) < 1)
a385ba
+		mpe->marginal_path_double_failed_time = MARGINAL_PATH_OFF;
a385ba
+
a385ba
+	FREE(buff);
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
 /*
a385ba
  * config file keywords printing
a385ba
  */
a385ba
@@ -2989,6 +3248,56 @@ snprint_mp_ghost_delay (char * buff, int
a385ba
 }
a385ba
 
a385ba
 static int
a385ba
+snprint_mp_marginal_path_err_sample_time (char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct mpentry * mpe = (struct mpentry *)data;
a385ba
+
a385ba
+	if (mpe->marginal_path_err_sample_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (mpe->marginal_path_err_sample_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", mpe->marginal_path_err_sample_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_mp_marginal_path_err_rate_threshold (char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct mpentry * mpe = (struct mpentry *)data;
a385ba
+
a385ba
+	if (mpe->marginal_path_err_rate_threshold == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (mpe->marginal_path_err_rate_threshold == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", mpe->marginal_path_err_rate_threshold);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_mp_marginal_path_err_recheck_gap_time (char * buff, int len,
a385ba
+					       void * data)
a385ba
+{
a385ba
+	struct mpentry * mpe = (struct mpentry *)data;
a385ba
+
a385ba
+	if (mpe->marginal_path_err_recheck_gap_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (mpe->marginal_path_err_recheck_gap_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d",
a385ba
+			mpe->marginal_path_err_recheck_gap_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_mp_marginal_path_double_failed_time (char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct mpentry * mpe = (struct mpentry *)data;
a385ba
+
a385ba
+	if (mpe->marginal_path_double_failed_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (mpe->marginal_path_double_failed_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", mpe->marginal_path_double_failed_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
 snprint_hw_fast_io_fail(char * buff, int len, void * data)
a385ba
 {
a385ba
 	struct hwentry * hwe = (struct hwentry *)data;
a385ba
@@ -3429,6 +3738,55 @@ snprint_hw_all_tg_pt(char * buff, int le
a385ba
 }
a385ba
 
a385ba
 static int
a385ba
+snprint_hw_marginal_path_err_sample_time(char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct hwentry * hwe = (struct hwentry *)data;
a385ba
+
a385ba
+	if (hwe->marginal_path_err_sample_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (hwe->marginal_path_err_sample_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", hwe->marginal_path_err_sample_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_hw_marginal_path_err_rate_threshold(char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct hwentry * hwe = (struct hwentry *)data;
a385ba
+
a385ba
+	if (hwe->marginal_path_err_rate_threshold == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (hwe->marginal_path_err_rate_threshold == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", hwe->marginal_path_err_rate_threshold);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_hw_marginal_path_err_recheck_gap_time(char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct hwentry * hwe = (struct hwentry *)data;
a385ba
+
a385ba
+	if (hwe->marginal_path_err_recheck_gap_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (hwe->marginal_path_err_recheck_gap_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d",
a385ba
+			hwe->marginal_path_err_recheck_gap_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_hw_marginal_path_double_failed_time(char * buff, int len, void * data)
a385ba
+{
a385ba
+	struct hwentry * hwe = (struct hwentry *)data;
a385ba
+
a385ba
+	if (hwe->marginal_path_double_failed_time == MARGINAL_PATH_UNDEF)
a385ba
+		return 0;
a385ba
+	if (hwe->marginal_path_double_failed_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", hwe->marginal_path_double_failed_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
 snprint_def_polling_interval (char * buff, int len, void * data)
a385ba
 {
a385ba
 	return snprintf(buff, len, "%i", conf->checkint);
a385ba
@@ -3945,6 +4303,46 @@ snprint_def_all_tg_pt(char * buff, int l
a385ba
 }
a385ba
 
a385ba
 static int
a385ba
+snprint_def_marginal_path_err_sample_time(char * buff, int len, void * data)
a385ba
+{
a385ba
+	if (conf->marginal_path_err_sample_time == MARGINAL_PATH_UNDEF ||
a385ba
+	    conf->marginal_path_err_sample_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d", conf->marginal_path_err_sample_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_def_marginal_path_err_rate_threshold(char * buff, int len, void * data)
a385ba
+{
a385ba
+	if (conf->marginal_path_err_rate_threshold == MARGINAL_PATH_UNDEF ||
a385ba
+	    conf->marginal_path_err_rate_threshold == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d",
a385ba
+			conf->marginal_path_err_rate_threshold);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_def_marginal_path_err_recheck_gap_time(char * buff, int len,
a385ba
+					       void * data)
a385ba
+{
a385ba
+	if (conf->marginal_path_err_recheck_gap_time == MARGINAL_PATH_UNDEF ||
a385ba
+	    conf->marginal_path_err_recheck_gap_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d",
a385ba
+			conf->marginal_path_err_recheck_gap_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
+snprint_def_marginal_path_double_failed_time(char * buff, int len, void * data)
a385ba
+{
a385ba
+	if (conf->marginal_path_double_failed_time == MARGINAL_PATH_UNDEF ||
a385ba
+	    conf->marginal_path_double_failed_time == MARGINAL_PATH_OFF)
a385ba
+		return snprintf(buff, len, "no");
a385ba
+	return snprintf(buff, len, "%d",
a385ba
+			conf->marginal_path_double_failed_time);
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
 snprint_ble_simple (char * buff, int len, void * data)
a385ba
 {
a385ba
 	struct blentry * ble = (struct blentry *)data;
a385ba
@@ -4043,6 +4441,10 @@ init_keywords(void)
a385ba
 	install_keyword("unpriv_sgio", &def_unpriv_sgio_handler, &snprint_def_unpriv_sgio);
a385ba
 	install_keyword("ghost_delay", &def_ghost_delay_handler, &snprint_def_ghost_delay);
a385ba
 	install_keyword("all_tg_pt", &def_all_tg_pt_handler, &snprint_def_all_tg_pt);
a385ba
+	install_keyword("marginal_path_err_sample_time", &def_marginal_path_err_sample_time_handler, &snprint_def_marginal_path_err_sample_time);
a385ba
+	install_keyword("marginal_path_err_rate_threshold", &def_marginal_path_err_rate_threshold_handler, &snprint_def_marginal_path_err_rate_threshold);
a385ba
+	install_keyword("marginal_path_err_recheck_gap_time", &def_marginal_path_err_recheck_gap_time_handler, &snprint_def_marginal_path_err_recheck_gap_time);
a385ba
+	install_keyword("marginal_path_double_failed_time", &def_marginal_path_double_failed_time_handler, &snprint_def_marginal_path_double_failed_time);
a385ba
 	__deprecated install_keyword("default_selector", &def_selector_handler, NULL);
a385ba
 	__deprecated install_keyword("default_path_grouping_policy", &def_pgpolicy_handler, NULL);
a385ba
 	__deprecated install_keyword("default_uid_attribute", &def_uid_attribute_handler, NULL);
a385ba
@@ -4120,6 +4522,10 @@ init_keywords(void)
a385ba
 	install_keyword("unpriv_sgio", &hw_unpriv_sgio_handler, &snprint_hw_unpriv_sgio);
a385ba
 	install_keyword("ghost_delay", &hw_ghost_delay_handler, &snprint_hw_ghost_delay);
a385ba
 	install_keyword("all_tg_pt", &hw_all_tg_pt_handler, &snprint_hw_all_tg_pt);
a385ba
+	install_keyword("marginal_path_err_sample_time", &hw_marginal_path_err_sample_time_handler, &snprint_hw_marginal_path_err_sample_time);
a385ba
+	install_keyword("marginal_path_err_rate_threshold", &hw_marginal_path_err_rate_threshold_handler, &snprint_hw_marginal_path_err_rate_threshold);
a385ba
+	install_keyword("marginal_path_err_recheck_gap_time", &hw_marginal_path_err_recheck_gap_time_handler, &snprint_hw_marginal_path_err_recheck_gap_time);
a385ba
+	install_keyword("marginal_path_double_failed_time", &hw_marginal_path_double_failed_time_handler, &snprint_hw_marginal_path_double_failed_time);
a385ba
 	install_sublevel_end();
a385ba
 
a385ba
 	install_keyword_root("overrides", &nop_handler);
a385ba
@@ -4184,5 +4590,9 @@ init_keywords(void)
a385ba
 	install_keyword("max_sectors_kb", &mp_max_sectors_kb_handler, &snprint_mp_max_sectors_kb);
a385ba
 	install_keyword("unpriv_sgio", &mp_unpriv_sgio_handler, &snprint_mp_unpriv_sgio);
a385ba
 	install_keyword("ghost_delay", &mp_ghost_delay_handler, &snprint_mp_ghost_delay);
a385ba
+	install_keyword("marginal_path_err_sample_time", &mp_marginal_path_err_sample_time_handler, &snprint_mp_marginal_path_err_sample_time);
a385ba
+	install_keyword("marginal_path_err_rate_threshold", &mp_marginal_path_err_rate_threshold_handler, &snprint_mp_marginal_path_err_rate_threshold);
a385ba
+	install_keyword("marginal_path_err_recheck_gap_time", &mp_marginal_path_err_recheck_gap_time_handler, &snprint_mp_marginal_path_err_recheck_gap_time);
a385ba
+	install_keyword("marginal_path_double_failed_time", &mp_marginal_path_double_failed_time_handler, &snprint_mp_marginal_path_double_failed_time);
a385ba
 	install_sublevel_end();
a385ba
 }
a385ba
Index: multipath-tools-130222/libmultipath/io_err_stat.c
a385ba
===================================================================
a385ba
--- /dev/null
a385ba
+++ multipath-tools-130222/libmultipath/io_err_stat.c
a385ba
@@ -0,0 +1,763 @@
a385ba
+/*
a385ba
+ * (C) Copyright HUAWEI Technology Corp. 2017, All Rights Reserved.
a385ba
+ *
a385ba
+ * io_err_stat.c
a385ba
+ * version 1.0
a385ba
+ *
a385ba
+ * IO error stream statistic process for path failure event from kernel
a385ba
+ *
a385ba
+ * Author(s): Guan Junxiong 2017 <guanjunxiong@huawei.com>
a385ba
+ *
a385ba
+ * This file is released under the GPL version 2, or any later version.
a385ba
+ */
a385ba
+
a385ba
+#include <unistd.h>
a385ba
+#include <pthread.h>
a385ba
+#include <signal.h>
a385ba
+#include <fcntl.h>
a385ba
+#include <sys/stat.h>
a385ba
+#include <sys/ioctl.h>
a385ba
+#include <linux/fs.h>
a385ba
+#include <libaio.h>
a385ba
+#include <errno.h>
a385ba
+#include <sys/mman.h>
a385ba
+
a385ba
+#include "vector.h"
a385ba
+#include "memory.h"
a385ba
+#include "checkers.h"
a385ba
+#include "config.h"
a385ba
+#include "structs.h"
a385ba
+#include "structs_vec.h"
a385ba
+#include "devmapper.h"
a385ba
+#include "debug.h"
a385ba
+#include "lock.h"
a385ba
+#include "time-util.h"
a385ba
+#include "io_err_stat.h"
a385ba
+
a385ba
+#define IOTIMEOUT_SEC			60
a385ba
+#define TIMEOUT_NO_IO_NSEC		10000000 /*10ms = 10000000ns*/
a385ba
+#define FLAKY_PATHFAIL_THRESHOLD	2
a385ba
+#define CONCUR_NR_EVENT			32
a385ba
+
a385ba
+#define PATH_IO_ERR_IN_CHECKING		-1
a385ba
+#define PATH_IO_ERR_WAITING_TO_CHECK	-2
a385ba
+
a385ba
+#define io_err_stat_log(prio, fmt, args...) \
a385ba
+	condlog(prio, "io error statistic: " fmt, ##args)
a385ba
+
a385ba
+
a385ba
+struct io_err_stat_pathvec {
a385ba
+	pthread_mutex_t mutex;
a385ba
+	vector		pathvec;
a385ba
+};
a385ba
+
a385ba
+struct dio_ctx {
a385ba
+	struct timespec	io_starttime;
a385ba
+	int		blksize;
a385ba
+	void		*buf;
a385ba
+	struct iocb	io;
a385ba
+};
a385ba
+
a385ba
+struct io_err_stat_path {
a385ba
+	char		devname[FILE_NAME_SIZE];
a385ba
+	int		fd;
a385ba
+	struct dio_ctx	*dio_ctx_array;
a385ba
+	int		io_err_nr;
a385ba
+	int		io_nr;
a385ba
+	struct timespec	start_time;
a385ba
+
a385ba
+	int		total_time;
a385ba
+	int		err_rate_threshold;
a385ba
+};
a385ba
+
a385ba
+pthread_t		io_err_stat_thr;
a385ba
+pthread_attr_t		io_err_stat_attr;
a385ba
+
a385ba
+static pthread_mutex_t io_err_thread_lock = PTHREAD_MUTEX_INITIALIZER;
a385ba
+static pthread_cond_t io_err_thread_cond = PTHREAD_COND_INITIALIZER;
a385ba
+static int io_err_thread_running = 0;
a385ba
+
a385ba
+#define uatomic_read(ptr) __atomic_load_n((ptr), __ATOMIC_SEQ_CST)
a385ba
+#define uatomic_set(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_SEQ_CST)
a385ba
+
a385ba
+static struct io_err_stat_pathvec *paths;
a385ba
+struct vectors *vecs;
a385ba
+io_context_t	ioctx;
a385ba
+
a385ba
+static void cancel_inflight_io(struct io_err_stat_path *pp);
a385ba
+
a385ba
+struct io_err_stat_path *find_err_path_by_dev(vector pathvec, char *dev)
a385ba
+{
a385ba
+	int i;
a385ba
+	struct io_err_stat_path *pp;
a385ba
+
a385ba
+	if (!pathvec)
a385ba
+		return NULL;
a385ba
+	vector_foreach_slot(pathvec, pp, i)
a385ba
+		if (!strcmp(pp->devname, dev))
a385ba
+			return pp;
a385ba
+
a385ba
+	io_err_stat_log(4, "%s: not found in check queue", dev);
a385ba
+
a385ba
+	return NULL;
a385ba
+}
a385ba
+
a385ba
+static int init_each_dio_ctx(struct dio_ctx *ct, int blksize,
a385ba
+		unsigned long pgsize)
a385ba
+{
a385ba
+	ct->blksize = blksize;
a385ba
+	if (posix_memalign(&ct->buf, pgsize, blksize))
a385ba
+		return 1;
a385ba
+	memset(ct->buf, 0, blksize);
a385ba
+	ct->io_starttime.tv_sec = 0;
a385ba
+	ct->io_starttime.tv_nsec = 0;
a385ba
+
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static void deinit_each_dio_ctx(struct dio_ctx *ct)
a385ba
+{
a385ba
+	if (ct->buf)
a385ba
+		free(ct->buf);
a385ba
+}
a385ba
+
a385ba
+static int setup_directio_ctx(struct io_err_stat_path *p)
a385ba
+{
a385ba
+	unsigned long pgsize = getpagesize();
a385ba
+	char fpath[PATH_MAX];
a385ba
+	int blksize = 0;
a385ba
+	int i;
a385ba
+
a385ba
+	if (snprintf(fpath, PATH_MAX, "/dev/%s", p->devname) >= PATH_MAX)
a385ba
+		return 1;
a385ba
+	if (p->fd < 0)
a385ba
+		p->fd = open(fpath, O_RDONLY | O_DIRECT);
a385ba
+	if (p->fd < 0)
a385ba
+		return 1;
a385ba
+
a385ba
+	p->dio_ctx_array = MALLOC(sizeof(struct dio_ctx) * CONCUR_NR_EVENT);
a385ba
+	if (!p->dio_ctx_array)
a385ba
+		goto fail_close;
a385ba
+
a385ba
+	if (ioctl(p->fd, BLKBSZGET, &blksize) < 0) {
a385ba
+		io_err_stat_log(4, "%s:cannot get blocksize, set default 512",
a385ba
+				p->devname);
a385ba
+		blksize = 512;
a385ba
+	}
a385ba
+	if (!blksize)
a385ba
+		goto free_pdctx;
a385ba
+
a385ba
+	for (i = 0; i < CONCUR_NR_EVENT; i++) {
a385ba
+		if (init_each_dio_ctx(p->dio_ctx_array + i, blksize, pgsize))
a385ba
+			goto deinit;
a385ba
+	}
a385ba
+	return 0;
a385ba
+
a385ba
+deinit:
a385ba
+	for (i = 0; i < CONCUR_NR_EVENT; i++)
a385ba
+		deinit_each_dio_ctx(p->dio_ctx_array + i);
a385ba
+free_pdctx:
a385ba
+	FREE(p->dio_ctx_array);
a385ba
+fail_close:
a385ba
+	close(p->fd);
a385ba
+
a385ba
+	return 1;
a385ba
+}
a385ba
+
a385ba
+static void destroy_directio_ctx(struct io_err_stat_path *p)
a385ba
+{
a385ba
+	int i;
a385ba
+
a385ba
+	if (!p || !p->dio_ctx_array)
a385ba
+		return;
a385ba
+	cancel_inflight_io(p);
a385ba
+
a385ba
+	for (i = 0; i < CONCUR_NR_EVENT; i++)
a385ba
+		deinit_each_dio_ctx(p->dio_ctx_array + i);
a385ba
+	FREE(p->dio_ctx_array);
a385ba
+
a385ba
+	if (p->fd > 0)
a385ba
+		close(p->fd);
a385ba
+}
a385ba
+
a385ba
+static struct io_err_stat_path *alloc_io_err_stat_path(void)
a385ba
+{
a385ba
+	struct io_err_stat_path *p;
a385ba
+
a385ba
+	p = (struct io_err_stat_path *)MALLOC(sizeof(*p));
a385ba
+	if (!p)
a385ba
+		return NULL;
a385ba
+
a385ba
+	memset(p->devname, 0, sizeof(p->devname));
a385ba
+	p->io_err_nr = 0;
a385ba
+	p->io_nr = 0;
a385ba
+	p->total_time = 0;
a385ba
+	p->start_time.tv_sec = 0;
a385ba
+	p->start_time.tv_nsec = 0;
a385ba
+	p->err_rate_threshold = 0;
a385ba
+	p->fd = -1;
a385ba
+
a385ba
+	return p;
a385ba
+}
a385ba
+
a385ba
+static void free_io_err_stat_path(struct io_err_stat_path *p)
a385ba
+{
a385ba
+	FREE(p);
a385ba
+}
a385ba
+
a385ba
+static struct io_err_stat_pathvec *alloc_pathvec(void)
a385ba
+{
a385ba
+	struct io_err_stat_pathvec *p;
a385ba
+	int r;
a385ba
+
a385ba
+	p = (struct io_err_stat_pathvec *)MALLOC(sizeof(*p));
a385ba
+	if (!p)
a385ba
+		return NULL;
a385ba
+	p->pathvec = vector_alloc();
a385ba
+	if (!p->pathvec)
a385ba
+		goto out_free_struct_pathvec;
a385ba
+	r = pthread_mutex_init(&p->mutex, NULL);
a385ba
+	if (r)
a385ba
+		goto out_free_member_pathvec;
a385ba
+
a385ba
+	return p;
a385ba
+
a385ba
+out_free_member_pathvec:
a385ba
+	vector_free(p->pathvec);
a385ba
+out_free_struct_pathvec:
a385ba
+	FREE(p);
a385ba
+	return NULL;
a385ba
+}
a385ba
+
a385ba
+static void free_io_err_pathvec(struct io_err_stat_pathvec *p)
a385ba
+{
a385ba
+	struct io_err_stat_path *path;
a385ba
+	int i;
a385ba
+
a385ba
+	if (!p)
a385ba
+		return;
a385ba
+	pthread_mutex_destroy(&p->mutex);
a385ba
+	if (!p->pathvec) {
a385ba
+		vector_foreach_slot(p->pathvec, path, i) {
a385ba
+			destroy_directio_ctx(path);
a385ba
+			free_io_err_stat_path(path);
a385ba
+		}
a385ba
+		vector_free(p->pathvec);
a385ba
+	}
a385ba
+	FREE(p);
a385ba
+}
a385ba
+
a385ba
+/*
a385ba
+ * return value
a385ba
+ * 0: enqueue OK
a385ba
+ * 1: fails because of internal error
a385ba
+ */
a385ba
+static int enqueue_io_err_stat_by_path(struct path *path)
a385ba
+{
a385ba
+	struct io_err_stat_path *p;
a385ba
+
a385ba
+	pthread_mutex_lock(&paths->mutex);
a385ba
+	p = find_err_path_by_dev(paths->pathvec, path->dev);
a385ba
+	if (p) {
a385ba
+		pthread_mutex_unlock(&paths->mutex);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	pthread_mutex_unlock(&paths->mutex);
a385ba
+
a385ba
+	p = alloc_io_err_stat_path();
a385ba
+	if (!p)
a385ba
+		return 1;
a385ba
+
a385ba
+	memcpy(p->devname, path->dev, sizeof(p->devname));
a385ba
+	p->total_time = path->mpp->marginal_path_err_sample_time;
a385ba
+	p->err_rate_threshold = path->mpp->marginal_path_err_rate_threshold;
a385ba
+
a385ba
+	if (setup_directio_ctx(p))
a385ba
+		goto free_ioerr_path;
a385ba
+	pthread_mutex_lock(&paths->mutex);
a385ba
+	if (!vector_alloc_slot(paths->pathvec))
a385ba
+		goto unlock_destroy;
a385ba
+	vector_set_slot(paths->pathvec, p);
a385ba
+	pthread_mutex_unlock(&paths->mutex);
a385ba
+
a385ba
+	io_err_stat_log(2, "%s: enqueue path %s to check",
a385ba
+			path->mpp->alias, path->dev);
a385ba
+	return 0;
a385ba
+
a385ba
+unlock_destroy:
a385ba
+	pthread_mutex_unlock(&paths->mutex);
a385ba
+	destroy_directio_ctx(p);
a385ba
+free_ioerr_path:
a385ba
+	free_io_err_stat_path(p);
a385ba
+
a385ba
+	return 1;
a385ba
+}
a385ba
+
a385ba
+int io_err_stat_handle_pathfail(struct path *path)
a385ba
+{
a385ba
+	struct timespec curr_time;
a385ba
+
a385ba
+	if (uatomic_read(&io_err_thread_running) == 0)
a385ba
+		return 1;
a385ba
+
a385ba
+	if (path->io_err_disable_reinstate) {
a385ba
+		io_err_stat_log(3, "%s: reinstate is already disabled",
a385ba
+				path->dev);
a385ba
+		return 1;
a385ba
+	}
a385ba
+	if (path->io_err_pathfail_cnt < 0)
a385ba
+		return 1;
a385ba
+
a385ba
+	if (!path->mpp)
a385ba
+		return 1;
a385ba
+	if (path->mpp->marginal_path_double_failed_time <= 0 ||
a385ba
+		path->mpp->marginal_path_err_sample_time <= 0 ||
a385ba
+		path->mpp->marginal_path_err_recheck_gap_time <= 0 ||
a385ba
+		path->mpp->marginal_path_err_rate_threshold < 0) {
a385ba
+		io_err_stat_log(4, "%s: parameter not set", path->mpp->alias);
a385ba
+		return 1;
a385ba
+	}
a385ba
+	if (path->mpp->marginal_path_err_sample_time < (2 * IOTIMEOUT_SEC)) {
a385ba
+		io_err_stat_log(2, "%s: marginal_path_err_sample_time should not less than %d",
a385ba
+				path->mpp->alias, 2 * IOTIMEOUT_SEC);
a385ba
+		return 1;
a385ba
+	}
a385ba
+	/*
a385ba
+	 * The test should only be started for paths that have failed
a385ba
+	 * repeatedly in a certain time frame, so that we have reason
a385ba
+	 * to assume they're flaky. Without bother the admin to configure
a385ba
+	 * the repeated count threshold and time frame, we assume a path
a385ba
+	 * which fails at least twice within 60 seconds is flaky.
a385ba
+	 */
a385ba
+	if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
a385ba
+		return 1;
a385ba
+	if (path->io_err_pathfail_cnt == 0) {
a385ba
+		path->io_err_pathfail_cnt++;
a385ba
+		path->io_err_pathfail_starttime = curr_time.tv_sec;
a385ba
+		io_err_stat_log(5, "%s: start path flakiness pre-checking",
a385ba
+				path->dev);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if ((curr_time.tv_sec - path->io_err_pathfail_starttime) >
a385ba
+			path->mpp->marginal_path_double_failed_time) {
a385ba
+		path->io_err_pathfail_cnt = 0;
a385ba
+		path->io_err_pathfail_starttime = curr_time.tv_sec;
a385ba
+		io_err_stat_log(5, "%s: restart path flakiness pre-checking",
a385ba
+				path->dev);
a385ba
+	}
a385ba
+	path->io_err_pathfail_cnt++;
a385ba
+	if (path->io_err_pathfail_cnt >= FLAKY_PATHFAIL_THRESHOLD) {
a385ba
+		path->io_err_disable_reinstate = 1;
a385ba
+		path->io_err_pathfail_cnt = PATH_IO_ERR_WAITING_TO_CHECK;
a385ba
+		/* enqueue path as soon as it comes up */
a385ba
+		path->io_err_dis_reinstate_time = 0;
a385ba
+		if (path->state != PATH_DOWN) {
a385ba
+			int oldstate = path->state;
a385ba
+			io_err_stat_log(2, "%s: mark as failed", path->dev);
a385ba
+			path->mpp->stat_path_failures++;
a385ba
+			path->state = PATH_DOWN;
a385ba
+			path->dmstate = PSTATE_FAILED;
a385ba
+			if (oldstate == PATH_UP || oldstate == PATH_GHOST)
a385ba
+				update_queue_mode_del_path(path->mpp);
a385ba
+			if (path->tick > conf->checkint)
a385ba
+				path->tick = conf->checkint;
a385ba
+		}
a385ba
+	}
a385ba
+
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+int need_io_err_check(struct path *pp)
a385ba
+{
a385ba
+	struct timespec curr_time;
a385ba
+	int r;
a385ba
+
a385ba
+	if (uatomic_read(&io_err_thread_running) == 0)
a385ba
+		return 0;
a385ba
+	if (pp->mpp->nr_active <= 0) {
a385ba
+		io_err_stat_log(2, "%s: recover path early", pp->dev);
a385ba
+		goto recover;
a385ba
+	}
a385ba
+	if (pp->io_err_pathfail_cnt != PATH_IO_ERR_WAITING_TO_CHECK)
a385ba
+		return 1;
a385ba
+	if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0 ||
a385ba
+	    (curr_time.tv_sec - pp->io_err_dis_reinstate_time) >
a385ba
+			pp->mpp->marginal_path_err_recheck_gap_time) {
a385ba
+		io_err_stat_log(4, "%s: reschedule checking after %d seconds",
a385ba
+				pp->dev,
a385ba
+				pp->mpp->marginal_path_err_recheck_gap_time);
a385ba
+		r = enqueue_io_err_stat_by_path(pp);
a385ba
+		/*
a385ba
+		 * Enqueue fails because of internal error.
a385ba
+		 * In this case , we recover this path
a385ba
+		 * Or else,  return 1 to set path state to PATH_SHAKY
a385ba
+		 */
a385ba
+		if (r == 1) {
a385ba
+			io_err_stat_log(3, "%s: enqueue fails, recovering",
a385ba
+					pp->dev);
a385ba
+			goto recover;
a385ba
+		} else
a385ba
+			pp->io_err_pathfail_cnt = PATH_IO_ERR_IN_CHECKING;
a385ba
+	}
a385ba
+
a385ba
+	return 1;
a385ba
+
a385ba
+recover:
a385ba
+	pp->io_err_pathfail_cnt = 0;
a385ba
+	pp->io_err_disable_reinstate = 0;
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int delete_io_err_stat_by_addr(struct io_err_stat_path *p)
a385ba
+{
a385ba
+	int i;
a385ba
+
a385ba
+	i = find_slot(paths->pathvec, p);
a385ba
+	if (i != -1)
a385ba
+		vector_del_slot(paths->pathvec, i);
a385ba
+
a385ba
+	destroy_directio_ctx(p);
a385ba
+	free_io_err_stat_path(p);
a385ba
+
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static void account_async_io_state(struct io_err_stat_path *pp, int rc)
a385ba
+{
a385ba
+	switch (rc) {
a385ba
+	case PATH_DOWN:
a385ba
+		pp->io_err_nr++;
a385ba
+		break;
a385ba
+	case PATH_UNCHECKED:
a385ba
+	case PATH_UP:
a385ba
+	case PATH_PENDING:
a385ba
+		break;
a385ba
+	default:
a385ba
+		break;
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static int poll_io_err_stat(struct vectors *vecs, struct io_err_stat_path *pp)
a385ba
+{
a385ba
+	struct timespec currtime, difftime;
a385ba
+	struct path *path;
a385ba
+	double err_rate;
a385ba
+
a385ba
+	if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
a385ba
+		return 1;
a385ba
+	timespecsub(&currtime, &pp->start_time, &difftime);
a385ba
+	if (difftime.tv_sec < pp->total_time)
a385ba
+		return 0;
a385ba
+
a385ba
+	io_err_stat_log(4, "%s: check end", pp->devname);
a385ba
+
a385ba
+	err_rate = pp->io_nr == 0 ? 0 : (pp->io_err_nr * 1000.0f) / pp->io_nr;
a385ba
+	io_err_stat_log(3, "%s: IO error rate (%.1f/1000)",
a385ba
+			pp->devname, err_rate);
a385ba
+	pthread_cleanup_push(cleanup_lock, &vecs->lock);
a385ba
+	lock(vecs->lock);
a385ba
+	pthread_testcancel();
a385ba
+	path = find_path_by_dev(vecs->pathvec, pp->devname);
a385ba
+	if (!path) {
a385ba
+		io_err_stat_log(4, "path %s not found'", pp->devname);
a385ba
+	} else if (err_rate <= pp->err_rate_threshold) {
a385ba
+		path->io_err_pathfail_cnt = 0;
a385ba
+		path->io_err_disable_reinstate = 0;
a385ba
+		io_err_stat_log(3, "%s: (%d/%d) good to enable reinstating",
a385ba
+				pp->devname, pp->io_err_nr, pp->io_nr);
a385ba
+		/*
a385ba
+		 * schedule path check as soon as possible to
a385ba
+		 * update path state. Do NOT reinstate dm path here
a385ba
+		 */
a385ba
+		path->tick = 1;
a385ba
+
a385ba
+	} else if (path->mpp && path->mpp->nr_active > 0) {
a385ba
+		io_err_stat_log(3, "%s: keep failing the dm path %s",
a385ba
+				path->mpp->alias, path->dev);
a385ba
+		path->io_err_pathfail_cnt = PATH_IO_ERR_WAITING_TO_CHECK;
a385ba
+		path->io_err_disable_reinstate = 1;
a385ba
+		path->io_err_dis_reinstate_time = currtime.tv_sec;
a385ba
+		io_err_stat_log(3, "%s: disable reinstating of %s",
a385ba
+				path->mpp->alias, path->dev);
a385ba
+	} else {
a385ba
+		path->io_err_pathfail_cnt = 0;
a385ba
+		path->io_err_disable_reinstate = 0;
a385ba
+		io_err_stat_log(3, "%s: there is orphan path, enable reinstating",
a385ba
+				pp->devname);
a385ba
+	}
a385ba
+	lock_cleanup_pop(vecs->lock);
a385ba
+
a385ba
+	delete_io_err_stat_by_addr(pp);
a385ba
+
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+static int send_each_async_io(struct dio_ctx *ct, int fd, char *dev)
a385ba
+{
a385ba
+	int rc = -1;
a385ba
+
a385ba
+	if (ct->io_starttime.tv_nsec == 0 &&
a385ba
+			ct->io_starttime.tv_sec == 0) {
a385ba
+		struct iocb *ios[1] = { &ct->io };
a385ba
+
a385ba
+		if (clock_gettime(CLOCK_MONOTONIC, &ct->io_starttime) != 0) {
a385ba
+			ct->io_starttime.tv_sec = 0;
a385ba
+			ct->io_starttime.tv_nsec = 0;
a385ba
+			return rc;
a385ba
+		}
a385ba
+		io_prep_pread(&ct->io, fd, ct->buf, ct->blksize, 0);
a385ba
+		if (io_submit(ioctx, 1, ios) != 1) {
a385ba
+			io_err_stat_log(5, "%s: io_submit error %i",
a385ba
+					dev, errno);
a385ba
+			return rc;
a385ba
+		}
a385ba
+		rc = 0;
a385ba
+	}
a385ba
+
a385ba
+	return rc;
a385ba
+}
a385ba
+
a385ba
+static void send_batch_async_ios(struct io_err_stat_path *pp)
a385ba
+{
a385ba
+	int i;
a385ba
+	struct dio_ctx *ct;
a385ba
+	struct timespec currtime, difftime;
a385ba
+
a385ba
+	if (clock_gettime(CLOCK_MONOTONIC, &currtime) != 0)
a385ba
+		return;
a385ba
+	/*
a385ba
+	 * Give a free time for all IO to complete or timeout
a385ba
+	 */
a385ba
+	if (pp->start_time.tv_sec != 0) {
a385ba
+		timespecsub(&currtime, &pp->start_time, &difftime);
a385ba
+		if (difftime.tv_sec + IOTIMEOUT_SEC >= pp->total_time)
a385ba
+			return;
a385ba
+	}
a385ba
+
a385ba
+	for (i = 0; i < CONCUR_NR_EVENT; i++) {
a385ba
+		ct = pp->dio_ctx_array + i;
a385ba
+		if (!send_each_async_io(ct, pp->fd, pp->devname))
a385ba
+			pp->io_nr++;
a385ba
+	}
a385ba
+	if (pp->start_time.tv_sec == 0 && pp->start_time.tv_nsec == 0 &&
a385ba
+		clock_gettime(CLOCK_MONOTONIC, &pp->start_time)) {
a385ba
+		pp->start_time.tv_sec = 0;
a385ba
+		pp->start_time.tv_nsec = 0;
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static int try_to_cancel_timeout_io(struct dio_ctx *ct, struct timespec *t,
a385ba
+		char *dev)
a385ba
+{
a385ba
+	struct timespec	difftime;
a385ba
+	struct io_event	event;
a385ba
+	int		rc = PATH_UNCHECKED;
a385ba
+	int		r;
a385ba
+
a385ba
+	if (ct->io_starttime.tv_sec == 0)
a385ba
+		return rc;
a385ba
+	timespecsub(t, &ct->io_starttime, &difftime);
a385ba
+	if (difftime.tv_sec > IOTIMEOUT_SEC) {
a385ba
+		struct iocb *ios[1] = { &ct->io };
a385ba
+
a385ba
+		io_err_stat_log(5, "%s: abort check on timeout", dev);
a385ba
+		r = io_cancel(ioctx, ios[0], &event);
a385ba
+		if (r)
a385ba
+			io_err_stat_log(5, "%s: io_cancel error %i",
a385ba
+					dev, errno);
a385ba
+		ct->io_starttime.tv_sec = 0;
a385ba
+		ct->io_starttime.tv_nsec = 0;
a385ba
+		rc = PATH_DOWN;
a385ba
+	} else {
a385ba
+		rc = PATH_PENDING;
a385ba
+	}
a385ba
+
a385ba
+	return rc;
a385ba
+}
a385ba
+
a385ba
+static void poll_async_io_timeout(void)
a385ba
+{
a385ba
+	struct io_err_stat_path *pp;
a385ba
+	struct timespec curr_time;
a385ba
+	int		rc = PATH_UNCHECKED;
a385ba
+	int		i, j;
a385ba
+
a385ba
+	if (clock_gettime(CLOCK_MONOTONIC, &curr_time) != 0)
a385ba
+		return;
a385ba
+	vector_foreach_slot(paths->pathvec, pp, i) {
a385ba
+		for (j = 0; j < CONCUR_NR_EVENT; j++) {
a385ba
+			rc = try_to_cancel_timeout_io(pp->dio_ctx_array + j,
a385ba
+					&curr_time, pp->devname);
a385ba
+			account_async_io_state(pp, rc);
a385ba
+		}
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static void cancel_inflight_io(struct io_err_stat_path *pp)
a385ba
+{
a385ba
+	struct io_event event;
a385ba
+	int i, r;
a385ba
+
a385ba
+	for (i = 0; i < CONCUR_NR_EVENT; i++) {
a385ba
+		struct dio_ctx *ct = pp->dio_ctx_array + i;
a385ba
+		struct iocb *ios[1] = { &ct->io };
a385ba
+
a385ba
+		if (ct->io_starttime.tv_sec == 0
a385ba
+				&& ct->io_starttime.tv_nsec == 0)
a385ba
+			continue;
a385ba
+		io_err_stat_log(5, "%s: abort infligh io",
a385ba
+				pp->devname);
a385ba
+		r = io_cancel(ioctx, ios[0], &event);
a385ba
+		if (r)
a385ba
+			io_err_stat_log(5, "%s: io_cancel error %d, %i",
a385ba
+					pp->devname, r, errno);
a385ba
+		ct->io_starttime.tv_sec = 0;
a385ba
+		ct->io_starttime.tv_nsec = 0;
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static inline int handle_done_dio_ctx(struct dio_ctx *ct, struct io_event *ev)
a385ba
+{
a385ba
+	ct->io_starttime.tv_sec = 0;
a385ba
+	ct->io_starttime.tv_nsec = 0;
a385ba
+	return (ev->res == ct->blksize) ? PATH_UP : PATH_DOWN;
a385ba
+}
a385ba
+
a385ba
+static void handle_async_io_done_event(struct io_event *io_evt)
a385ba
+{
a385ba
+	struct io_err_stat_path *pp;
a385ba
+	struct dio_ctx *ct;
a385ba
+	int rc = PATH_UNCHECKED;
a385ba
+	int i, j;
a385ba
+
a385ba
+	vector_foreach_slot(paths->pathvec, pp, i) {
a385ba
+		for (j = 0; j < CONCUR_NR_EVENT; j++) {
a385ba
+			ct = pp->dio_ctx_array + j;
a385ba
+			if (&ct->io == io_evt->obj) {
a385ba
+				rc = handle_done_dio_ctx(ct, io_evt);
a385ba
+				account_async_io_state(pp, rc);
a385ba
+				return;
a385ba
+			}
a385ba
+		}
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static void process_async_ios_event(int timeout_nsecs, char *dev)
a385ba
+{
a385ba
+	struct io_event events[CONCUR_NR_EVENT];
a385ba
+	int		i, n;
a385ba
+	struct timespec	timeout = { .tv_nsec = timeout_nsecs };
a385ba
+
a385ba
+	errno = 0;
a385ba
+	n = io_getevents(ioctx, 1L, CONCUR_NR_EVENT, events, &timeout);
a385ba
+	if (n < 0) {
a385ba
+		io_err_stat_log(3, "%s: async io events returned %d (errno=%s)",
a385ba
+				dev, n, strerror(errno));
a385ba
+	} else {
a385ba
+		for (i = 0; i < n; i++)
a385ba
+			handle_async_io_done_event(&events[i]);
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+static void service_paths(void)
a385ba
+{
a385ba
+	struct io_err_stat_path *pp;
a385ba
+	int i;
a385ba
+
a385ba
+	pthread_mutex_lock(&paths->mutex);
a385ba
+	vector_foreach_slot(paths->pathvec, pp, i) {
a385ba
+		send_batch_async_ios(pp);
a385ba
+		process_async_ios_event(TIMEOUT_NO_IO_NSEC, pp->devname);
a385ba
+		poll_async_io_timeout();
a385ba
+		poll_io_err_stat(vecs, pp);
a385ba
+	}
a385ba
+	pthread_mutex_unlock(&paths->mutex);
a385ba
+}
a385ba
+
a385ba
+static void cleanup_unlock(void *arg)
a385ba
+{
a385ba
+	pthread_mutex_unlock((pthread_mutex_t*) arg);
a385ba
+}
a385ba
+
a385ba
+static void cleanup_exited(void *arg)
a385ba
+{
a385ba
+	uatomic_set(&io_err_thread_running, 0);
a385ba
+}
a385ba
+
a385ba
+static void *io_err_stat_loop(void *data)
a385ba
+{
a385ba
+	vecs = (struct vectors *)data;
a385ba
+
a385ba
+	pthread_cleanup_push(cleanup_exited, NULL);
a385ba
+
a385ba
+	mlockall(MCL_CURRENT | MCL_FUTURE);
a385ba
+
a385ba
+	pthread_mutex_lock(&io_err_thread_lock);
a385ba
+	uatomic_set(&io_err_thread_running, 1);
a385ba
+	pthread_cond_broadcast(&io_err_thread_cond);
a385ba
+	pthread_mutex_unlock(&io_err_thread_lock);
a385ba
+
a385ba
+	while (1) {
a385ba
+		service_paths();
a385ba
+		usleep(100000);
a385ba
+	}
a385ba
+
a385ba
+	pthread_cleanup_pop(1);
a385ba
+	return NULL;
a385ba
+}
a385ba
+
a385ba
+int start_io_err_stat_thread(void *data)
a385ba
+{
a385ba
+	int ret;
a385ba
+
a385ba
+	if (uatomic_read(&io_err_thread_running) == 1)
a385ba
+		return 0;
a385ba
+
a385ba
+	if (io_setup(CONCUR_NR_EVENT, &ioctx) != 0) {
a385ba
+		io_err_stat_log(4, "io_setup failed");
a385ba
+		return 1;
a385ba
+	}
a385ba
+	paths = alloc_pathvec();
a385ba
+	if (!paths)
a385ba
+		goto destroy_ctx;
a385ba
+
a385ba
+	pthread_mutex_lock(&io_err_thread_lock);
a385ba
+	pthread_cleanup_push(cleanup_unlock, &io_err_thread_lock);
a385ba
+
a385ba
+	ret = pthread_create(&io_err_stat_thr, &io_err_stat_attr,
a385ba
+			     io_err_stat_loop, data);
a385ba
+
a385ba
+	while (!ret && !uatomic_read(&io_err_thread_running) &&
a385ba
+	       pthread_cond_wait(&io_err_thread_cond,
a385ba
+				 &io_err_thread_lock) == 0);
a385ba
+
a385ba
+	pthread_cleanup_pop(1);
a385ba
+
a385ba
+	if (ret) {
a385ba
+		io_err_stat_log(0, "cannot create io_error statistic thread");
a385ba
+		goto out_free;
a385ba
+	}
a385ba
+
a385ba
+	io_err_stat_log(2, "io_error statistic thread started");
a385ba
+	return 0;
a385ba
+
a385ba
+out_free:
a385ba
+	free_io_err_pathvec(paths);
a385ba
+destroy_ctx:
a385ba
+	io_destroy(ioctx);
a385ba
+	io_err_stat_log(0, "failed to start io_error statistic thread");
a385ba
+	return 1;
a385ba
+}
a385ba
+
a385ba
+void stop_io_err_stat_thread(void)
a385ba
+{
a385ba
+	if (io_err_stat_thr == (pthread_t)0)
a385ba
+		return;
a385ba
+
a385ba
+	if (uatomic_read(&io_err_thread_running) == 1)
a385ba
+		pthread_cancel(io_err_stat_thr);
a385ba
+
a385ba
+	pthread_join(io_err_stat_thr, NULL);
a385ba
+	free_io_err_pathvec(paths);
a385ba
+	io_destroy(ioctx);
a385ba
+}
a385ba
Index: multipath-tools-130222/libmultipath/io_err_stat.h
a385ba
===================================================================
a385ba
--- /dev/null
a385ba
+++ multipath-tools-130222/libmultipath/io_err_stat.h
a385ba
@@ -0,0 +1,15 @@
a385ba
+#ifndef _IO_ERR_STAT_H
a385ba
+#define _IO_ERR_STAT_H
a385ba
+
a385ba
+#include "vector.h"
a385ba
+#include "lock.h"
a385ba
+
a385ba
+
a385ba
+extern pthread_attr_t io_err_stat_attr;
a385ba
+
a385ba
+int start_io_err_stat_thread(void *data);
a385ba
+void stop_io_err_stat_thread(void);
a385ba
+int io_err_stat_handle_pathfail(struct path *path);
a385ba
+int need_io_err_check(struct path *pp);
a385ba
+
a385ba
+#endif /* _IO_ERR_STAT_H */
a385ba
Index: multipath-tools-130222/libmultipath/propsel.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/propsel.c
a385ba
+++ multipath-tools-130222/libmultipath/propsel.c
a385ba
@@ -956,6 +956,104 @@ select_delay_wait_checks (struct multipa
a385ba
 }
a385ba
 
a385ba
 extern int
a385ba
+select_marginal_path_err_sample_time(struct multipath * mp)
a385ba
+{
a385ba
+	if (mp->mpe &&
a385ba
+	    mp->mpe->marginal_path_err_sample_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_sample_time = mp->mpe->marginal_path_err_sample_time;
a385ba
+		condlog(3, "marginal_path_err_sample_time = %i (multipath setting)", mp->marginal_path_err_sample_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (mp->hwe &&
a385ba
+	    mp->hwe->marginal_path_err_sample_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_sample_time = mp->hwe->marginal_path_err_sample_time;
a385ba
+		condlog(3, "marginal_path_err_sample_time = %i (controler setting)", mp->marginal_path_err_sample_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (conf->marginal_path_err_sample_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_sample_time = conf->marginal_path_err_sample_time;
a385ba
+		condlog(3, "marginal_path_err_sample_time = %i (config file default)", mp->marginal_path_err_sample_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	mp->marginal_path_err_sample_time = DEFAULT_DELAY_CHECKS;
a385ba
+	condlog(3, "marginal_path_err_sample_time = DISABLED (internal default)");
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+extern int
a385ba
+select_marginal_path_err_rate_threshold(struct multipath * mp)
a385ba
+{
a385ba
+	if (mp->mpe &&
a385ba
+	    mp->mpe->marginal_path_err_rate_threshold != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_rate_threshold = mp->mpe->marginal_path_err_rate_threshold;
a385ba
+		condlog(3, "marginal_path_err_rate_threshold = %i (multipath setting)", mp->marginal_path_err_rate_threshold);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (mp->hwe &&
a385ba
+	    mp->hwe->marginal_path_err_rate_threshold != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_rate_threshold = mp->hwe->marginal_path_err_rate_threshold;
a385ba
+		condlog(3, "marginal_path_err_rate_threshold = %i (controler setting)", mp->marginal_path_err_rate_threshold);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (conf->marginal_path_err_rate_threshold != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_rate_threshold = conf->marginal_path_err_rate_threshold;
a385ba
+		condlog(3, "marginal_path_err_rate_threshold = %i (config file default)", mp->marginal_path_err_rate_threshold);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	mp->marginal_path_err_rate_threshold = DEFAULT_DELAY_CHECKS;
a385ba
+	condlog(3, "marginal_path_err_rate_threshold = DISABLED (internal default)");
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+extern int
a385ba
+select_marginal_path_err_recheck_gap_time(struct multipath * mp)
a385ba
+{
a385ba
+	if (mp->mpe && mp->mpe->marginal_path_err_recheck_gap_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_recheck_gap_time = mp->mpe->marginal_path_err_recheck_gap_time;
a385ba
+		condlog(3, "marginal_path_err_recheck_gap_time = %i (multipath setting)", mp->marginal_path_err_recheck_gap_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (mp->hwe && mp->hwe->marginal_path_err_recheck_gap_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_recheck_gap_time = mp->hwe->marginal_path_err_recheck_gap_time;
a385ba
+		condlog(3, "marginal_path_err_recheck_gap_time = %i (controler setting)", mp->marginal_path_err_recheck_gap_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (conf->marginal_path_err_recheck_gap_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_err_recheck_gap_time = conf->marginal_path_err_recheck_gap_time;
a385ba
+		condlog(3, "marginal_path_err_recheck_gap_time = %i (config file default)", mp->marginal_path_err_recheck_gap_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	mp->marginal_path_err_recheck_gap_time = DEFAULT_DELAY_CHECKS;
a385ba
+	condlog(3, "marginal_path_err_recheck_gap_time = DISABLED (internal default)");
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+extern int
a385ba
+select_marginal_path_double_failed_time(struct multipath * mp)
a385ba
+{
a385ba
+	if (mp->mpe &&
a385ba
+	    mp->mpe->marginal_path_double_failed_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_double_failed_time = mp->mpe->marginal_path_double_failed_time;
a385ba
+		condlog(3, "marginal_path_double_failed_time = %i (multipath setting)", mp->marginal_path_double_failed_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (mp->hwe &&
a385ba
+	    mp->hwe->marginal_path_double_failed_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_double_failed_time = mp->hwe->marginal_path_double_failed_time;
a385ba
+		condlog(3, "marginal_path_double_failed_time = %i (controler setting)", mp->marginal_path_double_failed_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	if (conf->marginal_path_double_failed_time != MARGINAL_PATH_UNDEF) {
a385ba
+		mp->marginal_path_double_failed_time = conf->marginal_path_double_failed_time;
a385ba
+		condlog(3, "marginal_path_double_failed_time = %i (config file default)", mp->marginal_path_double_failed_time);
a385ba
+		return 0;
a385ba
+	}
a385ba
+	mp->marginal_path_double_failed_time = DEFAULT_DELAY_CHECKS;
a385ba
+	condlog(3, "marginal_path_double_failed_time = DISABLED (internal default)");
a385ba
+	return 0;
a385ba
+}
a385ba
+
a385ba
+extern int
a385ba
 select_skip_kpartx (struct multipath * mp)
a385ba
 {
a385ba
 	if (mp->mpe && mp->mpe->skip_kpartx != SKIP_KPARTX_UNDEF) {
a385ba
Index: multipath-tools-130222/libmultipath/propsel.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/propsel.h
a385ba
+++ multipath-tools-130222/libmultipath/propsel.h
a385ba
@@ -24,6 +24,10 @@ int select_detect_checker(struct path *
a385ba
 int select_deferred_remove(struct multipath *mp);
a385ba
 int select_delay_watch_checks (struct multipath * mp);
a385ba
 int select_delay_wait_checks (struct multipath * mp);
a385ba
+int select_marginal_path_err_sample_time(struct multipath *mp);
a385ba
+int select_marginal_path_err_rate_threshold(struct multipath *mp);
a385ba
+int select_marginal_path_err_recheck_gap_time(struct multipath *mp);
a385ba
+int select_marginal_path_double_failed_time(struct multipath *mp);
a385ba
 int select_skip_kpartx (struct multipath * mp);
a385ba
 int select_max_sectors_kb (struct multipath * mp);
a385ba
 int select_unpriv_sgio (struct multipath * mp);
a385ba
Index: multipath-tools-130222/libmultipath/structs.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/structs.h
a385ba
+++ multipath-tools-130222/libmultipath/structs.h
a385ba
@@ -3,6 +3,7 @@
a385ba
 
a385ba
 #include <sys/types.h>
a385ba
 #include <inttypes.h>
a385ba
+#include <time.h>
a385ba
 
a385ba
 #include "prio.h"
a385ba
 #include "byteorder.h"
a385ba
@@ -176,6 +177,11 @@ enum delay_checks_states {
a385ba
 	DELAY_CHECKS_UNDEF = 0,
a385ba
 };
a385ba
 
a385ba
+enum marginal_path_states {
a385ba
+	MARGINAL_PATH_OFF = -1,
a385ba
+	MARGINAL_PATH_UNDEF = 0,
a385ba
+};
a385ba
+
a385ba
 enum missing_udev_info_states {
a385ba
 	INFO_OK,
a385ba
 	INFO_MISSING,
a385ba
@@ -252,6 +258,10 @@ struct path {
a385ba
 	int missing_udev_info;
a385ba
 	int retriggers;
a385ba
 	int wwid_changed;
a385ba
+	time_t io_err_dis_reinstate_time;
a385ba
+	int io_err_disable_reinstate;
a385ba
+	int io_err_pathfail_cnt;
a385ba
+	int io_err_pathfail_starttime;
a385ba
 
a385ba
 	/* configlet pointers */
a385ba
 	struct hwentry * hwe;
a385ba
@@ -285,6 +295,10 @@ struct multipath {
a385ba
 	int deferred_remove;
a385ba
 	int delay_watch_checks;
a385ba
 	int delay_wait_checks;
a385ba
+	int marginal_path_err_sample_time;
a385ba
+	int marginal_path_err_rate_threshold;
a385ba
+	int marginal_path_err_recheck_gap_time;
a385ba
+	int marginal_path_double_failed_time;
a385ba
 	int force_udev_reload;
a385ba
 	int skip_kpartx;
a385ba
 	int max_sectors_kb;
a385ba
Index: multipath-tools-130222/libmultipath/time-util.c
a385ba
===================================================================
a385ba
--- /dev/null
a385ba
+++ multipath-tools-130222/libmultipath/time-util.c
a385ba
@@ -0,0 +1,42 @@
a385ba
+#include <assert.h>
a385ba
+#include <pthread.h>
a385ba
+#include <time.h>
a385ba
+#include "time-util.h"
a385ba
+
a385ba
+/* Initialize @cond as a condition variable that uses the monotonic clock */
a385ba
+void pthread_cond_init_mono(pthread_cond_t *cond)
a385ba
+{
a385ba
+	pthread_condattr_t attr;
a385ba
+	int res;
a385ba
+
a385ba
+	res = pthread_condattr_init(&attr);
a385ba
+	assert(res == 0);
a385ba
+	res = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
a385ba
+	assert(res == 0);
a385ba
+	res = pthread_cond_init(cond, &attr);
a385ba
+	assert(res == 0);
a385ba
+	res = pthread_condattr_destroy(&attr);
a385ba
+	assert(res == 0);
a385ba
+}
a385ba
+
a385ba
+/* Ensure that 0 <= ts->tv_nsec && ts->tv_nsec < 1000 * 1000 * 1000. */
a385ba
+void normalize_timespec(struct timespec *ts)
a385ba
+{
a385ba
+	while (ts->tv_nsec < 0) {
a385ba
+		ts->tv_nsec += 1000UL * 1000 * 1000;
a385ba
+		ts->tv_sec--;
a385ba
+	}
a385ba
+	while (ts->tv_nsec >= 1000UL * 1000 * 1000) {
a385ba
+		ts->tv_nsec -= 1000UL * 1000 * 1000;
a385ba
+		ts->tv_sec++;
a385ba
+	}
a385ba
+}
a385ba
+
a385ba
+/* Compute *res = *a - *b */
a385ba
+void timespecsub(const struct timespec *a, const struct timespec *b,
a385ba
+		 struct timespec *res)
a385ba
+{
a385ba
+	res->tv_sec = a->tv_sec - b->tv_sec;
a385ba
+	res->tv_nsec = a->tv_nsec - b->tv_nsec;
a385ba
+	normalize_timespec(res);
a385ba
+}
a385ba
Index: multipath-tools-130222/libmultipath/time-util.h
a385ba
===================================================================
a385ba
--- /dev/null
a385ba
+++ multipath-tools-130222/libmultipath/time-util.h
a385ba
@@ -0,0 +1,13 @@
a385ba
+#ifndef _TIME_UTIL_H_
a385ba
+#define _TIME_UTIL_H_
a385ba
+
a385ba
+#include <pthread.h>
a385ba
+
a385ba
+struct timespec;
a385ba
+
a385ba
+void pthread_cond_init_mono(pthread_cond_t *cond);
a385ba
+void normalize_timespec(struct timespec *ts);
a385ba
+void timespecsub(const struct timespec *a, const struct timespec *b,
a385ba
+		 struct timespec *res);
a385ba
+
a385ba
+#endif /* _TIME_UTIL_H_ */
a385ba
Index: multipath-tools-130222/libmultipath/uevent.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/uevent.c
a385ba
+++ multipath-tools-130222/libmultipath/uevent.c
a385ba
@@ -616,12 +616,46 @@ uevent_get_dm_name(struct uevent *uev)
a385ba
 	int i;
a385ba
 
a385ba
 	for (i = 0; uev->envp[i] != NULL; i++) {
a385ba
-		if (!strncmp(uev->envp[i], "DM_NAME", 6) &&
a385ba
-		    strlen(uev->envp[i]) > 7) {
a385ba
+		if (!strncmp(uev->envp[i], "DM_NAME", 7) &&
a385ba
+		    strlen(uev->envp[i]) > 8) {
a385ba
 			p = MALLOC(strlen(uev->envp[i] + 8) + 1);
a385ba
 			strcpy(p, uev->envp[i] + 8);
a385ba
 			break;
a385ba
 		}
a385ba
 	}
a385ba
+	return p;
a385ba
+}
a385ba
+
a385ba
+extern char *
a385ba
+uevent_get_dm_path(struct uevent *uev)
a385ba
+{
a385ba
+	char *p = NULL;
a385ba
+	int i;
a385ba
+
a385ba
+	for (i = 0; uev->envp[i] != NULL; i++) {
a385ba
+		if (!strncmp(uev->envp[i], "DM_PATH", 7) &&
a385ba
+		    strlen(uev->envp[i]) > 8) {
a385ba
+			p = MALLOC(strlen(uev->envp[i] + 8) + 1);
a385ba
+			strcpy(p, uev->envp[i] + 8);
a385ba
+			break;
a385ba
+		}
a385ba
+	}
a385ba
+	return p;
a385ba
+}
a385ba
+
a385ba
+extern char *
a385ba
+uevent_get_dm_action(struct uevent *uev)
a385ba
+{
a385ba
+	char *p = NULL;
a385ba
+	int i;
a385ba
+
a385ba
+	for (i = 0; uev->envp[i] != NULL; i++) {
a385ba
+		if (!strncmp(uev->envp[i], "DM_ACTION", 9) &&
a385ba
+		    strlen(uev->envp[i]) > 10) {
a385ba
+			p = MALLOC(strlen(uev->envp[i] + 10) + 1);
a385ba
+			strcpy(p, uev->envp[i] + 10);
a385ba
+			break;
a385ba
+		}
a385ba
+	}
a385ba
 	return p;
a385ba
 }
a385ba
Index: multipath-tools-130222/libmultipath/uevent.h
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/libmultipath/uevent.h
a385ba
+++ multipath-tools-130222/libmultipath/uevent.h
a385ba
@@ -36,5 +36,7 @@ int uevent_get_major(struct uevent *uev)
a385ba
 int uevent_get_minor(struct uevent *uev);
a385ba
 int uevent_get_disk_ro(struct uevent *uev);
a385ba
 char *uevent_get_dm_name(struct uevent *uev);
a385ba
+char *uevent_get_dm_path(struct uevent *uev);
a385ba
+char *uevent_get_dm_action(struct uevent *uev);
a385ba
 
a385ba
 #endif /* _UEVENT_H */
a385ba
Index: multipath-tools-130222/multipath/multipath.conf.5
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/multipath/multipath.conf.5
a385ba
+++ multipath-tools-130222/multipath/multipath.conf.5
a385ba
@@ -527,7 +527,7 @@ recently become valid for this many chec
a385ba
 being watched, when they next become valid, they will not be used until they
a385ba
 have stayed up for
a385ba
 .I delay_wait_checks
a385ba
-checks. Default is
a385ba
+checks. See "Shaky paths detection" below. Default is
a385ba
 .I no
a385ba
 .TP
a385ba
 .B delay_wait_checks
a385ba
@@ -537,9 +537,56 @@ online fails again within
a385ba
 checks, the next time it comes back online, it will marked and delayed, and not
a385ba
 used until it has passed
a385ba
 .I delay_wait_checks
a385ba
-checks. Default is
a385ba
+checks. See "Shaky paths detection" below. Default is
a385ba
 .I no
a385ba
 .TP
a385ba
+.B marginal_path_double_failed_time
a385ba
+One of the four parameters of supporting path check based on accounting IO
a385ba
+error such as intermittent error. When a path failed event occurs twice in
a385ba
+\fImarginal_path_double_failed_time\fR seconds due to an IO error and all the
a385ba
+other three parameters are set, multipathd will fail the path and enqueue
a385ba
+this path into a queue of which members are sent a couple of continuous
a385ba
+direct reading asynchronous IOs at a fixed sample rate of 10HZ to start IO
a385ba
+error accounting process. See "Shaky paths detection" below. Default is
a385ba
+\fIno\fR
a385ba
+.TP
a385ba
+.B marginal_path_err_sample_time
a385ba
+One of the four parameters of supporting path check based on accounting IO
a385ba
+error such as intermittent error. If it is set to a value no less than 120,
a385ba
+when a path fail event occurs twice in \fImarginal_path_double_failed_time\fR
a385ba
+second due to an IO error, multipathd will fail the path and enqueue this
a385ba
+path into a queue of which members are sent a couple of continuous direct
a385ba
+reading asynchronous IOs at a fixed sample rate of 10HZ to start the IO
a385ba
+accounting process for the path will last for
a385ba
+\fImarginal_path_err_sample_time\fR.
a385ba
+If the rate of IO error on a particular path is greater than the
a385ba
+\fImarginal_path_err_rate_threshold\fR, then the path will not reinstate for
a385ba
+\fImarginal_path_err_recheck_gap_time\fR seconds unless there is only one
a385ba
+active path. After \fImarginal_path_err_recheck_gap_time\fR expires, the path
a385ba
+will be requeueed for rechecking. If checking result is good enough, the
a385ba
+path will be reinstated. See "Shaky paths detection" below. Default is
a385ba
+\fIno\fR
a385ba
+.TP
a385ba
+.B marginal_path_err_rate_threshold
a385ba
+The error rate threshold as a permillage (1/1000). One of the four parameters
a385ba
+of supporting path check based on accounting IO error such as intermittent
a385ba
+error. Refer to \fImarginal_path_err_sample_time\fR. If the rate of IO errors
a385ba
+on a particular path is greater than this parameter, then the path will not
a385ba
+reinstate for \fImarginal_path_err_recheck_gap_time\fR seconds unless there is
a385ba
+only one active path. See "Shaky paths detection" below. Default is \fIno\fR
a385ba
+.TP
a385ba
+.B marginal_path_err_recheck_gap_time
a385ba
+One of the four parameters of supporting path check based on accounting IO
a385ba
+error such as intermittent error. Refer to
a385ba
+\fImarginal_path_err_sample_time\fR. If this parameter is set to a positive
a385ba
+value, the failed path of  which the IO error rate is larger than
a385ba
+\fImarginal_path_err_rate_threshold\fR will be kept in failed state for
a385ba
+\fImarginal_path_err_recheck_gap_time\fR seconds. When
a385ba
+\fImarginal_path_err_recheck_gap_time\fR seconds expires, the path will be
a385ba
+requeueed for checking. If checking result is good enough, the path will be
a385ba
+reinstated, or else it will keep failed. See "Shaky paths detection" below.
a385ba
+Default is \fIno\fR
a385ba
+.TP
a385ba
 .B missing_uev_wait_timeout
a385ba
 Controls how many seconds multipathd will wait, after a new multipath device
a385ba
 is created, to receive a change event from udev for the device, before
a385ba
@@ -771,6 +818,14 @@ section:
a385ba
 .TP
a385ba
 .B delay_wait_checks
a385ba
 .TP
a385ba
+.B marginal_path_err_sample_time
a385ba
+.TP
a385ba
+.B marginal_path_err_rate_threshold
a385ba
+.TP
a385ba
+.B marginal_path_err_recheck_gap_time
a385ba
+.TP
a385ba
+.B marginal_path_double_failed_time
a385ba
+.TP
a385ba
 .B skip_kpartx
a385ba
 .TP
a385ba
 .B max_sectors_kb
a385ba
@@ -877,6 +932,14 @@ section:
a385ba
 .TP
a385ba
 .B delay_wait_checks
a385ba
 .TP
a385ba
+.B marginal_path_err_sample_time
a385ba
+.TP
a385ba
+.B marginal_path_err_rate_threshold
a385ba
+.TP
a385ba
+.B marginal_path_err_recheck_gap_time
a385ba
+.TP
a385ba
+.B marginal_path_double_failed_time
a385ba
+.TP
a385ba
 .B skip_kpartx
a385ba
 .TP
a385ba
 .B max_sectors_kb
a385ba
@@ -887,6 +950,47 @@ section:
a385ba
 .RE
a385ba
 .PD
a385ba
 .LP
a385ba
+.SH "Shaky paths detection"
a385ba
+A common problem in SAN setups is the occurence of intermittent errors: a
a385ba
+path is unreachable, then reachable again for a short time, disappears again,
a385ba
+and so forth. This happens typically on unstable interconnects. It is
a385ba
+undesirable to switch pathgroups unnecessarily on such frequent, unreliable
a385ba
+events. \fImultipathd\fR supports two different methods for detecting this
a385ba
+situation and dealing with it. Both methods share the same basic mode of
a385ba
+operation: If a path is found to be \(dqshaky\(dq or \(dqflipping\(dq,
a385ba
+and appears to be in healthy status, it is not reinstated (put back to use)
a385ba
+immediately. Instead, it is watched for some time, and only reinstated
a385ba
+if the healthy state appears to be stable. The logic of determining
a385ba
+\(dqshaky\(dq condition, as well as the logic when to reinstate,
a385ba
+differs between the two methods.
a385ba
+.TP 8
a385ba
+.B \(dqdelay_checks\(dq failure tracking
a385ba
+If a path fails again within a
a385ba
+\fIdelay_watch_checks\fR interval after a failure, don't
a385ba
+reinstate it until it passes a \fIdelay_wait_checks\fR interval
a385ba
+in always good status.
a385ba
+The intervals are measured in \(dqticks\(dq, i.e. the
a385ba
+time between path checks by multipathd, which is variable and controlled by the
a385ba
+\fIpolling_interval\fR and \fImax_polling_interval\fR parameters.
a385ba
+.TP
a385ba
+.B \(dqmarginal_path\(dq failure tracking
a385ba
+If a second failure event (good->bad transition) occurs within
a385ba
+\fImarginal_path_double_failed_time\fR seconds after a failure, high-frequency
a385ba
+monitoring is started for the affected path: I/O is sent at a rate of 10 per
a385ba
+second. This is done for \fImarginal_path_err_sample_time\fR seconds. During
a385ba
+this period, the path is not reinstated. If the
a385ba
+rate of errors remains below \fImarginal_path_err_rate_threshold\fR during the
a385ba
+monitoring period, the path is reinstated. Otherwise, it
a385ba
+is kept in failed state for \fImarginal_path_err_recheck_gap_time\fR, and
a385ba
+after that, it is monitored again. For this method, time intervals are measured
a385ba
+in seconds.
a385ba
+.RE
a385ba
+.LP
a385ba
+See the documentation
a385ba
+of the individual options above for details.
a385ba
+It is \fBstrongly discouraged\fR to use more than one of these methods for any
a385ba
+given multipath map, because the two concurrent methods may interact in
a385ba
+unpredictable ways.
a385ba
 .SH "KNOWN ISSUES"
a385ba
 The usage of
a385ba
 .B queue_if_no_path
a385ba
Index: multipath-tools-130222/multipathd/cli_handlers.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/multipathd/cli_handlers.c
a385ba
+++ multipath-tools-130222/multipathd/cli_handlers.c
a385ba
@@ -721,7 +721,7 @@ int resize_map(struct multipath *mpp, un
a385ba
 
a385ba
 	mpp->size = size;
a385ba
 	update_mpp_paths(mpp, vecs->pathvec);
a385ba
-	setup_map(mpp, params, PARAMS_SIZE);
a385ba
+	setup_map(mpp, params, PARAMS_SIZE, vecs);
a385ba
 	mpp->action = ACT_RESIZE;
a385ba
 	if (domap(mpp, params) <= 0) {
a385ba
 		condlog(0, "%s: failed to resize map : %s", mpp->alias,
a385ba
Index: multipath-tools-130222/multipathd/main.c
a385ba
===================================================================
a385ba
--- multipath-tools-130222.orig/multipathd/main.c
a385ba
+++ multipath-tools-130222/multipathd/main.c
a385ba
@@ -56,6 +56,7 @@
a385ba
 #include <log.h>
a385ba
 #include <file.h>
a385ba
 #include <prkey.h>
a385ba
+#include <io_err_stat.h>
a385ba
 
a385ba
 #include "main.h"
a385ba
 #include "pidfile.h"
a385ba
@@ -274,7 +275,7 @@ retry:
a385ba
 	mpp->action = ACT_RELOAD;
a385ba
 
a385ba
 	extract_hwe_from_path(mpp);
a385ba
-	if (setup_map(mpp, params, PARAMS_SIZE)) {
a385ba
+	if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
a385ba
 		condlog(0, "%s: failed to setup new map in update", mpp->alias);
a385ba
 		retries = -1;
a385ba
 		goto fail;
a385ba
@@ -638,7 +639,7 @@ rescan:
a385ba
 	/*
a385ba
 	 * push the map to the device-mapper
a385ba
 	 */
a385ba
-	if (setup_map(mpp, params, PARAMS_SIZE)) {
a385ba
+	if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
a385ba
 		condlog(0, "%s: failed to setup map for addition of new "
a385ba
 			"path %s", mpp->alias, pp->dev);
a385ba
 		goto fail_map;
a385ba
@@ -771,7 +772,7 @@ ev_remove_path (struct path *pp, struct
a385ba
 			 */
a385ba
 		}
a385ba
 
a385ba
-		if (setup_map(mpp, params, PARAMS_SIZE)) {
a385ba
+		if (setup_map(mpp, params, PARAMS_SIZE, vecs)) {
a385ba
 			condlog(0, "%s: failed to setup map for"
a385ba
 				" removal of path %s", mpp->alias, pp->dev);
a385ba
 			goto fail;
a385ba
@@ -891,6 +892,41 @@ uev_update_path (struct uevent *uev, str
a385ba
 }
a385ba
 
a385ba
 static int
a385ba
+uev_pathfail_check(struct uevent *uev, struct vectors *vecs)
a385ba
+{
a385ba
+	char *action = NULL, *devt = NULL;
a385ba
+	struct path *pp;
a385ba
+	int r = 1;
a385ba
+
a385ba
+	action = uevent_get_dm_action(uev);
a385ba
+	if (!action)
a385ba
+		return 1;
a385ba
+	if (strncmp(action, "PATH_FAILED", 11))
a385ba
+		goto out;
a385ba
+	devt = uevent_get_dm_path(uev);
a385ba
+	if (!devt) {
a385ba
+		condlog(3, "%s: No DM_PATH in uevent", uev->kernel);
a385ba
+		goto out;
a385ba
+	}
a385ba
+
a385ba
+	pp = find_path_by_devt(vecs->pathvec, devt);
a385ba
+	if (!pp)
a385ba
+		goto out_devt;
a385ba
+	r = io_err_stat_handle_pathfail(pp);
a385ba
+
a385ba
+	if (r)
a385ba
+		condlog(3, "io_err_stat: %s: cannot handle pathfail uevent",
a385ba
+			pp->dev);
a385ba
+out_devt:
a385ba
+	FREE(devt);
a385ba
+	FREE(action);
a385ba
+	return r;
a385ba
+out:
a385ba
+	FREE(action);
a385ba
+	return 1;
a385ba
+}
a385ba
+
a385ba
+static int
a385ba
 map_discovery (struct vectors * vecs)
a385ba
 {
a385ba
 	struct multipath * mpp;
a385ba
@@ -974,6 +1010,14 @@ uev_trigger (struct uevent * uev, void *
a385ba
 	if (!strncmp(uev->kernel, "dm-", 3)) {
a385ba
 		if (!strncmp(uev->action, "change", 6)) {
a385ba
 			r = uev_add_map(uev, vecs);
a385ba
+
a385ba
+			/*
a385ba
+			 * the kernel-side dm-mpath issues a PATH_FAILED event
a385ba
+			 * when it encounters a path IO error. It is reason-
a385ba
+			 * able be the entry of path IO error accounting pro-
a385ba
+			 * cess.
a385ba
+			 */
a385ba
+			uev_pathfail_check(uev, vecs);
a385ba
 			goto out;
a385ba
 		}
a385ba
 		if (!strncmp(uev->action, "remove", 6)) {
a385ba
@@ -1405,6 +1449,17 @@ check_path (struct vectors * vecs, struc
a385ba
 		return;
a385ba
 
a385ba
 	if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
a385ba
+	    pp->io_err_disable_reinstate && need_io_err_check(pp)) {
a385ba
+		pp->state = PATH_SHAKY;
a385ba
+		/*
a385ba
+		 * to reschedule as soon as possible,so that this path can
a385ba
+		 * be recoverd in time
a385ba
+		 */
a385ba
+		pp->tick = 1;
a385ba
+		return;
a385ba
+	}
a385ba
+
a385ba
+	if ((newstate == PATH_UP || newstate == PATH_GHOST) &&
a385ba
 	     pp->wait_checks > 0) {
a385ba
 		if (pp->mpp && pp->mpp->nr_active > 0) {
a385ba
 			pp->state = PATH_DELAYED;
a385ba
@@ -1955,6 +2010,7 @@ child (void * param)
a385ba
 	setup_thread_attr(&misc_attr, 64 * 1024, 1);
a385ba
 	setup_thread_attr(&uevent_attr, 128 * 1024, 1);
a385ba
 	setup_thread_attr(&waiter_attr, 32 * 1024, 1);
a385ba
+	setup_thread_attr(&io_err_stat_attr, 32 * 1024, 0);
a385ba
 
a385ba
 	if (logsink) {
a385ba
 		setup_thread_attr(&log_attr, 64 * 1024, 0);
a385ba
@@ -2097,6 +2153,8 @@ child (void * param)
a385ba
 	*/
a385ba
 	cleanup_checkers();
a385ba
 	cleanup_prio();
a385ba
+	stop_io_err_stat_thread();
a385ba
+	pthread_attr_destroy(&io_err_stat_attr);
a385ba
 
a385ba
 	dm_lib_release();
a385ba
 	dm_lib_exit();