Blame SOURCES/0057-UP-fix-tmo.patch

38852f
---
38852f
 libmultipath/discovery.c |  109 +++++++++++++++++++++++++++++++++++++----------
38852f
 libmultipath/sysfs.c     |   86 +++++++++++++++++++++++++++++++------
38852f
 libmultipath/sysfs.h     |    2 
38852f
 3 files changed, 161 insertions(+), 36 deletions(-)
38852f
38852f
Index: multipath-tools-130222/libmultipath/discovery.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/discovery.c
38852f
+++ multipath-tools-130222/libmultipath/discovery.c
38852f
@@ -162,7 +162,6 @@ declare_sysfs_get_str(cutype);
38852f
 declare_sysfs_get_str(vendor);
38852f
 declare_sysfs_get_str(model);
38852f
 declare_sysfs_get_str(rev);
38852f
-declare_sysfs_get_str(state);
38852f
 declare_sysfs_get_str(dev);
38852f
 
38852f
 int
38852f
@@ -315,9 +314,14 @@ static void
38852f
 sysfs_set_rport_tmo(struct multipath *mpp, struct path *pp)
38852f
 {
38852f
 	struct udev_device *rport_dev = NULL;
38852f
-	char value[11];
38852f
+	char value[16];
38852f
 	char rport_id[32];
38852f
+	int delay_fast_io_fail = 0;
38852f
+	int current_dev_loss = 0;
38852f
+	int ret;
38852f
 
38852f
+	if (!mpp->dev_loss && mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)
38852f
+		return;
38852f
 	sprintf(rport_id, "rport-%d:%d-%d",
38852f
 		pp->sg_id.host_no, pp->sg_id.channel, pp->sg_id.transport_id);
38852f
 	rport_dev = udev_device_new_from_subsystem_sysname(conf->udev,
38852f
@@ -330,33 +334,85 @@ sysfs_set_rport_tmo(struct multipath *mp
38852f
 	condlog(4, "target%d:%d:%d -> %s", pp->sg_id.host_no,
38852f
 		pp->sg_id.channel, pp->sg_id.scsi_id, rport_id);
38852f
 
38852f
-	snprintf(value, 11, "%u", mpp->dev_loss);
38852f
-	if (mpp->dev_loss &&
38852f
-	    sysfs_attr_set_value(rport_dev, "dev_loss_tmo", value, 11) <= 0) {
38852f
-		if ((mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET ||
38852f
-		     mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
38852f
-		    && mpp->dev_loss > 600) {
38852f
-			condlog(3, "%s: limiting dev_loss_tmo to 600, since "
38852f
-				"fast_io_fail is not set", mpp->alias);
38852f
-			snprintf(value, 11, "%u", 600);
38852f
-			if (sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
38852f
-						 value, 11) <= 0)
38852f
-				condlog(0, "%s failed to set dev_loss_tmo",
38852f
-					mpp->alias);
38852f
+	memset(value, 0, 16);
38852f
+	if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
38852f
+		ret = sysfs_attr_get_value(rport_dev, "dev_loss_tmo",
38852f
+					   value, 16);
38852f
+		if (ret <= 0) {
38852f
+			condlog(0, "%s: failed to read dev_loss_tmo value, "
38852f
+				"error %d", rport_id, -ret);
38852f
 			goto out;
38852f
 		}
38852f
+		if (sscanf(value, "%u\n", &current_dev_loss) != 1) {
38852f
+			condlog(0, "%s: Cannot parse dev_loss_tmo "
38852f
+				"attribute '%s'", rport_id, value);
38852f
+			goto out;
38852f
+		}
38852f
+		if ((mpp->dev_loss &&
38852f
+		     mpp->fast_io_fail >= (int)mpp->dev_loss) ||
38852f
+	            (!mpp->dev_loss &&
38852f
+                     mpp->fast_io_fail >= (int)current_dev_loss)) {
38852f
+			condlog(3, "%s: limiting fast_io_fail_tmo to %d, since "
38852f
+                        	"it must be less than dev_loss_tmo",
38852f
+				rport_id, mpp->dev_loss - 1);
38852f
+			if (mpp->dev_loss)
38852f
+				mpp->fast_io_fail = mpp->dev_loss - 1;
38852f
+			else
38852f
+				mpp->fast_io_fail = current_dev_loss - 1;
38852f
+		}
38852f
+		if (mpp->fast_io_fail >= (int)current_dev_loss)
38852f
+			delay_fast_io_fail = 1;
38852f
+	}
38852f
+	if (mpp->dev_loss > 600 &&
38852f
+	    (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF ||
38852f
+             mpp->fast_io_fail == MP_FAST_IO_FAIL_UNSET)) {
38852f
+		condlog(3, "%s: limiting dev_loss_tmo to 600, since "
38852f
+			"fast_io_fail is unset or off", rport_id);
38852f
+		mpp->dev_loss = 600;
38852f
 	}
38852f
-	if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET){
38852f
+	if (mpp->fast_io_fail != MP_FAST_IO_FAIL_UNSET) {
38852f
 		if (mpp->fast_io_fail == MP_FAST_IO_FAIL_OFF)
38852f
 			sprintf(value, "off");
38852f
 		else if (mpp->fast_io_fail == MP_FAST_IO_FAIL_ZERO)
38852f
 			sprintf(value, "0");
38852f
+		else if (delay_fast_io_fail)
38852f
+			snprintf(value, 16, "%u", current_dev_loss - 1);
38852f
 		else
38852f
-			snprintf(value, 11, "%u", mpp->fast_io_fail);
38852f
-		if (sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
38852f
-					 value, 11) <= 0) {
38852f
-			condlog(0, "%s failed to set fast_io_fail_tmo",
38852f
-				mpp->alias);
38852f
+			snprintf(value, 16, "%u", mpp->fast_io_fail);
38852f
+		ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
38852f
+					   value, strlen(value));
38852f
+		if (ret <= 0) {
38852f
+			if (ret == -EBUSY)
38852f
+				condlog(3, "%s: rport blocked", rport_id);
38852f
+			else
38852f
+				condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
38852f
+					rport_id, value, -ret);
38852f
+			goto out;
38852f
+		}
38852f
+	}
38852f
+	if (mpp->dev_loss) {
38852f
+		snprintf(value, 16, "%u", mpp->dev_loss);
38852f
+		ret = sysfs_attr_set_value(rport_dev, "dev_loss_tmo",
38852f
+					   value, strlen(value));
38852f
+		if (ret <= 0) {
38852f
+			if (ret == -EBUSY)
38852f
+				condlog(3, "%s: rport blocked", rport_id);
38852f
+			else
38852f
+				condlog(0, "%s: failed to set dev_loss_tmo to %s, error %d",
38852f
+					rport_id, value, -ret);
38852f
+			goto out;
38852f
+		}
38852f
+	}
38852f
+	if (delay_fast_io_fail) {
38852f
+		snprintf(value, 16, "%u", mpp->fast_io_fail);
38852f
+		ret = sysfs_attr_set_value(rport_dev, "fast_io_fail_tmo",
38852f
+					   value, strlen(value));
38852f
+		if (ret <= 0) {
38852f
+			if (ret == -EBUSY)
38852f
+				condlog(3, "%s: rport blocked", rport_id);
38852f
+			else
38852f
+				condlog(0, "%s: failed to set fast_io_fail_tmo to %s, error %d",
38852f
+					rport_id, value, -ret);
38852f
 		}
38852f
 	}
38852f
 out:
38852f
@@ -394,7 +450,7 @@ sysfs_set_session_tmo(struct multipath *
38852f
 		} else {
38852f
 			snprintf(value, 11, "%u", mpp->fast_io_fail);
38852f
 			if (sysfs_attr_set_value(session_dev, "recovery_tmo",
38852f
-						 value, 11)) {
38852f
+						 value, 11) <= 0) {
38852f
 				condlog(3, "%s: Failed to set recovery_tmo, "
38852f
 					" error %d", pp->dev, errno);
38852f
 			}
38852f
@@ -752,6 +808,9 @@ cciss_sysfs_pathinfo (struct path * pp)
38852f
 static int
38852f
 common_sysfs_pathinfo (struct path * pp)
38852f
 {
38852f
+	if (!pp)
38852f
+		return 1;
38852f
+
38852f
 	if (!pp->udev) {
38852f
 		condlog(4, "%s: udev not initialised", pp->dev);
38852f
 		return 1;
38852f
@@ -793,7 +852,8 @@ path_offline (struct path * pp)
38852f
 		return PATH_DOWN;
38852f
 	}
38852f
 
38852f
-	if (sysfs_get_state(parent, buff, SCSI_STATE_SIZE))
38852f
+	memset(buff, 0x0, SCSI_STATE_SIZE);
38852f
+	if (sysfs_attr_get_value(parent, "state", buff, SCSI_STATE_SIZE) <= 0)
38852f
 		return PATH_DOWN;
38852f
 
38852f
 	condlog(3, "%s: path state = %s", pp->dev, buff);
38852f
@@ -983,6 +1043,9 @@ pathinfo (struct path *pp, vector hwtabl
38852f
 {
38852f
 	int path_state;
38852f
 
38852f
+	if (!pp)
38852f
+		return 1;
38852f
+
38852f
 	condlog(3, "%s: mask = 0x%x", pp->dev, mask);
38852f
 
38852f
 	/*
38852f
Index: multipath-tools-130222/libmultipath/sysfs.c
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/sysfs.c
38852f
+++ multipath-tools-130222/libmultipath/sysfs.c
38852f
@@ -38,7 +38,12 @@
38852f
 #include "debug.h"
38852f
 #include "devmapper.h"
38852f
 
38852f
-ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,
38852f
+/*
38852f
+ * When we modify an attribute value we cannot rely on libudev for now,
38852f
+ * as libudev lacks the capability to update an attribute value.
38852f
+ * So for modified attributes we need to implement our own function.
38852f
+ */
38852f
+ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name,
38852f
 			     char * value, size_t value_len)
38852f
 {
38852f
 	char devpath[PATH_SIZE];
38852f
@@ -54,28 +59,83 @@ ssize_t sysfs_attr_set_value(struct udev
38852f
 	condlog(4, "open '%s'", devpath);
38852f
 	if (stat(devpath, &statbuf) != 0) {
38852f
 		condlog(4, "stat '%s' failed: %s", devpath, strerror(errno));
38852f
-		return 0;
38852f
+		return -errno;
38852f
 	}
38852f
 
38852f
 	/* skip directories */
38852f
-	if (S_ISDIR(statbuf.st_mode))
38852f
-		return 0;
38852f
+	if (S_ISDIR(statbuf.st_mode)) {
38852f
+		condlog(4, "%s is a directory", devpath);
38852f
+		return -EISDIR;
38852f
+	}
38852f
 
38852f
 	/* skip non-writeable files */
38852f
-	if ((statbuf.st_mode & S_IWUSR) == 0)
38852f
+	if ((statbuf.st_mode & S_IRUSR) == 0) {
38852f
+		condlog(4, "%s is not readable", devpath);
38852f
+		return -EPERM;
38852f
+	}
38852f
+
38852f
+	/* read attribute value */
38852f
+	fd = open(devpath, O_RDONLY);
38852f
+	if (fd < 0) {
38852f
+		condlog(4, "attribute '%s' can not be opened: %s",
38852f
+			devpath, strerror(errno));
38852f
+		return -errno;
38852f
+	}
38852f
+	size = read(fd, value, value_len);
38852f
+	if (size < 0) {
38852f
+		condlog(4, "read from %s failed: %s", devpath, strerror(errno));
38852f
+		size = -errno;
38852f
+	} else if (size == value_len) {
38852f
+		condlog(4, "overflow while reading from %s", devpath);
38852f
+		size = 0;
38852f
+	}
38852f
+
38852f
+	close(fd);
38852f
+	return size;
38852f
+}
38852f
+
38852f
+ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,
38852f
+			     char * value, size_t value_len)
38852f
+{
38852f
+	char devpath[PATH_SIZE];
38852f
+	struct stat statbuf;
38852f
+	int fd;
38852f
+	ssize_t size = -1;
38852f
+
38852f
+	if (!dev || !attr_name || !value || !value_len)
38852f
 		return 0;
38852f
 
38852f
+	snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev),
38852f
+		 attr_name);
38852f
+	condlog(4, "open '%s'", devpath);
38852f
+	if (stat(devpath, &statbuf) != 0) {
38852f
+		condlog(4, "stat '%s' failed: %s", devpath, strerror(errno));
38852f
+		return -errno;
38852f
+	}
38852f
+
38852f
+	/* skip directories */
38852f
+	if (S_ISDIR(statbuf.st_mode)) {
38852f
+		condlog(4, "%s is a directory", devpath);
38852f
+		return -EISDIR;
38852f
+	}
38852f
+
38852f
+	/* skip non-writeable files */
38852f
+	if ((statbuf.st_mode & S_IWUSR) == 0) {
38852f
+		condlog(4, "%s is not writeable", devpath);
38852f
+		return -EPERM;
38852f
+	}
38852f
+
38852f
 	/* write attribute value */
38852f
 	fd = open(devpath, O_WRONLY);
38852f
 	if (fd < 0) {
38852f
 		condlog(4, "attribute '%s' can not be opened: %s",
38852f
 			devpath, strerror(errno));
38852f
-		return 0;
38852f
+		return -errno;
38852f
 	}
38852f
 	size = write(fd, value, value_len);
38852f
 	if (size < 0) {
38852f
 		condlog(4, "write to %s failed: %s", devpath, strerror(errno));
38852f
-		size = 0;
38852f
+		size = -errno;
38852f
 	} else if (size < value_len) {
38852f
 		condlog(4, "tried to write %ld to %s. Wrote %ld",
38852f
 			(long)value_len, devpath, (long)size);
38852f
@@ -89,14 +149,14 @@ ssize_t sysfs_attr_set_value(struct udev
38852f
 int
38852f
 sysfs_get_size (struct path *pp, unsigned long long * size)
38852f
 {
38852f
-	const char * attr;
38852f
+	char attr[255];
38852f
 	int r;
38852f
 
38852f
-	if (!pp->udev)
38852f
+	if (!pp->udev || !size)
38852f
 		return 1;
38852f
 
38852f
-	attr = udev_device_get_sysattr_value(pp->udev, "size");
38852f
-	if (!attr) {
38852f
+	attr[0] = '\0';
38852f
+	if (sysfs_attr_get_value(pp->udev, "size", attr, 255) == 0) {
38852f
 		condlog(3, "%s: No size attribute in sysfs", pp->dev);
38852f
 		return 1;
38852f
 	}
38852f
@@ -104,8 +164,8 @@ sysfs_get_size (struct path *pp, unsigne
38852f
 	r = sscanf(attr, "%llu\n", size);
38852f
 
38852f
 	if (r != 1) {
38852f
-		condlog(3, "%s: Cannot parse size attribute '%s'",
38852f
-			pp->dev, attr);
38852f
+		condlog(3, "%s: Cannot parse size attribute", pp->dev);
38852f
+		*size = 0;
38852f
 		return 1;
38852f
 	}
38852f
 
38852f
Index: multipath-tools-130222/libmultipath/sysfs.h
38852f
===================================================================
38852f
--- multipath-tools-130222.orig/libmultipath/sysfs.h
38852f
+++ multipath-tools-130222/libmultipath/sysfs.h
38852f
@@ -7,6 +7,8 @@
38852f
 
38852f
 ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name,
38852f
 			     char * value, size_t value_len);
38852f
+ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name,
38852f
+			     char * value, size_t value_len);
38852f
 int sysfs_get_size (struct path *pp, unsigned long long * size);
38852f
 int sysfs_check_holders(char * check_devt, char * new_devt);
38852f
 #endif