Blame SOURCES/0027-Make-rescan-run-in-parallel.patch

6c64be
From 3c5ec3835d5fd57a993cb814ecd74b48419a7459 Mon Sep 17 00:00:00 2001
6c64be
From: Mike Christie <michaelc@cs.wisc.edu>
6c64be
Date: Mon, 29 Jul 2013 14:13:36 -0500
6c64be
Subject: Make rescan run in parallel
6c64be
6c64be
Patch from Saggi Mizrahi:
6c64be
6c64be
This fixes a problem where a host which is inaccessible would block the
6c64be
scan of other hosts in the system.
6c64be
6c64be
[compilation and minor cosmetic fixes by Mike Christie]
6c64be
Signed-off-by: Saggi Mizrahi <smizrahi@redhat.com>
6c64be
---
6c64be
 usr/host.c         |  2 +-
6c64be
 usr/initiator.c    |  3 ++-
6c64be
 usr/iscsi_sysfs.c  | 61 +++++++++++++++++++++++++++++++++++++++++++++---------
6c64be
 usr/iscsi_sysfs.h  |  3 ++-
6c64be
 usr/iscsiadm.c     | 18 +++++++++-------
6c64be
 usr/iscsid.c       |  3 ++-
6c64be
 usr/session_info.c |  4 ++--
6c64be
 usr/session_mgmt.c |  7 ++++---
6c64be
 8 files changed, 74 insertions(+), 27 deletions(-)
6c64be
6c64be
diff --git a/usr/host.c b/usr/host.c
6c64be
index b03e50f..1fcb350 100644
6c64be
--- a/usr/host.c
6c64be
+++ b/usr/host.c
6c64be
@@ -242,7 +242,7 @@ static int host_info_print_tree(void *data, struct host_info *hinfo)
6c64be
 	link_info.data = &hinfo->host_no;
6c64be
 
6c64be
 	err = iscsi_sysfs_for_each_session(&link_info, &num_found,
6c64be
-					   session_info_create_list);
6c64be
+					   session_info_create_list, 0);
6c64be
 	if (err || !num_found)
6c64be
 		return 0;
6c64be
 
6c64be
diff --git a/usr/initiator.c b/usr/initiator.c
6c64be
index 86df222..a3b24b7 100644
6c64be
--- a/usr/initiator.c
6c64be
+++ b/usr/initiator.c
6c64be
@@ -1855,7 +1855,8 @@ static int session_is_running(node_rec_t *rec)
6c64be
 	if (session_find_by_rec(rec))
6c64be
 		return 1;
6c64be
 
6c64be
-	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_session))
6c64be
+	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_session,
6c64be
+					 0))
6c64be
 		return 1;
6c64be
 
6c64be
 	return 0;
6c64be
diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
6c64be
index 64a4ce7..aed10a3 100644
6c64be
--- a/usr/iscsi_sysfs.c
6c64be
+++ b/usr/iscsi_sysfs.c
6c64be
@@ -24,6 +24,7 @@
6c64be
 #include <dirent.h>
6c64be
 #include <sys/types.h>
6c64be
 #include <sys/stat.h>
6c64be
+#include <sys/wait.h>
6c64be
 
6c64be
 #include "log.h"
6c64be
 #include "initiator.h"
6c64be
@@ -1167,11 +1168,13 @@ int iscsi_sysfs_get_sessioninfo_by_id(struct session_info *info, char *session)
6c64be
 }
6c64be
 
6c64be
 int iscsi_sysfs_for_each_session(void *data, int *nr_found,
6c64be
-				 iscsi_sysfs_session_op_fn *fn)
6c64be
+				 iscsi_sysfs_session_op_fn *fn,
6c64be
+				 int in_parallel)
6c64be
 {
6c64be
 	struct dirent **namelist;
6c64be
-	int rc = 0, n, i;
6c64be
+	int rc = 0, n, i, chldrc = 0;
6c64be
 	struct session_info *info;
6c64be
+	pid_t pid = 0;
6c64be
 
6c64be
 	info = calloc(1, sizeof(*info));
6c64be
 	if (!info)
6c64be
@@ -1193,14 +1196,52 @@ int iscsi_sysfs_for_each_session(void *data, int *nr_found,
6c64be
 			continue;
6c64be
 		}
6c64be
 
6c64be
-		rc = fn(data, info);
6c64be
-		if (rc > 0)
6c64be
-			break;
6c64be
-		else if (rc == 0)
6c64be
-			(*nr_found)++;
6c64be
-		else
6c64be
-			/* if less than zero it means it was not a match */
6c64be
-			rc = 0;
6c64be
+		if (in_parallel) {
6c64be
+			pid = fork();
6c64be
+		}
6c64be
+		if (pid == 0) {
6c64be
+			rc = fn(data, info);
6c64be
+			if (in_parallel) {
6c64be
+				exit(rc);
6c64be
+			} else {
6c64be
+				if (rc > 0) {
6c64be
+					break;
6c64be
+				} else if (rc == 0) {
6c64be
+					(*nr_found)++;
6c64be
+				} else {
6c64be
+					/* if less than zero it means it was not a match */
6c64be
+					rc = 0;
6c64be
+				}
6c64be
+			}
6c64be
+		} else if (pid < 0) {
6c64be
+			log_error("could not fork() for session %s, err %d",
6c64be
+				   namelist[i]->d_name, errno);
6c64be
+		}
6c64be
+	}
6c64be
+
6c64be
+	if (in_parallel) {
6c64be
+		while (1) {
6c64be
+			if (wait(&chldrc) < 0) {
6c64be
+				/*
6c64be
+				 * ECHILD means no more children which is
6c64be
+				 * expected to happen sooner or later.
6c64be
+				 */
6c64be
+				if (errno != ECHILD) {
6c64be
+					rc = errno;
6c64be
+				}
6c64be
+				break;
6c64be
+			}
6c64be
+
6c64be
+			if ((chldrc > 0) && (rc == 0)) {
6c64be
+				/*
6c64be
+				 * The non-parallel code path returns the first
6c64be
+				 * error so this keeps the same semantics.
6c64be
+				 */
6c64be
+				rc = chldrc;
6c64be
+			} else if (chldrc == 0) {
6c64be
+				(*nr_found)++;
6c64be
+			}
6c64be
+		}
6c64be
 	}
6c64be
 
6c64be
 	for (i = 0; i < n; i++)
6c64be
diff --git a/usr/iscsi_sysfs.h b/usr/iscsi_sysfs.h
6c64be
index d130d36..9a56105 100644
6c64be
--- a/usr/iscsi_sysfs.h
6c64be
+++ b/usr/iscsi_sysfs.h
6c64be
@@ -51,7 +51,8 @@ extern int iscsi_sysfs_for_each_iface_on_host(void *data, uint32_t host_no,
6c64be
 					      int *nr_found,
6c64be
 					      iscsi_sysfs_iface_op_fn *fn);
6c64be
 extern int iscsi_sysfs_for_each_session(void *data, int *nr_found,
6c64be
-					iscsi_sysfs_session_op_fn *fn);
6c64be
+					iscsi_sysfs_session_op_fn *fn,
6c64be
+					int in_parallel);
6c64be
 extern int iscsi_sysfs_for_each_host(void *data, int *nr_found,
6c64be
 				     iscsi_sysfs_host_op_fn *fn);
6c64be
 extern uint32_t iscsi_sysfs_get_host_no_from_sid(uint32_t sid, int *err);
6c64be
diff --git a/usr/iscsiadm.c b/usr/iscsiadm.c
6c64be
index 5030894..da0a3ec 100644
6c64be
--- a/usr/iscsiadm.c
6c64be
+++ b/usr/iscsiadm.c
6c64be
@@ -347,7 +347,8 @@ match_startup_mode(node_rec_t *rec, char *mode)
6c64be
 }
6c64be
 
6c64be
 static int
6c64be
-for_each_session(struct node_rec *rec, iscsi_sysfs_session_op_fn *fn)
6c64be
+for_each_session(struct node_rec *rec, iscsi_sysfs_session_op_fn *fn,
6c64be
+		 int in_parallel)
6c64be
 {
6c64be
 	int err, num_found = 0;
6c64be
 
6c64be
@@ -355,7 +356,8 @@ for_each_session(struct node_rec *rec, iscsi_sysfs_session_op_fn *fn)
6c64be
 		num_found = 1;
6c64be
 		err = fn(rec, rec->session.info);
6c64be
 	} else {
6c64be
-		err = iscsi_sysfs_for_each_session(rec, &num_found, fn);
6c64be
+		err = iscsi_sysfs_for_each_session(rec, &num_found, fn,
6c64be
+						   in_parallel);
6c64be
 	}
6c64be
 	if (err)
6c64be
 		log_error("Could not execute operation on all sessions: %s",
6c64be
@@ -435,7 +437,7 @@ logout_by_startup(char *mode)
6c64be
 	rc = iscsi_logout_portals(mode, &nr_found, 1, __logout_by_startup);
6c64be
 	if (rc == ISCSI_ERR_NO_OBJS_FOUND)
6c64be
 		log_error("No matching sessions found");
6c64be
-	return rc; 
6c64be
+	return rc;
6c64be
 }
6c64be
 
6c64be
 struct startup_data {
6c64be
@@ -479,7 +481,7 @@ __do_leading_login(void *data, struct list_head *list, struct node_rec *rec)
6c64be
 	 * If there is an existing session that matcthes the target,
6c64be
 	 * the leading login is complete.
6c64be
 	 */
6c64be
-	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_target)) {
6c64be
+	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_target, 0)) {
6c64be
 		log_debug(1, "Skipping %s: Already a session for that target",
6c64be
 			  rec->name);
6c64be
 		return -1;
6c64be
@@ -579,7 +581,7 @@ login_by_startup(char *mode)
6c64be
 		list_for_each_entry_safe(rec, tmp_rec, &startup.leading_logins,
6c64be
 					 list) {
6c64be
 			if (!iscsi_sysfs_for_each_session(rec, &nr_found,
6c64be
-							  iscsi_match_target))
6c64be
+							  iscsi_match_target, 0))
6c64be
 				missed_leading_login++;
6c64be
 			/*
6c64be
 			 * Cleanup the list, since 'iscsi_login_portals_safe'
6c64be
@@ -1210,7 +1212,7 @@ do_target_discovery(discovery_rec_t *drec, struct list_head *ifaces,
6c64be
 		host_no = iscsi_sysfs_get_host_no_from_hwinfo(iface, &rc);
6c64be
 		if (rc || host_no == -1) {
6c64be
 			log_debug(1, "Could not match iface" iface_fmt " to "
6c64be
-				  "host.", iface_str(iface)); 
6c64be
+				  "host.", iface_str(iface));
6c64be
 			/* try software iscsi */
6c64be
 			continue;
6c64be
 		}
6c64be
@@ -2116,12 +2118,12 @@ static int exec_node_op(int op, int do_login, int do_logout,
6c64be
 	}
6c64be
 
6c64be
 	if (do_rescan) {
6c64be
-		rc = for_each_session(rec, rescan_portal);
6c64be
+		rc = for_each_session(rec, rescan_portal, 1);
6c64be
 		goto out;
6c64be
 	}
6c64be
 
6c64be
 	if (do_stats) {
6c64be
-		rc = for_each_session(rec, session_stats);
6c64be
+		rc = for_each_session(rec, session_stats, 0);
6c64be
 		goto out;
6c64be
 	}
6c64be
 
6c64be
diff --git a/usr/iscsid.c b/usr/iscsid.c
6c64be
index b4bb65b..8f19220 100644
6c64be
--- a/usr/iscsid.c
6c64be
+++ b/usr/iscsid.c
6c64be
@@ -511,7 +511,8 @@ int main(int argc, char *argv[])
6c64be
 	if (pid == 0) {
6c64be
 		int nr_found = 0;
6c64be
 		/* child */
6c64be
-		iscsi_sysfs_for_each_session(NULL, &nr_found, sync_session);
6c64be
+		/* TODO - test with async support enabled */
6c64be
+		iscsi_sysfs_for_each_session(NULL, &nr_found, sync_session, 0);
6c64be
 		exit(0);
6c64be
 	} else if (pid < 0) {
6c64be
 		log_error("Fork failed error %d: existing sessions"
6c64be
diff --git a/usr/session_info.c b/usr/session_info.c
6c64be
index 1f84c49..de156c6 100644
6c64be
--- a/usr/session_info.c
6c64be
+++ b/usr/session_info.c
6c64be
@@ -368,7 +368,7 @@ int session_info_print(int info_level, struct session_info *info, int do_show)
6c64be
 			num_found = 1;
6c64be
 		} else
6c64be
 			err = iscsi_sysfs_for_each_session(info, &num_found,
6c64be
-						   session_info_print_flat);
6c64be
+						   session_info_print_flat, 0);
6c64be
 		break;
6c64be
 	case 3:
6c64be
 		version = iscsi_sysfs_get_iscsi_kernel_version();
6c64be
@@ -403,7 +403,7 @@ int session_info_print(int info_level, struct session_info *info, int do_show)
6c64be
 		link_info.match_fn = NULL;
6c64be
 
6c64be
 		err = iscsi_sysfs_for_each_session(&link_info, &num_found,
6c64be
-						   session_info_create_list);
6c64be
+						   session_info_create_list, 0);
6c64be
 		if (err || !num_found)
6c64be
 			break;
6c64be
 
6c64be
diff --git a/usr/session_mgmt.c b/usr/session_mgmt.c
6c64be
index 0b7373f..87b8e00 100644
6c64be
--- a/usr/session_mgmt.c
6c64be
+++ b/usr/session_mgmt.c
6c64be
@@ -172,7 +172,7 @@ int iscsi_login_portal(void *data, struct list_head *list, struct node_rec *rec)
6c64be
 	 * that are missing.
6c64be
 	 */
6c64be
 	rc = iscsi_sysfs_for_each_session(rec, &session_count,
6c64be
-					  iscsi_match_session_count);
6c64be
+					  iscsi_match_session_count, 0);
6c64be
 	if (rc) {
6c64be
 		log_error("Could not count current number of sessions");
6c64be
 		goto done;
6c64be
@@ -421,7 +421,7 @@ int iscsi_logout_portals(void *data, int *nr_found, int wait,
6c64be
 	*nr_found = 0;
6c64be
 
6c64be
 	err = iscsi_sysfs_for_each_session(&link_info, nr_found,
6c64be
-					   session_info_create_list);
6c64be
+					   session_info_create_list, 0);
6c64be
 	if (err && !list_empty(&session_list))
6c64be
 		log_error("Could not read in all sessions: %s",
6c64be
 			  iscsi_err_to_str(err));
6c64be
@@ -466,7 +466,8 @@ free_list:
6c64be
 int iscsi_check_for_running_session(struct node_rec *rec)
6c64be
 {
6c64be
 	int nr_found = 0;
6c64be
-	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_session))
6c64be
+	if (iscsi_sysfs_for_each_session(rec, &nr_found, iscsi_match_session,
6c64be
+					 0))
6c64be
 		return 1;
6c64be
 	return 0;
6c64be
 }
6c64be
-- 
6c64be
1.8.1.4
6c64be