[stgt] [PATCH 07/12] Define bs_finish data structure to hadle it per target

Chandra Seetharaman sekharan at us.ibm.com
Mon Sep 27 07:21:04 CEST 2010


Define a new data structure bs_finish to enable handling bs finishes
by individual threads of a target.

Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp>
Signed-off-by: Chandra Seetharaman <sekharan at us.ibm.com>

---
 usr/bs.c     |   49 ++++++++++++++++++++++++++-----------------------
 usr/target.c |    5 +++++
 usr/target.h |    2 ++
 usr/tgtd.h   |    8 ++++++++
 4 files changed, 41 insertions(+), 23 deletions(-)

Index: tgt-1.0.8.4/usr/tgtd.h
===================================================================
--- tgt-1.0.8.4.orig/usr/tgtd.h
+++ tgt-1.0.8.4/usr/tgtd.h
@@ -336,6 +336,14 @@ struct event_data {
 
 extern int sig_fd;
 
+struct bs_finish {
+	struct list_head finished_list;
+	pthread_mutex_t finished_lock;
+	pid_t	th_id;
+};
+
+void bs_sig_request_done(int fd, int events, void *data);
+
 int do_tgt_event_add(int efd, struct list_head *list, int fd, int events,
 		     event_handler_t handler, void *data);
 void do_tgt_event_del(int efd, struct list_head *list, int fd);
Index: tgt-1.0.8.4/usr/bs.c
===================================================================
--- tgt-1.0.8.4.orig/usr/bs.c
+++ tgt-1.0.8.4/usr/bs.c
@@ -36,12 +36,12 @@
 #include "tgtd.h"
 #include "tgtadm_error.h"
 #include "util.h"
+#include "target.h"
 #include "bs_thread.h"
 
 static LIST_HEAD(bst_list);
 
-static LIST_HEAD(finished_list);
-static pthread_mutex_t finished_lock;
+struct bs_finish bs_finish, *bsf = &bs_finish;
 
 int sig_fd = -1;
 
@@ -87,15 +87,15 @@ retry:
 		goto out;
 	}
 
-	pthread_mutex_lock(&finished_lock);
+	pthread_mutex_lock(&bsf->finished_lock);
 retest:
-	if (list_empty(&finished_list)) {
-		pthread_cond_wait(&finished_cond, &finished_lock);
+	if (list_empty(&bsf->finished_list)) {
+		pthread_cond_wait(&finished_cond, &bsf->finished_lock);
 		goto retest;
 	}
 
-	while (!list_empty(&finished_list)) {
-		cmd = list_first_entry(&finished_list,
+	while (!list_empty(&bsf->finished_list)) {
+		cmd = list_first_entry(&bsf->finished_list,
 				 struct scsi_cmd, bs_list);
 
 		dprintf("found %p\n", cmd);
@@ -104,7 +104,7 @@ retest:
 		list_add_tail(&cmd->bs_list, &ack_list);
 	}
 
-	pthread_mutex_unlock(&finished_lock);
+	pthread_mutex_unlock(&bsf->finished_lock);
 
 	nr = 1;
 rewrite:
@@ -154,9 +154,10 @@ rewrite:
 	}
 }
 
-static void bs_sig_request_done(int fd, int events, void *data)
+void bs_sig_request_done(int fd, int events, void *data)
 {
 	int ret;
+	struct bs_finish *b = data;
 	struct scsi_cmd *cmd;
 	struct signalfd_siginfo siginfo[16];
 	LIST_HEAD(list);
@@ -166,9 +167,9 @@ static void bs_sig_request_done(int fd, 
 		return;
 	}
 
-	pthread_mutex_lock(&finished_lock);
-	list_splice_init(&finished_list, &list);
-	pthread_mutex_unlock(&finished_lock);
+	pthread_mutex_lock(&b->finished_lock);
+	list_splice_init(&b->finished_list, &list);
+	pthread_mutex_unlock(&b->finished_lock);
 
 	while (!list_empty(&list)) {
 		cmd = list_first_entry(&list, struct scsi_cmd, bs_list);
@@ -184,6 +185,7 @@ static void *bs_thread_worker_fn(void *a
 	struct bs_thread_info *info = arg;
 	struct scsi_cmd *cmd;
 	sigset_t set;
+	struct bs_finish *tbsf;
 
 	sigfillset(&set);
 	sigprocmask(SIG_BLOCK, &set, NULL);
@@ -212,26 +214,25 @@ static void *bs_thread_worker_fn(void *a
 
 		info->request_fn(cmd);
 
-		pthread_mutex_lock(&finished_lock);
-		list_add_tail(&cmd->bs_list, &finished_list);
-		pthread_mutex_unlock(&finished_lock);
+		tbsf = cmd->c_target->bsf;
+		pthread_mutex_lock(&tbsf->finished_lock);
+		list_add_tail(&cmd->bs_list, &tbsf->finished_list);
+		pthread_mutex_unlock(&tbsf->finished_lock);
 
 		if (sig_fd < 0)
 			pthread_cond_signal(&finished_cond);
 		else
-			kill(getpid(), SIGUSR2);
+			syscall(SYS_tgkill, getpid(), tbsf->th_id, SIGUSR2);
 	}
 
 	pthread_exit(NULL);
 }
 
-static int bs_init_signalfd(void)
+static int bs_init_signalfd(struct bs_finish *b)
 {
 	sigset_t mask;
 	int ret;
 
-	pthread_mutex_init(&finished_lock, NULL);
-
 	sigemptyset(&mask);
 	sigaddset(&mask, SIGUSR2);
 	sigprocmask(SIG_BLOCK, &mask, NULL);
@@ -240,7 +241,7 @@ static int bs_init_signalfd(void)
 	if (sig_fd < 0)
 		return 1;
 
-	ret = tgt_event_add(sig_fd, EPOLLIN, bs_sig_request_done, NULL);
+	ret = tgt_event_add(sig_fd, EPOLLIN, bs_sig_request_done, b);
 	if (ret < 0) {
 		close (sig_fd);
 		sig_fd = -1;
@@ -256,7 +257,6 @@ static int bs_init_notify_thread(void)
 	int ret;
 
 	pthread_cond_init(&finished_cond, NULL);
-	pthread_mutex_init(&finished_lock, NULL);
 
 	ret = pipe(command_fd);
 	if (ret) {
@@ -298,7 +298,6 @@ close_command_fd:
 	close(command_fd[1]);
 destroy_cond_mutex:
 	pthread_cond_destroy(&finished_cond);
-	pthread_mutex_destroy(&finished_lock);
 
 	return 1;
 }
@@ -307,7 +306,11 @@ int bs_init(void)
 {
 	int ret;
 
-	ret = bs_init_signalfd();
+	pthread_mutex_init(&bsf->finished_lock, NULL);
+	INIT_LIST_HEAD(&bsf->finished_list);
+	bsf->th_id = syscall(SYS_gettid);
+
+	ret = bs_init_signalfd(bsf);
 	if (!ret) {
 		eprintf("use signalfd notification\n");
 		return 0;
Index: tgt-1.0.8.4/usr/target.h
===================================================================
--- tgt-1.0.8.4.orig/usr/target.h
+++ tgt-1.0.8.4/usr/target.h
@@ -39,6 +39,8 @@ struct target {
 	struct list_head acl_list;
 
 	struct tgt_account account;
+
+	struct bs_finish *bsf;
 };
 
 struct it_nexus {
Index: tgt-1.0.8.4/usr/target.c
===================================================================
--- tgt-1.0.8.4.orig/usr/target.c
+++ tgt-1.0.8.4/usr/target.c
@@ -1778,6 +1778,8 @@ char *tgt_targetname(int tid)
 
 #define DEFAULT_NR_ACCOUNT 16
 
+extern struct bs_finish *bsf;
+
 int tgt_target_create(int lld, int tid, char *args)
 {
 	struct target *target, *pos;
@@ -1841,6 +1843,9 @@ int tgt_target_create(int lld, int tid, 
 
 	target->bst = bst;
 
+	/* We set the global one, but may be replaced by target_create() */
+	target->bsf = bsf;
+
 	target->target_state = SCSI_TARGET_READY;
 	target->lid = lld;
 
--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list