[sheepdog] [PATCH 2/2] dog: judge vdi is standalone or not by vdi family graph

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed May 28 06:11:13 CEST 2014


SD_MAX_CHILDREN is removed in the future because the new GC algorithm
resolves the limitation of a maximum number of children. So "dog alter
copy-vdi" command shouldn't depend on it. This patch lets the command
judge VDI is standalone or not by vdi family graph like "dog vdi
tree".

Cc: Ruoyu <liangry at ucweb.com>
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 dog/alter.c    |   32 +++++++++++++++++++++++++++-----
 dog/treeview.c |   17 ++++++-----------
 dog/treeview.h |   12 ++++++++++++
 3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/dog/alter.c b/dog/alter.c
index c2126bf..7af7f9f 100644
--- a/dog/alter.c
+++ b/dog/alter.c
@@ -15,6 +15,7 @@
 #include <sys/time.h>
 
 #include "dog.h"
+#include "treeview.h"
 
 static struct sd_option alter_options[] = {
 	{'c', "copies", true, "specify the data redundancy level"},
@@ -107,6 +108,30 @@ failure:
 	return EXIT_FAILURE;
 }
 
+static void construct_vdi_tree(uint32_t vid, const char *name, const char *tag,
+			       uint32_t snapid, uint32_t flags,
+			       const struct sd_inode *i, void *data)
+{
+	add_vdi_tree(name, tag, vid, i->parent_vdi_id, false);
+}
+
+static bool is_vdi_standalone(uint32_t vid, const char *name)
+{
+	struct vdi_tree *vdi;
+
+	init_tree();
+	if (parse_vdi(construct_vdi_tree, SD_INODE_HEADER_SIZE, NULL) < 0)
+		return EXIT_SYSFAIL;
+
+	vdi = find_vdi_from_root(vid, name);
+	if (!vdi) {
+		sd_err("failed to construct vdi tree");
+		return false;
+	}
+
+	return !vdi->pvid && list_empty(&vdi->children);
+}
+
 #define ALTER_VDI_COPY_PRINT				\
 	"    __\n"				\
 	"   ()'`;\n"				\
@@ -117,7 +142,7 @@ failure:
 static int alter_vdi_copy(int argc, char **argv)
 {
 	int ret, old_nr_copies;
-	uint32_t vid, child_vdi_id[MAX_CHILDREN];
+	uint32_t vid;
 	const char *vdiname = argv[optind++];
 	char buf[SD_INODE_HEADER_SIZE];
 	struct sd_inode *inode = (struct sd_inode *)buf;
@@ -162,10 +187,7 @@ static int alter_vdi_copy(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	memset(child_vdi_id, 0, sizeof(uint32_t) * MAX_CHILDREN);
-	if (inode->parent_vdi_id != 0 ||
-			memcmp(inode->child_vdi_id, child_vdi_id,
-			sizeof(uint32_t) * MAX_CHILDREN) != 0) {
+	if (!is_vdi_standalone(vid, inode->name)) {
 		sd_err("Only standalone vdi supports "
 			   "changing redundancy level.");
 		sd_err("Please clone %s with -n (--no-share) "
diff --git a/dog/treeview.c b/dog/treeview.c
index c5bad44..e3b75f4 100644
--- a/dog/treeview.c
+++ b/dog/treeview.c
@@ -20,21 +20,11 @@
 #define MAX_DEPTH    100
 #endif
 
-struct vdi_tree {
-	char name[1024];
-	char label[256];
-	uint32_t vid;
-	uint32_t pvid;
-	bool highlight;
-	struct list_head children;
-	struct list_node siblings;
-};
-
 static int *width, *more;
 static struct vdi_tree *root;
 
 static struct vdi_tree *find_vdi(struct vdi_tree *parent, uint32_t vid,
-				 const char *name)
+			  const char *name)
 {
 	struct vdi_tree *vdi, *ret;
 
@@ -69,6 +59,11 @@ void init_tree(void)
 	root = new_vdi("", "", 0, 0, 0);
 }
 
+struct vdi_tree *find_vdi_from_root(uint32_t vid, const char *name)
+{
+	return find_vdi(root, vid, name);
+}
+
 void add_vdi_tree(const char *name, const char *label, uint32_t vid,
 		  uint32_t pvid, bool highlight)
 {
diff --git a/dog/treeview.h b/dog/treeview.h
index 9787fbb..01443e4 100644
--- a/dog/treeview.h
+++ b/dog/treeview.h
@@ -12,10 +12,22 @@
 #define __TREEVIEW__
 
 #include <stdbool.h>
+#include "list.h"
+
+struct vdi_tree {
+	char name[1024];
+	char label[256];
+	uint32_t vid;
+	uint32_t pvid;
+	bool highlight;
+	struct list_head children;
+	struct list_node siblings;
+};
 
 void init_tree(void);
 void add_vdi_tree(const char *label, const char *tag, uint32_t vid,
 		  uint32_t pvid, bool highlight);
 void dump_tree(void);
+struct vdi_tree *find_vdi_from_root(uint32_t vid, const char *name);
 
 #endif
-- 
1.7.1




More information about the sheepdog mailing list