[sheepdog] [PATCH 4/5] sheep: init work queues before init_store()

Liu Yuan namei.unix at gmail.com
Tue Dec 4 13:20:30 CET 2012


From: Liu Yuan <tailai.ly at taobao.com>

This fixes the NULL reference problem found by 049

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/sheep.c      |   80 ++++++++++++++++++++++++++++++++--------------------
 sheep/sheep_priv.h |    3 +-
 sheep/store.c      |    6 +---
 3 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/sheep/sheep.c b/sheep/sheep.c
index 2ba9008..74acaa4 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -330,6 +330,25 @@ static void init_journal_arg(char *arg)
 	}
 }
 
+static int init_work_queues(void)
+{
+	sys->gateway_wqueue = init_work_queue("gway", false);
+	sys->io_wqueue = init_work_queue("io", false);
+	sys->recovery_wqueue = init_work_queue("rw", false);
+	sys->deletion_wqueue = init_work_queue("deletion", true);
+	sys->block_wqueue = init_work_queue("block", true);
+	sys->sockfd_wqueue = init_work_queue("sockfd", true);
+	if (is_object_cache_enabled()) {
+		sys->reclaim_wqueue = init_work_queue("reclaim", true);
+		if (!sys->reclaim_wqueue)
+			return -1;
+	}
+	if (!sys->gateway_wqueue || !sys->io_wqueue || !sys->recovery_wqueue ||
+	    !sys->deletion_wqueue || !sys->block_wqueue || !sys->sockfd_wqueue)
+			return -1;
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex;
@@ -510,34 +529,30 @@ int main(int argc, char **argv)
 	if (ret)
 		exit(1);
 
-	ret = init_global_pathnames(dir);
+	ret = init_event(EPOLL_SIZE);
 	if (ret)
 		exit(1);
 
-	/* We should init journal file before backend init */
-	if (uatomic_is_true(&sys->use_journal)) {
-		if (!strlen(jpath))
-			/* internal journal */
-			memcpy(jpath, dir, strlen(dir));
-		dprintf("%s, %zu, %d\n", jpath, jsize, jskip);
-		ret = journal_file_init(jpath, jsize, jskip);
-		if (ret)
-			exit(1);
-	}
+	ret = init_global_pathnames(dir);
+	if (ret)
+		exit(1);
 
-	ret = init_store(dir);
+	ret = create_listen_port(bindaddr, port);
 	if (ret)
 		exit(1);
 
-	ret = init_event(EPOLL_SIZE);
+	ret = init_unix_domain_socket(dir);
 	if (ret)
 		exit(1);
 
-	ret = create_listen_port(bindaddr, port);
+	local_req_init();
+
+	ret = init_signal();
 	if (ret)
 		exit(1);
 
-	ret = init_unix_domain_socket(dir);
+	/* This function must be called before create_cluster() */
+	ret = init_disk_space(dir);
 	if (ret)
 		exit(1);
 
@@ -547,25 +562,30 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	local_req_init();
+	/* We should init journal file before backend init */
+	if (uatomic_is_true(&sys->use_journal)) {
+		if (!strlen(jpath))
+			/* internal journal */
+			memcpy(jpath, dir, strlen(dir));
+		dprintf("%s, %zu, %d\n", jpath, jsize, jskip);
+		ret = journal_file_init(jpath, jsize, jskip);
+		if (ret)
+			exit(1);
+	}
 
-	ret = init_signal();
+	/*
+	 * After this function, we are multi-threaded.
+	 *
+	 * Put those init functions that need single threaded environment, for
+	 * e.g, signal handling, above this call and those need multi-threaded
+	 * environment, for e.g, work queues below.
+	 */
+	ret = init_work_queues();
 	if (ret)
 		exit(1);
 
-	sys->gateway_wqueue = init_work_queue("gway", false);
-	sys->io_wqueue = init_work_queue("io", false);
-	sys->recovery_wqueue = init_work_queue("rw", false);
-	sys->deletion_wqueue = init_work_queue("deletion", true);
-	sys->block_wqueue = init_work_queue("block", true);
-	sys->sockfd_wqueue = init_work_queue("sockfd", true);
-	if (is_object_cache_enabled()) {
-		sys->reclaim_wqueue = init_work_queue("reclaim", true);
-		if (!sys->reclaim_wqueue)
-			exit(1);
-	}
-	if (!sys->gateway_wqueue || !sys->io_wqueue || !sys->recovery_wqueue ||
-	    !sys->deletion_wqueue || !sys->block_wqueue || !sys->sockfd_wqueue)
+	ret = init_store(dir);
+	if (ret)
 		exit(1);
 
 	ret = trace_init();
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index bcdf427..1cfbab7 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -223,7 +223,8 @@ int init_unix_domain_socket(const char *dir);
 int init_store(const char *dir);
 int init_global_pathnames(const char *d);
 int init_base_path(const char *dir);
-int init_obj_path(const char *base_path);
+int init_obj_path(const char *d);
+int init_disk_space(const char *d);
 
 int fill_vdi_copy_list(void *data);
 int get_vdi_copy_number(uint32_t vid);
diff --git a/sheep/store.c b/sheep/store.c
index 3c4f1d5..e321e1d 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -337,7 +337,7 @@ static int init_store_driver(void)
 	return sd_store->init(obj_path);
 }
 
-static int init_disk_space(const char *base_path)
+int init_disk_space(const char *base_path)
 {
 	int ret = SD_RES_SUCCESS;
 	uint64_t space_size = 0;
@@ -399,10 +399,6 @@ int init_store(const char *d)
 {
 	int ret;
 
-	ret = init_disk_space(d);
-	if (ret)
-		return ret;
-
 	if (!sys->gateway_only) {
 		ret = init_store_driver();
 		if (ret)
-- 
1.7.9.5




More information about the sheepdog mailing list