[sheepdog] [PATCH RFC 1/4] add helper function to check the vdi is snapshot

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Apr 25 12:23:24 CEST 2013


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/collie.h          |    1 -
 collie/common.c          |    7 +------
 collie/node.c            |    2 +-
 collie/vdi.c             |   24 ++++++++++++------------
 include/sheepdog_proto.h |    5 +++++
 sheep/vdi.c              |    6 +++---
 6 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/collie/collie.h b/collie/collie.h
index 5973748..b319f9b 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -56,7 +56,6 @@ extern struct sd_vnode sd_vnodes[SD_MAX_VNODES];
 extern int sd_nodes_nr, sd_vnodes_nr;
 extern unsigned master_idx;
 
-bool is_current(const struct sheepdog_inode *i);
 char *size_to_str(uint64_t _size, char *str, int str_size);
 typedef void (*vdi_parser_func_t)(uint32_t vid, const char *name,
 				  const char *tag, uint32_t snapid,
diff --git a/collie/common.c b/collie/common.c
index 0642ee0..08a7e3a 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -11,11 +11,6 @@
 
 #include "collie.h"
 
-bool is_current(const struct sheepdog_inode *i)
-{
-	return !i->snap_ctime;
-}
-
 char *size_to_str(uint64_t _size, char *str, int str_size)
 {
 	const char *units[] = {"MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
@@ -163,7 +158,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void *data)
 			}
 		}
 
-		snapid = is_current(&i) ? 0 : i.snap_id;
+		snapid = sd_is_snapshot(&i) ? i.snap_id : 0;
 		func(i.vdi_id, i.name, i.tag, snapid, 0, &i, data);
 	}
 
diff --git a/collie/node.c b/collie/node.c
index d92c078..c3f257c 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -21,7 +21,7 @@ static void cal_total_vdi_size(uint32_t vid, const char *name, const char *tag,
 {
 	uint64_t *size = data;
 
-	if (is_current(i))
+	if (!sd_is_snapshot(i))
 		*size += i->vdi_size;
 }
 
diff --git a/collie/vdi.c b/collie/vdi.c
index 5594a51..54ff20d 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -126,7 +126,7 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 		is_clone = true;
 
 	if (raw_output) {
-		printf("%c ", is_current(i) ? (is_clone ? 'c' : '=') : 's');
+		printf("%c ", sd_is_snapshot(i) ? 's' : (is_clone ? 'c' : '='));
 		while (*name) {
 			if (isspace(*name) || *name == '\\')
 				putchar('\\');
@@ -137,9 +137,9 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 				i->nr_copies, i->tag);
 	} else {
 		printf("%c %-8s %5d %7s %7s %7s %s  %7" PRIx32 " %5d %13s\n",
-				is_current(i) ? (is_clone ? 'c' : ' ') : 's',
-				name, snapid, vdi_size_str, my_objs_str, cow_objs_str,
-				dbuf, vid, i->nr_copies, i->tag);
+		       sd_is_snapshot(i) ? 's' : (is_clone ? 'c' : ' '),
+		       name, snapid, vdi_size_str, my_objs_str, cow_objs_str,
+		       dbuf, vid, i->nr_copies, i->tag);
 	}
 }
 
@@ -151,17 +151,17 @@ static void print_vdi_tree(uint32_t vid, const char *name, const char *tag,
 	struct tm tm;
 	char buf[128];
 
-	if (is_current(i))
-		pstrcpy(buf, sizeof(buf), "(you are here)");
-	else {
+	if (sd_is_snapshot(i)) {
 		ti = i->create_time >> 32;
 		localtime_r(&ti, &tm);
 
 		strftime(buf, sizeof(buf),
 			 "[%Y-%m-%d %H:%M]", &tm);
-	}
+	} else
+		pstrcpy(buf, sizeof(buf), "(you are here)");
 
-	add_vdi_tree(name, buf, vid, i->parent_vdi_id, highlight && is_current(i));
+	add_vdi_tree(name, buf, vid, i->parent_vdi_id,
+		     highlight && !sd_is_snapshot(i));
 }
 
 static void print_vdi_graph(uint32_t vid, const char *name, const char *tag,
@@ -191,10 +191,10 @@ static void print_vdi_graph(uint32_t vid, const char *name, const char *tag,
 	       "Time: %10s",
 	       name, snapid, size_str, dbuf, tbuf);
 
-	if (is_current(i))
-		printf("\",\n    color=\"red\"\n  ];\n\n");
-	else
+	if (sd_is_snapshot(i))
 		printf("\"\n  ];\n\n");
+	else
+		printf("\",\n    color=\"red\"\n  ];\n\n");
 
 }
 
diff --git a/include/sheepdog_proto.h b/include/sheepdog_proto.h
index 627a90b..028b1be 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -282,4 +282,9 @@ static inline uint32_t attr_oid_to_vid(uint64_t oid)
 	return (~VDI_ATTR_BIT & oid) >> VDI_SPACE_SHIFT;
 }
 
+static inline bool sd_is_snapshot(const struct sheepdog_inode *inode)
+{
+	return inode->snap_ctime != 0;
+}
+
 #endif
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 5f23846..e3c6c80 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -334,7 +334,7 @@ static int find_first_vdi(unsigned long start, unsigned long end,
 		if (!strncmp(inode->name, name, strlen(inode->name))) {
 			*next_snap = inode->snap_id + 1;
 
-			if (!(tag && tag[0]) && !snapid && inode->snap_ctime)
+			if (!(tag && tag[0]) && !snapid && sd_is_snapshot(inode))
 				break;
 
 			vdi_found = true;
@@ -345,7 +345,7 @@ static int find_first_vdi(unsigned long start, unsigned long end,
 				if (snapid != inode->snap_id)
 					continue;
 
-				if (inode->snap_ctime == 0) {
+				if (!sd_is_snapshot(inode)) {
 					sd_dprintf("vdi %" PRIx32 " is not a "
 						   "snapshot\n", inode->vdi_id);
 					ret = SD_RES_INVALID_PARMS;
@@ -766,7 +766,7 @@ next:
 
 	if (vid == inode->vdi_id && inode->snap_id == 1
 			&& inode->parent_vdi_id != 0
-			&& !inode->snap_ctime) {
+			&& !sd_is_snapshot(inode)) {
 		sd_dprintf("vdi %" PRIx32 " is a cloned vdi.", vid);
 		/* current vdi is a cloned vdi */
 		*cloned = true;
-- 
1.7.2.5




More information about the sheepdog mailing list