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