[sheepdog] [PATCH 4/6] sheep: add a helper to get vdi's inode

Liu Yuan namei.unix at gmail.com
Sat Apr 27 07:44:20 CEST 2013


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

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/sheep_priv.h |    2 +-
 sheep/vdi.c        |  115 +++++++++++++++++++---------------------------------
 2 files changed, 43 insertions(+), 74 deletions(-)

diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index e1a3c3b..a7f0222 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -257,7 +257,7 @@ int get_obj_copy_number(uint64_t oid, int nr_zones);
 int get_max_copy_number(void);
 int get_req_copy_number(struct request *req);
 int add_vdi_copy_number(uint32_t vid, int nr_copies);
-int vdi_exist(uint32_t vid);
+bool vdi_exist(uint32_t vid);
 int vdi_create(struct vdi_iocb *iocb, uint32_t *new_vid);
 int vdi_delete(struct vdi_iocb *iocb, struct request *req);
 int vdi_lookup(struct vdi_iocb *iocb, struct vdi_info *info);
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 46fc2f3..7230940 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -170,25 +170,36 @@ static inline bool vdi_is_deleted(struct sd_inode *inode)
 	return *inode->name == '\0';
 }
 
-int vdi_exist(uint32_t vid)
+static struct sd_inode *vdi_get_inode(uint32_t vid)
 {
-	struct sd_inode *inode;
-	int ret = 1;
+	struct sd_inode *inode = NULL;
+	uint64_t oid = vid_to_vdi_oid(vid);
+	int ret;
 
-	inode = xzalloc(sizeof(*inode));
-	ret = read_object(vid_to_vdi_oid(vid), (char *)inode,
-			  sizeof(*inode), 0);
+	inode = malloc(sizeof(*inode));
+	if (!inode) {
+		sd_eprintf("failed to allocate memory");
+		return NULL;
+	}
+	ret = read_object(oid, (void *)inode, sizeof(*inode), 0);
 	if (ret != SD_RES_SUCCESS) {
-		sd_eprintf("fail to read vdi inode (%" PRIx32 ")", vid);
-		ret = 0;
-		goto out;
+		sd_eprintf("failed, %s", sd_strerror(ret));
+		free(inode);
+		return NULL;
 	}
 
-	if (vdi_is_deleted(inode)) {
-		ret = 0;
+	return inode;
+}
+
+bool vdi_exist(uint32_t vid)
+{
+	struct sd_inode *inode;
+	bool ret = false;
+
+	inode = vdi_get_inode(vid);
+	if (!inode || vdi_is_deleted(inode))
 		goto out;
-	}
-	ret = 1;
+	ret = true;;
 out:
 	free(inode);
 	return ret;
@@ -564,26 +575,19 @@ static LIST_HEAD(deletion_work_list);
 
 static int delete_inode(struct deletion_work *dw)
 {
-	struct sd_inode *inode = NULL;
-	int ret = SD_RES_SUCCESS;
+	struct sd_inode *inode = vdi_get_inode(dw->vid);
+	int ret;
 
-	inode = xzalloc(sizeof(*inode));
-	ret = read_object(vid_to_vdi_oid(dw->vid), (char *)inode,
-			  SD_INODE_HEADER_SIZE, 0);
-	if (ret != SD_RES_SUCCESS) {
-		ret = SD_RES_EIO;
-		goto out;
-	}
+	if (!inode)
+		return SD_RES_EIO;
 
 	memset(inode->name, 0, sizeof(inode->name));
-
 	ret = write_object(vid_to_vdi_oid(dw->vid), (char *)inode,
 			   SD_INODE_HEADER_SIZE, 0, false);
 	if (ret != 0) {
 		ret = SD_RES_EIO;
 		goto out;
 	}
-
 out:
 	free(inode);
 	return ret;
@@ -615,19 +619,9 @@ static void delete_one(struct work *work)
 
 	sd_dprintf("%d %d, %16x", dw->done, dw->count, vdi_id);
 
-	inode = malloc(sizeof(*inode));
-	if (!inode) {
-		sd_eprintf("failed to allocate memory");
+	inode = vdi_get_inode(vdi_id);
+	if (!inode)
 		return;
-	}
-
-	ret = read_backend_object(vid_to_vdi_oid(vdi_id),
-			  (void *)inode, sizeof(*inode), 0);
-
-	if (ret != SD_RES_SUCCESS) {
-		sd_eprintf("cannot find VDI object");
-		goto out;
-	}
 
 	if (inode->vdi_size == 0 && vdi_is_deleted(inode))
 		goto out;
@@ -697,31 +691,23 @@ static void delete_one_done(struct work *work)
 
 static int fill_vdi_list(struct deletion_work *dw, uint32_t root_vid)
 {
-	int ret, i;
+	int i;
 	struct sd_inode *inode = NULL;
 	int done = dw->count;
 	uint32_t vid;
 
-	inode = malloc(SD_INODE_HEADER_SIZE);
-	if (!inode) {
-		sd_eprintf("failed to allocate memory");
-		goto err;
-	}
-
 	dw->buf[dw->count++] = root_vid;
 again:
 	vid = dw->buf[done++];
-	ret = read_backend_object(vid_to_vdi_oid(vid), (char *)inode,
-			  SD_INODE_HEADER_SIZE, 0);
+	inode = vdi_get_inode(vid);
+	if (!inode)
+		return 0;
 
-	if (ret != SD_RES_SUCCESS) {
-		sd_eprintf("cannot find VDI object");
-		goto err;
+	if (!vdi_is_deleted(inode) && vid != dw->vid) {
+		free(inode);
+		return 1;
 	}
 
-	if (!vdi_is_deleted(inode) && vid != dw->vid)
-		goto out;
-
 	for (i = 0; i < ARRAY_SIZE(inode->child_vdi_id); i++) {
 		if (!inode->child_vdi_id[i])
 			continue;
@@ -729,32 +715,21 @@ again:
 		dw->buf[dw->count++] = inode->child_vdi_id[i];
 	}
 
+	free(inode);
 	if (dw->buf[done])
 		goto again;
-err:
-	free(inode);
 	return 0;
-out:
-	free(inode);
-	return 1;
 }
 
 static uint64_t get_vdi_root(uint32_t vid, bool *cloned)
 {
-	int ret;
 	struct sd_inode *inode = NULL;
 
 	*cloned = false;
-
-	inode = malloc(SD_INODE_HEADER_SIZE);
-	if (!inode) {
-		sd_eprintf("failed to allocate memory");
-		vid = 0;
-		goto out;
-	}
 next:
-	ret = read_backend_object(vid_to_vdi_oid(vid), (char *)inode,
-			  SD_INODE_HEADER_SIZE, 0);
+	inode = vdi_get_inode(vid);
+	if (!inode)
+		return 0;
 
 	if (vid == inode->vdi_id && inode->snap_id == 1
 			&& inode->parent_vdi_id != 0
@@ -764,21 +739,15 @@ next:
 		*cloned = true;
 	}
 
-	if (ret != SD_RES_SUCCESS) {
-		sd_eprintf("cannot find VDI object");
-		vid = 0;
-		goto out;
-	}
-
 	if (!inode->parent_vdi_id)
 		goto out;
 
 	vid = inode->parent_vdi_id;
+	free(inode);
 
 	goto next;
 out:
 	free(inode);
-
 	return vid;
 }
 
-- 
1.7.9.5




More information about the sheepdog mailing list