[sheepdog] [PATCH v5 1/8] change accessing of inode->data_vdi_id[] to function

Robin Dong robin.k.dong at gmail.com
Wed Nov 6 09:26:04 CET 2013


We need to modify all the direct accessing of data_vdi_id[] to common function
because the following change of index in inode.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 dog/cluster.c            |    7 ++++---
 dog/vdi.c                |   41 +++++++++++++++++++++++------------------
 include/sheepdog_proto.h |    6 +++++-
 lib/Makefile.am          |    2 +-
 lib/sd_inode.c           |   13 +++++++++++++
 sheep/vdi.c              |    7 ++++---
 sheepfs/volume.c         |   15 ++++++---------
 7 files changed, 56 insertions(+), 35 deletions(-)
 create mode 100644 lib/sd_inode.c

diff --git a/dog/cluster.c b/dog/cluster.c
index c2f97ad..2e77407 100644
--- a/dog/cluster.c
+++ b/dog/cluster.c
@@ -246,6 +246,7 @@ static void fill_object_tree(uint32_t vid, const char *name, const char *tag,
 			     const struct sd_inode *i, void *data)
 {
 	uint64_t vdi_oid = vid_to_vdi_oid(vid), vmstate_oid;
+	uint32_t vdi_id;
 	int nr_objs, nr_vmstate_object;
 
 	/* ignore active vdi */
@@ -258,9 +259,9 @@ static void fill_object_tree(uint32_t vid, const char *name, const char *tag,
 	/* fill data object id */
 	nr_objs = count_data_objs(i);
 	for (uint64_t idx = 0; idx < nr_objs; idx++) {
-		if (i->data_vdi_id[idx]) {
-			uint64_t oid = vid_to_data_oid(i->data_vdi_id[idx],
-						       idx);
+		vdi_id = sd_inode_get_vid(i, idx);
+		if (vdi_id) {
+			uint64_t oid = vid_to_data_oid(vdi_id, idx);
 			object_tree_insert(oid, i->nr_copies, i->copy_policy);
 		}
 	}
diff --git a/dog/vdi.c b/dog/vdi.c
index d9a9a0f..efbf9f4 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -293,14 +293,16 @@ static int obj_info_filler(const char *sheep, uint64_t oid, struct sd_rsp *rsp,
 {
 	struct obj_info_filler_info *info = data;
 	struct sd_inode *inode = (struct sd_inode *)buf;
+	uint32_t vdi_id;
 
 	switch (rsp->result) {
 	case SD_RES_SUCCESS:
 		if (info->success)
 			break;
 		info->success = true;
-		if (inode->data_vdi_id[info->idx]) {
-			info->data_oid = vid_to_data_oid(inode->data_vdi_id[info->idx], 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;
 		}
 		break;
@@ -561,7 +563,7 @@ static int vdi_create(int argc, char **argv)
 			goto out;
 		}
 
-		inode->data_vdi_id[idx] = vid;
+		sd_inode_set_vid(inode, idx, vid);
 		ret = sd_write_object(vid_to_vdi_oid(vid), 0, &vid, sizeof(vid),
 				      SD_INODE_HEADER_SIZE + sizeof(vid) * idx,
 				      0, inode->nr_copies, inode->copy_policy,
@@ -628,7 +630,7 @@ static int vdi_snapshot(int argc, char **argv)
 static int vdi_clone(int argc, char **argv)
 {
 	const char *src_vdi = argv[optind++], *dst_vdi;
-	uint32_t base_vid, new_vid;
+	uint32_t base_vid, new_vid, vdi_id;
 	uint64_t oid;
 	int idx, max_idx, ret;
 	struct sd_inode *inode = NULL;
@@ -668,8 +670,9 @@ static int vdi_clone(int argc, char **argv)
 		size_t size;
 
 		vdi_show_progress(idx * SD_DATA_OBJ_SIZE, inode->vdi_size);
-		if (inode->data_vdi_id[idx]) {
-			oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+		vdi_id = sd_inode_get_vid(inode, idx);
+		if (vdi_id) {
+			oid = vid_to_data_oid(vdi_id, idx);
 			ret = sd_read_object(oid, buf, SD_DATA_OBJ_SIZE, 0, true);
 			if (ret) {
 				ret = EXIT_FAILURE;
@@ -1192,6 +1195,7 @@ static int vdi_read(int argc, char **argv)
 	int ret, idx;
 	struct sd_inode *inode = NULL;
 	uint64_t offset = 0, oid, done = 0, total = (uint64_t) -1;
+	uint32_t vdi_id;
 	unsigned int len;
 	char *buf = NULL;
 
@@ -1226,9 +1230,9 @@ 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);
-
-		if (inode->data_vdi_id[idx]) {
-			oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+		vdi_id = sd_inode_get_vid(inode, idx);
+		if (vdi_id) {
+			oid = vid_to_data_oid(vdi_id, idx);
 			ret = sd_read_object(oid, buf, len, offset, false);
 			if (ret != SD_RES_SUCCESS) {
 				sd_err("Failed to read VDI");
@@ -1261,7 +1265,7 @@ out:
 static int vdi_write(int argc, char **argv)
 {
 	const char *vdiname = argv[optind++];
-	uint32_t vid, flags;
+	uint32_t vid, flags, vdi_id;
 	int ret, idx;
 	struct sd_inode *inode = NULL;
 	uint64_t offset = 0, oid, old_oid, done = 0, total = (uint64_t) -1;
@@ -1302,11 +1306,12 @@ static int vdi_write(int argc, char **argv)
 		flags = 0;
 		len = min(total - done, SD_DATA_OBJ_SIZE - offset);
 
-		if (!inode->data_vdi_id[idx])
+		vdi_id = sd_inode_get_vid(inode, idx);
+		if (!vdi_id)
 			create = true;
 		else if (!is_data_obj_writeable(inode, idx)) {
 			create = true;
-			old_oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+			old_oid = vid_to_data_oid(vdi_id, idx);
 		}
 
 		if (vdi_cmd_data.writeback)
@@ -1323,8 +1328,8 @@ static int vdi_write(int argc, char **argv)
 			total = done + len;
 		}
 
-		inode->data_vdi_id[idx] = inode->vdi_id;
-		oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+		sd_inode_set_vid(inode, idx, inode->vdi_id);
+		oid = vid_to_data_oid(inode->vdi_id, idx);
 		ret = sd_write_object(oid, old_oid, buf, len, offset, flags,
 				      inode->nr_copies, inode->copy_policy,
 				      create, false);
@@ -1682,7 +1687,7 @@ int do_vdi_check(const struct sd_inode *inode)
 	max_idx = count_data_objs(inode);
 	vdi_show_progress(done, inode->vdi_size);
 	for (int idx = 0; idx < max_idx; idx++) {
-		vid = inode->data_vdi_id[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);
@@ -1837,8 +1842,8 @@ static int vdi_backup(int argc, char **argv)
 	}
 
 	for (idx = 0; idx < nr_objs; idx++) {
-		uint32_t from_vid = from_inode->data_vdi_id[idx];
-		uint32_t to_vid = to_inode->data_vdi_id[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;
@@ -1891,7 +1896,7 @@ static int restore_obj(struct obj_backup *backup, uint32_t vid,
 		       struct sd_inode *parent_inode)
 {
 	int ret;
-	uint32_t parent_vid = parent_inode->data_vdi_id[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/sheepdog_proto.h b/include/sheepdog_proto.h
index 7138608..36d5701 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -235,6 +235,10 @@ struct sheepdog_vdi_attr {
 	char value[SD_MAX_VDI_ATTR_VALUE_LEN];
 };
 
+uint32_t sd_inode_get_vid(const struct sd_inode *inode, int idx);
+void sd_inode_set_vid(struct sd_inode *inode, int idx, uint32_t vdi_id);
+void sd_inode_copy_vids(struct sd_inode *oldi, struct sd_inode *newi);
+
 /* 64 bit FNV-1a non-zero initial basis */
 #define FNV1A_64_INIT ((uint64_t) 0xcbf29ce484222325ULL)
 #define FNV_64_PRIME ((uint64_t) 0x100000001b3ULL)
@@ -325,7 +329,7 @@ static inline uint64_t hash_64(uint64_t val, unsigned int bits)
 static inline bool is_data_obj_writeable(const struct sd_inode *inode,
 					 int idx)
 {
-	return inode->vdi_id == inode->data_vdi_id[idx];
+	return inode->vdi_id == sd_inode_get_vid(inode, idx);
 }
 
 static inline bool is_vdi_obj(uint64_t oid)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index b6ac290..a681167 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -5,7 +5,7 @@ AM_CPPFLAGS                = -I$(top_builddir)/include -I$(top_srcdir)/include
 noinst_LIBRARIES	= libsheepdog.a
 
 libsheepdog_a_SOURCES	= event.c logger.c net.c util.c rbtree.c strbuf.c \
-			  sha1.c option.c work.c sockfd_cache.c fec.c
+			  sha1.c option.c work.c sockfd_cache.c fec.c sd_inode.c
 
 if BUILD_SHA1_HW
 libsheepdog_a_SOURCES	+= sha1_ssse3.S
diff --git a/lib/sd_inode.c b/lib/sd_inode.c
new file mode 100644
index 0000000..9d1926f
--- /dev/null
+++ b/lib/sd_inode.c
@@ -0,0 +1,13 @@
+#include <string.h>
+
+#include "sheepdog_proto.h"
+
+uint32_t sd_inode_get_vid(const struct sd_inode *inode, int idx)
+{
+	return inode->data_vdi_id[idx];
+}
+
+void sd_inode_set_vid(struct sd_inode *inode, int idx, uint32_t vdi_id)
+{
+	inode->data_vdi_id[idx] = vdi_id;
+}
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 333a5f7..f493dee 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -847,13 +847,14 @@ static void delete_one(struct work *work)
 	nr_objs = count_data_objs(inode);
 	for (nr_deleted = 0, i = 0; i < nr_objs; i++) {
 		uint64_t oid;
+		uint32_t vid = sd_inode_get_vid(inode, i);
 
-		if (!inode->data_vdi_id[i])
+		if (!vid)
 			continue;
 
-		oid = vid_to_data_oid(inode->data_vdi_id[i], i);
+		oid = vid_to_data_oid(vid, i);
 
-		if (inode->data_vdi_id[i] != inode->vdi_id) {
+		if (vid != inode->vdi_id) {
 			sd_debug("object %" PRIx64 " is base's data, would"
 				 " not be deleted.", oid);
 			continue;
diff --git a/sheepfs/volume.c b/sheepfs/volume.c
index ca8925a..29abb62 100644
--- a/sheepfs/volume.c
+++ b/sheepfs/volume.c
@@ -117,7 +117,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size,
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	int ret, fd, sock_idx;
 	bool create = false;
-	uint32_t vid = oid_to_vid(oid);
+	uint32_t vid = oid_to_vid(oid), vdi_id;
 	struct vdi_inode *vdi;
 	unsigned long idx = 0;
 	uint64_t cow_oid = 0;
@@ -129,7 +129,8 @@ 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);
-		if (!vdi->inode->data_vdi_id[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) {
 				memset(buf, 0, size);
@@ -138,14 +139,10 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size,
 			create = true;
 		} else {
 			if (rw == VOLUME_READ) {
-				oid = vid_to_data_oid(
-					vdi->inode->data_vdi_id[idx],
-					idx);
+				oid = vid_to_data_oid(vdi_id, idx);
 			/* in case we are writing a COW object */
 			} else if (!is_data_obj_writeable(vdi->inode, idx)) {
-				cow_oid = vid_to_data_oid(
-						vdi->inode->data_vdi_id[idx],
-						idx);
+				cow_oid = vid_to_data_oid(vdi_id, idx);
 				hdr.flags |= SD_FLAG_CMD_COW;
 				create = true;
 			}
@@ -179,7 +176,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t size,
 	}
 
 	if (create) {
-		vdi->inode->data_vdi_id[idx] = vid;
+		sd_inode_set_vid(vdi->inode, idx, vid);
 		/* writeback inode update */
 		if (volume_rw_object((char *)&vid, vid_to_vdi_oid(vid),
 				     sizeof(vid),
-- 
1.7.1




More information about the sheepdog mailing list