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

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