[sheepdog] [PATCH v2 3/5] util/sd_inode: kill {set,get} macros

Liu Yuan namei.unix at gmail.com
Fri Feb 21 07:33:39 CET 2014


users of these functions should not bother with writer and reader and simplify
the parameters of internal functions a lot. Make them as functions also
explicitly namespace them as 'sd_inode' helpers for better maintainability.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/cluster.c            |   4 +-
 dog/dog.c                |   3 +
 dog/dog.h                |  10 ----
 dog/vdi.c                |  43 +++++++--------
 include/internal_proto.h |   3 +-
 include/sheepdog_proto.h |  17 +++---
 lib/sd_inode.c           | 141 +++++++++++++++++++++++++++--------------------
 sheep/http/kv.c          |  23 ++++----
 sheep/http/oalloc.c      |  13 ++---
 sheep/nfs/fs.c           |   7 +--
 sheep/nfs/nfs.c          |   2 +-
 sheep/ops.c              |   7 +--
 sheep/sheep.c            |   4 ++
 sheep/sheep_priv.h       |  10 ----
 sheep/vdi.c              |   4 +-
 sheepfs/core.c           |   3 +
 sheepfs/sheepfs.h        |   5 ++
 sheepfs/volume.c         |  28 ++--------
 18 files changed, 159 insertions(+), 168 deletions(-)

diff --git a/dog/cluster.c b/dog/cluster.c
index 8e1586f..169e837 100644
--- a/dog/cluster.c
+++ b/dog/cluster.c
@@ -316,14 +316,14 @@ static void fill_object_tree(uint32_t vid, const char *name, const char *tag,
 	if (i->store_policy == 0) {
 		nr_objs = count_data_objs(i);
 		for (uint32_t idx = 0; idx < nr_objs; idx++) {
-			vdi_id = INODE_GET_VID(i, idx);
+			vdi_id = sd_inode_get_vid(i, idx);
 			if (!vdi_id)
 				continue;
 			uint64_t oid = vid_to_data_oid(vdi_id, idx);
 			object_tree_insert(oid, i->nr_copies, i->copy_policy);
 		}
 	} else
-		traverse_btree(dog_bnode_reader, i, fill_cb, &i);
+		traverse_btree(i, fill_cb, &i);
 
 	/* fill vmstate object id */
 	nr_vmstate_object = DIV_ROUND_UP(i->vm_state_size, SD_DATA_OBJ_SIZE);
diff --git a/dog/dog.c b/dog/dog.c
index 6e55b2f..fbedd6d 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -494,6 +494,9 @@ int main(int argc, char **argv)
 		}
 	}
 
+	if (sd_inode_actor_init(dog_bnode_writer, dog_bnode_reader) < 0)
+		exit(EXIT_SYSFAIL);
+
 	if (!is_stdout_console() || raw_output)
 		highlight = false;
 
diff --git a/dog/dog.h b/dog/dog.h
index cfc4bec..59d5a1c 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -99,16 +99,6 @@ int dog_bnode_writer(uint64_t oid, void *mem, unsigned int len, uint64_t offset,
 int dog_bnode_reader(uint64_t oid, void **mem, unsigned int len,
 		     uint64_t offset);
 
-#define INODE_GET_VID(inode, idx) (sd_inode_get_vid(dog_bnode_reader, \
-							inode, idx))
-#define INODE_SET_VID(inode, idx, vdi_id) (sd_inode_set_vid(dog_bnode_writer, \
-					   dog_bnode_reader, inode, idx, idx, \
-					   vdi_id))
-#define INODE_SET_VID_RANGE(inode, idx_start, idx_end, vdi_id) \
-				(sd_inode_set_vid(dog_bnode_writer, \
-				dog_bnode_reader, inode, idx_start, \
-				idx_end, vdi_id))
-
 extern struct command vdi_command;
 extern struct command node_command;
 extern struct command cluster_command;
diff --git a/dog/vdi.c b/dog/vdi.c
index 2476296..3cc6239 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -79,7 +79,7 @@ int dog_bnode_reader(uint64_t oid, void **mem, unsigned int len,
 static inline bool is_data_obj_writeable(const struct sd_inode *inode,
 					 uint32_t idx)
 {
-	return inode->vdi_id == INODE_GET_VID(inode, idx);
+	return inode->vdi_id == sd_inode_get_vid(inode, idx);
 }
 
 static void vdi_show_progress(uint64_t done, uint64_t total)
@@ -124,7 +124,7 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 			 "%Y-%m-%d %H:%M", &tm);
 	}
 
-	sd_inode_stat(i, &my_objs, &cow_objs, dog_bnode_reader);
+	sd_inode_stat(i, &my_objs, &cow_objs);
 
 	if (i->snap_id == 1 && i->parent_vdi_id != 0)
 		is_clone = true;
@@ -285,7 +285,7 @@ static int obj_info_filler(const char *sheep, uint64_t oid, struct sd_rsp *rsp,
 		if (info->success)
 			break;
 		info->success = true;
-		vdi_id = INODE_GET_VID(inode, info->idx);
+		vdi_id = sd_inode_get_vid(inode, info->idx);
 		if (vdi_id) {
 			info->data_oid = vid_to_data_oid(vdi_id, info->idx);
 			return 1;
@@ -555,9 +555,8 @@ static int vdi_create(int argc, char **argv)
 			goto out;
 		}
 
-		INODE_SET_VID(inode, idx, vid);
-		ret = sd_inode_write_vid(dog_bnode_writer, inode, idx, vid, vid,
-					 0, false, true);
+		sd_inode_set_vid(inode, idx, vid);
+		ret = sd_inode_write_vid(inode, idx, vid, vid, 0, false, true);
 		if (ret) {
 			ret = EXIT_FAILURE;
 			goto out;
@@ -670,7 +669,7 @@ static int vdi_clone(int argc, char **argv)
 		size_t size;
 
 		vdi_show_progress(idx * SD_DATA_OBJ_SIZE, inode->vdi_size);
-		vdi_id = INODE_GET_VID(inode, idx);
+		vdi_id = sd_inode_get_vid(inode, idx);
 		if (vdi_id) {
 			oid = vid_to_data_oid(vdi_id, idx);
 			ret = dog_read_object(oid, buf, SD_DATA_OBJ_SIZE, 0,
@@ -692,9 +691,9 @@ static int vdi_clone(int argc, char **argv)
 			goto out;
 		}
 
-		INODE_SET_VID(new_inode, idx, new_vid);
-		ret = sd_inode_write_vid(dog_bnode_writer, new_inode, idx,
-					 new_vid, new_vid, 0, false, true);
+		sd_inode_set_vid(new_inode, idx, new_vid);
+		ret = sd_inode_write_vid(new_inode, idx, new_vid, new_vid, 0,
+					 false, true);
 		if (ret) {
 			ret = EXIT_FAILURE;
 			goto out;
@@ -885,13 +884,13 @@ static int vdi_object_map(int argc, char **argv)
 
 	printf("Index       VID\n");
 	if (idx != ~0) {
-		vid = INODE_GET_VID(inode, idx);
+		vid = sd_inode_get_vid(inode, idx);
 		printf("%08"PRIu64" %8"PRIx32"\n", idx, vid);
 	} else {
 		uint64_t max_idx = count_data_objs(inode);
 
 		for (idx = 0; idx < max_idx; idx++) {
-			vid = INODE_GET_VID(inode, idx);
+			vid = sd_inode_get_vid(inode, idx);
 			if (vid)
 				printf("%08"PRIu64" %8"PRIx32"\n", idx, vid);
 		}
@@ -1287,7 +1286,7 @@ static int vdi_read(int argc, char **argv)
 	offset %= SD_DATA_OBJ_SIZE;
 	while (done < total) {
 		len = min(total - done, SD_DATA_OBJ_SIZE - offset);
-		vdi_id = INODE_GET_VID(inode, idx);
+		vdi_id = sd_inode_get_vid(inode, idx);
 		if (vdi_id) {
 			oid = vid_to_data_oid(vdi_id, idx);
 			ret = dog_read_object(oid, buf, len, offset, false);
@@ -1363,7 +1362,7 @@ static int vdi_write(int argc, char **argv)
 		flags = 0;
 		len = min(total - done, SD_DATA_OBJ_SIZE - offset);
 
-		vdi_id = INODE_GET_VID(inode, idx);
+		vdi_id = sd_inode_get_vid(inode, idx);
 		if (!vdi_id)
 			create = true;
 		else if (!is_data_obj_writeable(inode, idx)) {
@@ -1385,7 +1384,7 @@ static int vdi_write(int argc, char **argv)
 			total = done + len;
 		}
 
-		INODE_SET_VID(inode, idx, inode->vdi_id);
+		sd_inode_set_vid(inode, idx, inode->vdi_id);
 		oid = vid_to_data_oid(inode->vdi_id, idx);
 		ret = dog_write_object(oid, old_oid, buf, len, offset, flags,
 				      inode->nr_copies, inode->copy_policy,
@@ -1397,8 +1396,8 @@ static int vdi_write(int argc, char **argv)
 		}
 
 		if (create) {
-			ret = sd_inode_write_vid(dog_bnode_writer, inode, idx,
-						 vid, vid, flags, false, false);
+			ret = sd_inode_write_vid(inode, idx, vid, vid, flags,
+						 false, false);
 			if (ret) {
 				ret = EXIT_FAILURE;
 				goto out;
@@ -1830,7 +1829,7 @@ int do_vdi_check(const struct sd_inode *inode)
 		max_idx = count_data_objs(inode);
 		vdi_show_progress(done, inode->vdi_size);
 		for (uint32_t idx = 0; idx < max_idx; idx++) {
-			vid = INODE_GET_VID(inode, idx);
+			vid = sd_inode_get_vid(inode, idx);
 			if (vid) {
 				oid = vid_to_data_oid(vid, idx);
 				queue_vdi_check_work(inode, oid, &done, wq,
@@ -1842,7 +1841,7 @@ int do_vdi_check(const struct sd_inode *inode)
 		}
 	} else {
 		struct check_arg arg = {inode, &done, wq, nr_copies};
-		traverse_btree(dog_bnode_reader, inode, check_cb, &arg);
+		traverse_btree(inode, check_cb, &arg);
 		vdi_show_progress(inode->vdi_size, inode->vdi_size);
 	}
 
@@ -1992,8 +1991,8 @@ static int vdi_backup(int argc, char **argv)
 	}
 
 	for (idx = 0; idx < nr_objs; idx++) {
-		uint32_t from_vid = INODE_GET_VID(from_inode, idx);
-		uint32_t to_vid = INODE_GET_VID(to_inode, idx);
+		uint32_t from_vid = sd_inode_get_vid(from_inode, idx);
+		uint32_t to_vid = sd_inode_get_vid(to_inode, idx);
 
 		if (to_vid == 0 && from_vid == 0)
 			continue;
@@ -2046,7 +2045,7 @@ static int restore_obj(struct obj_backup *backup, uint32_t vid,
 		       struct sd_inode *parent_inode)
 {
 	int ret;
-	uint32_t parent_vid = INODE_GET_VID(parent_inode, backup->idx);
+	uint32_t parent_vid = sd_inode_get_vid(parent_inode, backup->idx);
 	uint64_t parent_oid = 0;
 
 	if (parent_vid)
diff --git a/include/internal_proto.h b/include/internal_proto.h
index 22ea897..460264c 100644
--- a/include/internal_proto.h
+++ b/include/internal_proto.h
@@ -263,8 +263,7 @@ struct sd_stat {
 	} r;
 };
 
-void sd_inode_stat(const struct sd_inode *inode, uint64_t *, uint64_t *,
-		   read_node_fn reader);
+void sd_inode_stat(const struct sd_inode *inode, uint64_t *, uint64_t *);
 
 #ifdef HAVE_TRACE
 
diff --git a/include/sheepdog_proto.h b/include/sheepdog_proto.h
index 67d10b2..9746fcc 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -272,14 +272,13 @@ struct sheepdog_vdi_attr {
 };
 
 extern void sd_inode_init(void *data, int depth);
-extern uint32_t sd_inode_get_vid(read_node_fn reader,
-				 const struct sd_inode *inode, uint32_t idx);
-extern void sd_inode_set_vid(write_node_fn writer, read_node_fn reader,
-			     struct sd_inode *inode, uint32_t idx_start,
-			     uint32_t idx_end, uint32_t vdi_id);
-extern int sd_inode_write(write_node_fn writer, struct sd_inode *inode,
-			  int flags, bool create, bool direct);
-extern int sd_inode_write_vid(write_node_fn writer, struct sd_inode *inode,
+extern int sd_inode_actor_init(write_node_fn writer, read_node_fn reader);
+extern uint32_t sd_inode_get_vid(const struct sd_inode *inode, uint32_t idx);
+extern int sd_inode_set_vid(struct sd_inode *inode, uint32_t idx, uint32_t);
+extern int sd_inode_set_vid_range(struct sd_inode *inode, uint32_t idx_start,
+				  uint32_t idx_end, uint32_t vdi_id);
+extern int sd_inode_write(struct sd_inode *inode, int flags, bool create, bool);
+extern int sd_inode_write_vid(struct sd_inode *inode,
 			      uint32_t idx, uint32_t vid, uint32_t value,
 			      int flags, bool create, bool direct);
 extern uint32_t sd_inode_get_meta_size(struct sd_inode *inode, size_t size);
@@ -289,7 +288,7 @@ extern void sd_inode_copy_vdis(write_node_fn writer, read_node_fn reader,
 			       struct sd_inode *newi);
 
 typedef void (*btree_cb_fn)(void *data, enum btree_node_type type, void *arg);
-extern void traverse_btree(read_node_fn reader, const struct sd_inode *inode,
+extern void traverse_btree(const struct sd_inode *inode,
 			   btree_cb_fn fn, void *arg);
 
 /* 64 bit FNV-1a non-zero initial basis */
diff --git a/lib/sd_inode.c b/lib/sd_inode.c
index 68f3cf6..4bc5eaa 100644
--- a/lib/sd_inode.c
+++ b/lib/sd_inode.c
@@ -108,6 +108,13 @@ struct find_path {
 	int depth;
 };
 
+struct sd_inode_actor {
+	write_node_fn writer;
+	read_node_fn reader;
+};
+
+static struct sd_inode_actor inode_actor;
+
 static int index_compare(struct sd_index *a, struct sd_index *b)
 {
 	return intcmp(a->idx, b->idx);
@@ -123,8 +130,7 @@ static int indirect_idx_compare(struct sd_indirect_idx *a,
  * traverse the whole btree that include all the inode->data_vdi_id, bnode,
  * data objects and call btree_cb_fn()
  */
-void traverse_btree(read_node_fn reader, const struct sd_inode *inode,
-		    btree_cb_fn fn, void *arg)
+void traverse_btree(const struct sd_inode *inode, btree_cb_fn fn, void *arg)
 {
 	struct sd_index_header *header = INDEX_HEADER(inode->data_vdi_id);
 	struct sd_index_header *leaf_node = NULL;
@@ -148,8 +154,8 @@ void traverse_btree(read_node_fn reader, const struct sd_inode *inode,
 		tmp = (void *)leaf_node;
 
 		while (iter_idx != last_idx) {
-			reader(iter_idx->oid, &tmp,
-			       SD_INODE_DATA_INDEX_SIZE, 0);
+			inode_actor.reader(iter_idx->oid, &tmp,
+					   SD_INODE_DATA_INDEX_SIZE, 0);
 
 			fn(iter_idx, BTREE_INDIRECT_IDX, arg);
 			fn(leaf_node, BTREE_HEAD, arg);
@@ -193,11 +199,11 @@ static void dump_cb(void *data, enum btree_node_type type, void *arg)
 #endif
 
 /* dump the information of B-tree */
-static void dump_btree(read_node_fn reader, struct sd_inode *inode)
+static void dump_btree(struct sd_inode *inode)
 {
 #ifdef DEBUG
 	sd_info("btree> BEGIN");
-	traverse_btree(reader, inode, dump_cb, NULL);
+	traverse_btree(inode, dump_cb, NULL);
 	sd_info("btree> END");
 #endif
 }
@@ -208,8 +214,6 @@ static void dump_btree(read_node_fn reader, struct sd_inode *inode)
  * so it could only be used in sd_inode_set_vid() which will be protected by
  * distributed lock and should be released in the end of sd_inode_set_vid().
  */
-static write_node_fn caller_writer;
-static read_node_fn caller_reader;
 
 /* no rationale */
 #define NUMBER_OF_CACHE	4
@@ -225,23 +229,23 @@ static void icache_init(void)
 	cache_idx = 0;
 }
 
-static void icache_writeout(write_node_fn writer, int copies, int policy)
+static void icache_writeout(int copies, int policy)
 {
 	int i;
 	for (i = 0; i < cache_idx; i++) {
-		writer(cache_array[i].oid, cache_array[i].mem,
-		       SD_INODE_DATA_INDEX_SIZE, 0, 0, copies, policy,
-		       false, false);
+		inode_actor.writer(cache_array[i].oid, cache_array[i].mem,
+				   SD_INODE_DATA_INDEX_SIZE, 0, 0, copies,
+				   policy, false, false);
 	}
 }
 
-static void icache_release(write_node_fn writer, int copies, int policy)
+static void icache_release(int copies, int policy)
 {
-	icache_writeout(writer, copies, policy);
+	icache_writeout(copies, policy);
 	icache_init();
 }
 
-static void icache_insert(write_node_fn writer, int copies, int policy,
+static void icache_insert(int copies, int policy,
 			  uint64_t oid, void *mem)
 {
 	int i;
@@ -255,7 +259,7 @@ static void icache_insert(write_node_fn writer, int copies, int policy,
 
 	if (cache_idx == (NUMBER_OF_CACHE - 1)) {
 		sd_debug("cache for B-tree is full, so write all out");
-		icache_release(writer, copies, policy);
+		icache_release(copies, policy);
 	}
 
 	/* insert new cache */
@@ -280,11 +284,11 @@ static int icache_writer(uint64_t id, void *mem, unsigned int len,
 {
 	/* Only try to cache entire ext-node */
 	if (!offset && !create && !direct && len == SD_INODE_DATA_INDEX_SIZE) {
-		icache_insert(caller_writer, copies, copy_policy, id, mem);
+		icache_insert(copies, copy_policy, id, mem);
 		return SD_RES_SUCCESS;
 	}
-	return caller_writer(id, mem, len, offset, flags, copies, copy_policy,
-			     create, direct);
+	return inode_actor.writer(id, mem, len, offset, flags, copies,
+				  copy_policy, create, direct);
 }
 
 static int icache_reader(uint64_t id, void **mem, unsigned int len,
@@ -299,7 +303,7 @@ static int icache_reader(uint64_t id, void **mem, unsigned int len,
 			return SD_RES_SUCCESS;
 		}
 	}
-	return caller_reader(id, mem, len, offset);
+	return inode_actor.reader(id, mem, len, offset);
 }
 
 void sd_inode_init(void *data, int depth)
@@ -511,8 +515,7 @@ out:
 	return ret;
 }
 
-uint32_t sd_inode_get_vid(read_node_fn reader, const struct sd_inode *inode,
-			  uint32_t idx)
+uint32_t sd_inode_get_vid(const struct sd_inode *inode, uint32_t idx)
 {
 	struct find_path path;
 	int ret;
@@ -525,7 +528,7 @@ uint32_t sd_inode_get_vid(read_node_fn reader, const struct sd_inode *inode,
 			return 0;
 
 		memset(&path, 0, sizeof(path));
-		ret = search_whole_btree(reader, inode, idx, &path);
+		ret = search_whole_btree(inode_actor.reader, inode, idx, &path);
 		if (ret == SD_RES_SUCCESS)
 			return path.p_index->vdi_id;
 		if (path.p_index_header)
@@ -679,17 +682,12 @@ out:
 		free(path.p_index_header);
 }
 
-void sd_inode_set_vid(write_node_fn writer, read_node_fn reader,
-		      struct sd_inode *inode, uint32_t idx_start,
-		      uint32_t idx_end, uint32_t vdi_id)
+int sd_inode_set_vid_range(struct sd_inode *inode, uint32_t idx_start,
+			   uint32_t idx_end, uint32_t vdi_id)
 {
 	struct sd_index_header *header;
 	int idx;
 
-	/* save default writer and reader */
-	caller_writer = writer;
-	caller_reader = reader;
-
 	for (idx = idx_start; idx <= idx_end; idx++) {
 		if (inode->store_policy == 0)
 			inode->data_vdi_id[idx] = vdi_id;
@@ -700,15 +698,26 @@ void sd_inode_set_vid(write_node_fn writer, read_node_fn reader,
 			if (header->magic != INODE_BTREE_MAGIC)
 				panic("%s() B-tree in inode is corrupt!",
 				      __func__);
-			/* use cache version of writer and reader */
+			/*
+			 * use icache(write buffer) to accelerate batch set
+			 * operation. icache will be released after this
+			 * transaction to assure consistency.
+			 */
 			set_vid_for_btree(icache_writer, icache_reader, inode,
 					  idx, vdi_id);
 		}
 	}
 	if (inode->store_policy != 0)
-		dump_btree(reader, inode);
+		dump_btree(inode);
 
-	icache_release(caller_writer, inode->nr_copies, inode->copy_policy);
+	icache_release(inode->nr_copies, inode->copy_policy);
+	/* XXX: return error code */
+	return 0;
+}
+
+int sd_inode_set_vid(struct sd_inode *inode, uint32_t idx, uint32_t vdi_id)
+{
+	return sd_inode_set_vid_range(inode, idx, idx, vdi_id);
 }
 
 /*
@@ -740,53 +749,57 @@ uint32_t sd_inode_get_meta_size(struct sd_inode *inode, size_t size)
 }
 
 /* Write the whole meta-data of inode out */
-int sd_inode_write(write_node_fn writer, struct sd_inode *inode, int flags,
-		   bool create, bool direct)
+int sd_inode_write(struct sd_inode *inode, int flags, bool create, bool direct)
 {
 	uint32_t len;
 	int ret;
 
 	if (inode->store_policy == 0)
-		ret = writer(vid_to_vdi_oid(inode->vdi_id), inode,
-			     SD_INODE_HEADER_SIZE, 0,
-			     flags, inode->nr_copies, inode->copy_policy,
-			     create, direct);
+		ret = inode_actor.writer(vid_to_vdi_oid(inode->vdi_id), inode,
+					 SD_INODE_HEADER_SIZE, 0,
+					 flags, inode->nr_copies,
+					 inode->copy_policy,
+					 create, direct);
 	else {
 		len = SD_INODE_HEADER_SIZE + sd_inode_get_meta_size(inode, 0);
-		ret = writer(vid_to_vdi_oid(inode->vdi_id), inode, len, 0,
-			     flags, inode->nr_copies, inode->copy_policy,
-			     create, false);
+		ret = inode_actor.writer(vid_to_vdi_oid(inode->vdi_id), inode,
+					 len, 0, flags, inode->nr_copies,
+					 inode->copy_policy, create, false);
 		if (ret != SD_RES_SUCCESS)
 			goto out;
-		ret = writer(vid_to_vdi_oid(inode->vdi_id),
-			     &(inode->btree_counter),
-			     sizeof(uint32_t),
-			     offsetof(struct sd_inode, btree_counter), flags,
-			     inode->nr_copies, inode->copy_policy,
-			     create, false);
+		ret = inode_actor.writer(vid_to_vdi_oid(inode->vdi_id),
+					 &(inode->btree_counter),
+					 sizeof(uint32_t),
+					 offsetof(struct sd_inode,
+						  btree_counter),
+					 flags,
+					 inode->nr_copies, inode->copy_policy,
+					 create, false);
 	}
 out:
 	return ret;
 }
 
 /* Write the meta-data of inode out */
-int sd_inode_write_vid(write_node_fn writer, struct sd_inode *inode,
+int sd_inode_write_vid(struct sd_inode *inode,
 		       uint32_t idx, uint32_t vid, uint32_t value,
 		       int flags, bool create, bool direct)
 {
 	int ret = SD_RES_SUCCESS;
 
 	if (inode->store_policy == 0)
-		ret = writer(vid_to_vdi_oid(vid), &value, sizeof(value),
-			     SD_INODE_HEADER_SIZE + sizeof(value) * idx,
-			     flags, inode->nr_copies, inode->copy_policy,
-			     create, direct);
+		ret = inode_actor.writer(vid_to_vdi_oid(vid), &value,
+					 sizeof(value),
+				SD_INODE_HEADER_SIZE + sizeof(value) * idx,
+					 flags, inode->nr_copies,
+					 inode->copy_policy,
+					 create, direct);
 	else {
 		/*
 		 * For btree type sd_inode, we only have to write all
 		 * meta-data of sd_inode out.
 		 */
-		ret = sd_inode_write(writer, inode, flags, create, direct);
+		ret = sd_inode_write(inode, flags, create, direct);
 	}
 	return ret;
 }
@@ -849,11 +862,10 @@ static void stat_cb(void *data, enum btree_node_type type, void *arg)
 }
 
 static void hypver_volume_stat(const struct sd_inode *inode,
-			       uint64_t *my_objs, uint64_t *cow_objs,
-			       read_node_fn reader)
+			       uint64_t *my_objs, uint64_t *cow_objs)
 {
 	struct stat_arg arg = {my_objs, cow_objs, inode->vdi_id};
-	traverse_btree(reader, inode, stat_cb, &arg);
+	traverse_btree(inode, stat_cb, &arg);
 }
 
 static void volume_stat(const struct sd_inode *inode, uint64_t *my_objs,
@@ -913,10 +925,21 @@ static void volume_stat(const struct sd_inode *inode, uint64_t *my_objs,
  * means the number of the other objects.
  */
 void sd_inode_stat(const struct sd_inode *inode, uint64_t *my_objs,
-		   uint64_t *cow_objs, read_node_fn reader)
+		   uint64_t *cow_objs)
 {
 	if (inode->store_policy == 0)
 		volume_stat(inode, my_objs, cow_objs);
 	else
-		hypver_volume_stat(inode, my_objs, cow_objs, reader);
+		hypver_volume_stat(inode, my_objs, cow_objs);
+}
+
+int sd_inode_actor_init(write_node_fn writer, read_node_fn reader)
+{
+	if (!writer || !reader) {
+		sd_err("failed to init sd inode actor");
+		return -1;
+	}
+	inode_actor.writer = writer;
+	inode_actor.reader = reader;
+	return 0;
 }
diff --git a/sheep/http/kv.c b/sheep/http/kv.c
index 2c6bce6..fa7d13a 100644
--- a/sheep/http/kv.c
+++ b/sheep/http/kv.c
@@ -141,7 +141,7 @@ static int read_account_meta(const char *account, uint64_t *bucket_count,
 		goto out;
 	}
 
-	traverse_btree(sheep_bnode_reader, inode, bucket_iterater, &arg);
+	traverse_btree(inode, bucket_iterater, &arg);
 	*object_count = arg.object_count;
 	*bucket_count = arg.bucket_count;
 	*used = arg.bytes_used;
@@ -234,9 +234,8 @@ static int bnode_do_create(struct kv_bnode *bnode, struct sd_inode *inode,
 		sd_err("failed to create object, %" PRIx64, oid);
 		goto out;
 	}
-	INODE_SET_VID(inode, idx, vid);
-	ret = sd_inode_write_vid(sheep_bnode_writer, inode, idx,
-				 vid, vid, 0, false, false);
+	sd_inode_set_vid(inode, idx, vid);
+	ret = sd_inode_write_vid(inode, idx, vid, vid, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update inode, %" PRIx64,
 		       vid_to_vdi_oid(vid));
@@ -264,7 +263,7 @@ static int bnode_create(struct kv_bnode *bnode, uint32_t account_vid)
 	hval = sd_hash(bnode->name, strlen(bnode->name));
 	for (i = 0; i < MAX_DATA_OBJS; i++) {
 		idx = (hval + i) % MAX_DATA_OBJS;
-		tmp_vid = INODE_GET_VID(inode, idx);
+		tmp_vid = sd_inode_get_vid(inode, idx);
 		if (tmp_vid)
 			continue;
 		else
@@ -477,7 +476,7 @@ static int bucket_iterate_object(uint32_t bucket_vid, object_iter_cb cb,
 		goto out;
 	}
 
-	traverse_btree(sheep_bnode_reader, inode, object_iterater, &arg);
+	traverse_btree(inode, object_iterater, &arg);
 out:
 	free(inode);
 	return ret;
@@ -591,8 +590,7 @@ int kv_iterate_bucket(const char *account, bucket_iter_cb cb, void *opaque)
 		goto out;
 	}
 
-	traverse_btree(sheep_bnode_reader, &account_inode,
-		       bucket_iterater, &arg);
+	traverse_btree(&account_inode, bucket_iterater, &arg);
 out:
 	sys->cdrv->unlock(account_vid);
 	return ret;
@@ -767,9 +765,8 @@ static int onode_do_create(struct kv_onode *onode, struct sd_inode *inode,
 	if (!create)
 		goto out;
 
-	INODE_SET_VID(inode, idx, vid);
-	ret = sd_inode_write_vid(sheep_bnode_writer, inode, idx,
-				 vid, vid, 0, false, false);
+	sd_inode_set_vid(inode, idx, vid);
+	ret = sd_inode_write_vid(inode, idx, vid, vid, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update inode, %" PRIx64,
 		       vid_to_vdi_oid(vid));
@@ -799,7 +796,7 @@ static int onode_create(struct kv_onode *onode, uint32_t bucket_vid)
 	hval = sd_hash(onode->name, strlen(onode->name));
 	for (i = 0; i < MAX_DATA_OBJS; i++) {
 		idx = (hval + i) % MAX_DATA_OBJS;
-		tmp_vid = INODE_GET_VID(inode, idx);
+		tmp_vid = sd_inode_get_vid(inode, idx);
 		if (tmp_vid) {
 			uint64_t oid = vid_to_data_oid(bucket_vid, idx);
 			char name[SD_MAX_OBJECT_NAME] = { };
@@ -920,7 +917,7 @@ static int onode_lookup(struct kv_onode *onode, uint32_t ovid, const char *name)
 	hval = sd_hash(name, strlen(name));
 	for (i = 0; i < MAX_DATA_OBJS; i++) {
 		idx = (hval + i) % MAX_DATA_OBJS;
-		tmp_vid = INODE_GET_VID(inode, idx);
+		tmp_vid = sd_inode_get_vid(inode, idx);
 		if (tmp_vid) {
 			uint64_t oid = vid_to_data_oid(ovid, idx);
 
diff --git a/sheep/http/oalloc.c b/sheep/http/oalloc.c
index cb68497..ed4da40 100644
--- a/sheep/http/oalloc.c
+++ b/sheep/http/oalloc.c
@@ -90,9 +90,8 @@ int oalloc_init(uint32_t vid)
 		       sd_strerror(ret));
 		goto out;
 	}
-	INODE_SET_VID(inode, 0, vid);
-	ret = sd_inode_write_vid(sheep_bnode_writer, inode, 0,
-				 vid, vid, 0, false, false);
+	sd_inode_set_vid(inode, 0, vid);
+	ret = sd_inode_write_vid(inode, 0, vid, vid, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update inode, %" PRIx32", %s", vid,
 		       sd_strerror(ret));
@@ -179,9 +178,9 @@ int oalloc_new_finish(uint32_t vid, uint64_t start, uint64_t count)
 	}
 
 	sd_debug("start %"PRIu64" end %"PRIu64, start, start + count - 1);
-	INODE_SET_VID_RANGE(inode, start, (start + count - 1), vid);
+	sd_inode_set_vid_range(inode, start, (start + count - 1), vid);
 
-	ret = sd_inode_write(sheep_bnode_writer, inode, 0, false, false);
+	ret = sd_inode_write(inode, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update inode, %" PRIx64", %s",
 		       vid_to_vdi_oid(vid), sd_strerror(ret));
@@ -278,9 +277,9 @@ int oalloc_free(uint32_t vid, uint64_t start, uint64_t count)
 
 	sd_debug("discard start %"PRIu64" end %"PRIu64, start,
 		 start + count - 1);
-	INODE_SET_VID_RANGE(inode, start, (start + count - 1), 0);
+	sd_inode_set_vid_range(inode, start, (start + count - 1), 0);
 
-	ret = sd_inode_write(sheep_bnode_writer, inode, 0, false, false);
+	ret = sd_inode_write(inode, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update inode, %" PRIx64", %s",
 		       vid_to_vdi_oid(vid), sd_strerror(ret));
diff --git a/sheep/nfs/fs.c b/sheep/nfs/fs.c
index cb1624d..ec92f12 100644
--- a/sheep/nfs/fs.c
+++ b/sheep/nfs/fs.c
@@ -63,9 +63,8 @@ static int inode_do_create(struct inode_data *id)
 	if (!create)
 		goto out;
 
-	INODE_SET_VID(sd_inode, idx, vid);
-	ret = sd_inode_write_vid(sheep_bnode_writer, sd_inode, idx,
-				 vid, vid, 0, false, false);
+	sd_inode_set_vid(sd_inode, idx, vid);
+	ret = sd_inode_write_vid(sd_inode, idx, vid, vid, 0, false, false);
 	if (ret != SD_RES_SUCCESS) {
 		sd_err("failed to update sd inode, %" PRIx64,
 		       vid_to_vdi_oid(vid));
@@ -95,7 +94,7 @@ static int inode_lookup(struct inode_data *idata)
 	hval = sd_hash(name, strlen(name));
 	for (i = 0; i < MAX_DATA_OBJS; i++) {
 		idx = (hval + i) % MAX_DATA_OBJS;
-		tmp_vid = INODE_GET_VID(sd_inode, idx);
+		tmp_vid = sd_inode_get_vid(sd_inode, idx);
 		if (tmp_vid) {
 			uint64_t oid = vid_to_data_oid(vid, idx);
 			uint64_t block;
diff --git a/sheep/nfs/nfs.c b/sheep/nfs/nfs.c
index afc0cf8..036f995 100644
--- a/sheep/nfs/nfs.c
+++ b/sheep/nfs/nfs.c
@@ -639,7 +639,7 @@ void *nfs3_fsstat(struct svc_req *req, struct nfs_arg *argp)
 		goto out;
 	}
 
-	sd_inode_stat(sd_inode, &my, &cow, sheep_bnode_reader);
+	sd_inode_stat(sd_inode, &my, &cow);
 
 	sd_debug("%"PRIx32" contains %lu objects", vid, my);
 
diff --git a/sheep/ops.c b/sheep/ops.c
index 6451563..cc70c8c 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -849,12 +849,11 @@ static int local_discard_obj(struct request *req)
 	if (ret != SD_RES_SUCCESS)
 		goto out;
 
-	tmp_vid = INODE_GET_VID(inode, idx);
+	tmp_vid = sd_inode_get_vid(inode, idx);
 	/* if vid in idx is not exist, we don't need to remove it */
 	if (tmp_vid) {
-		INODE_SET_VID(inode, idx, 0);
-		ret = sd_inode_write_vid(sheep_bnode_writer, inode, idx, vid,
-					 0, 0, false, false);
+		sd_inode_set_vid(inode, idx, 0);
+		ret = sd_inode_write_vid(inode, idx, vid, 0, 0, false, false);
 		if (ret != SD_RES_SUCCESS)
 			goto out;
 		if (sd_remove_object(oid) != SD_RES_SUCCESS)
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 7ec3658..74d1aaf 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -766,6 +766,10 @@ int main(int argc, char **argv)
 		dirp = strtok(argv[optind], ",");
 	}
 
+	ret = sd_inode_actor_init(sheep_bnode_writer, sheep_bnode_reader);
+	if (ret)
+		exit(1);
+
 	ret = init_base_path(dirp);
 	if (ret)
 		exit(1);
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index b7cb937..b498c45 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -399,16 +399,6 @@ int sheep_bnode_writer(uint64_t oid, void *mem, unsigned int len,
 int sheep_bnode_reader(uint64_t oid, void **mem, unsigned int len,
 		       uint64_t offset);
 
-#define INODE_GET_VID(inode, idx) (sd_inode_get_vid(sheep_bnode_reader, \
-					inode, idx))
-#define INODE_SET_VID(inode, idx, vdi_id) (sd_inode_set_vid(sheep_bnode_writer,\
-					   sheep_bnode_reader, inode, idx, idx,\
-					   vdi_id))
-#define INODE_SET_VID_RANGE(inode, idx_start, idx_end, vdi_id) \
-				(sd_inode_set_vid(sheep_bnode_writer,\
-				sheep_bnode_reader, inode, idx_start, \
-				idx_end, vdi_id))
-
 /* Operations */
 
 const struct sd_op_template *get_sd_op(uint8_t opcode);
diff --git a/sheep/vdi.c b/sheep/vdi.c
index de3959b..ac1fa90 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -878,7 +878,7 @@ static int delete_one_vdi(uint32_t vdi_id)
 		nr_objs = count_data_objs(inode);
 		for (nr_deleted = 0, i = 0; i < nr_objs; i++) {
 			uint64_t oid;
-			uint32_t vid = INODE_GET_VID(inode, i);
+			uint32_t vid = sd_inode_get_vid(inode, i);
 
 			if (!vid)
 				continue;
@@ -900,7 +900,7 @@ static int delete_one_vdi(uint32_t vdi_id)
 		}
 	} else {
 		struct delete_arg arg = {inode, &nr_deleted};
-		traverse_btree(sheep_bnode_reader, inode, delete_cb, &arg);
+		traverse_btree(inode, delete_cb, &arg);
 	}
 
 	if (vdi_is_deleted(inode))
diff --git a/sheepfs/core.c b/sheepfs/core.c
index a0ea936..03172fd 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -388,6 +388,9 @@ int main(int argc, char **argv)
 		}
 	}
 
+	if (sd_inode_actor_init(sheepfs_bnode_writer, sheepfs_bnode_reader) < 0)
+		exit(1);
+
 	t = sheepfs_run_cmd("dog");
 	if (!strlen(t->buf)) {
 		fprintf(stderr, "command dog not found\n");
diff --git a/sheepfs/sheepfs.h b/sheepfs/sheepfs.h
index e5bf40e..495b2e5 100644
--- a/sheepfs/sheepfs.h
+++ b/sheepfs/sheepfs.h
@@ -66,6 +66,11 @@ int volume_remove_entry(const char *entry);
 int volume_sync(const char *path);
 int volume_open(const char *path, struct fuse_file_info *);
 int reset_socket_pool(void);
+int sheepfs_bnode_writer(uint64_t oid, void *mem, unsigned int len,
+			 uint64_t offset, uint32_t flags, int copies,
+			 int copy_policy, bool create, bool direct);
+int sheepfs_bnode_reader(uint64_t oid, void **mem, unsigned int len,
+			 uint64_t offset);
 
 /* cluster.c */
 int cluster_info_read(const char *path, char *buf, size_t size, off_t);
diff --git a/sheepfs/volume.c b/sheepfs/volume.c
index 26decf8..f93525a 100644
--- a/sheepfs/volume.c
+++ b/sheepfs/volume.c
@@ -64,28 +64,10 @@ struct vdi_inode {
 static struct rb_root vdi_inode_tree = RB_ROOT;
 static struct sd_rw_lock vdi_inode_tree_lock = SD_RW_LOCK_INITIALIZER;
 
-
-static int sheepfs_bnode_writer(uint64_t oid, void *mem, unsigned int len,
-				uint64_t offset, uint32_t flags, int copies,
-				int copy_policy, bool create, bool direct);
-static int sheepfs_bnode_reader(uint64_t oid, void **mem, unsigned int len,
-				uint64_t offset);
-
-#define INODE_GET_VID(inode, idx) (sd_inode_get_vid(\
-					sheepfs_bnode_reader, inode, idx))
-#define INODE_SET_VID(inode, idx, vdi_id) (sd_inode_set_vid( \
-					sheepfs_bnode_writer, \
-					sheepfs_bnode_reader, \
-					inode, idx, idx, vdi_id))
-#define INODE_SET_VID_RANGE(inode, idx_start, idx_end, vdi_id) \
-				(sd_inode_set_vid(sheepfs_bnode_writer, \
-				sheepfs_bnode_reader, inode, idx_start, \
-				idx_end, vdi_id))
-
 static inline bool is_data_obj_writeable(const struct sd_inode *inode,
 					 uint32_t idx)
 {
-	return inode->vdi_id == INODE_GET_VID(inode, idx);
+	return inode->vdi_id == sd_inode_get_vid(inode, idx);
 }
 
 static int vdi_inode_cmp(const struct vdi_inode *a, const struct vdi_inode *b)
@@ -153,7 +135,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size,
 	if (is_data_obj(oid)) {
 		idx = data_oid_to_idx(oid);
 		assert(vdi);
-		vdi_id = INODE_GET_VID(vdi->inode, idx);
+		vdi_id = sd_inode_get_vid(vdi->inode, idx);
 		if (!vdi_id) {
 			/* if object doesn't exist, we'er done */
 			if (rw == VOLUME_READ) {
@@ -200,10 +182,10 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size,
 	}
 
 	if (create) {
-		INODE_SET_VID(vdi->inode, idx, vid);
+		sd_inode_set_vid(vdi->inode, idx, vid);
 		/* writeback inode update */
-		if (sd_inode_write_vid(sheepfs_bnode_writer, vdi->inode, idx,
-					vid, vid, 0, false, false) < 0)
+		if (sd_inode_write_vid(vdi->inode, idx, vid, vid, 0, false,
+				       false) < 0)
 			return -1;
 	}
 done:
-- 
1.8.1.2




More information about the sheepdog mailing list