[sheepdog] [PATCH v3 11/12] sheep: make stat_data_objs a generic function

Liu Yuan namei.unix at gmail.com
Wed Jan 29 09:26:25 CET 2014


and rename it as sd_inode_stat()

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/vdi.c                | 96 +-----------------------------------------------
 include/internal_proto.h |  3 ++
 lib/sd_inode.c           | 95 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 95 deletions(-)

diff --git a/dog/vdi.c b/dog/vdi.c
index 1e175b3..091ce91 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -87,100 +87,6 @@ static void vdi_show_progress(uint64_t done, uint64_t total)
 	return show_progress(done, total, false);
 }
 
-struct stat_arg {
-	uint64_t *my;
-	uint64_t *cow;
-	uint32_t vid;
-};
-
-static void stat_cb(void *data, enum btree_node_type type, void *arg)
-{
-	struct sd_extent *ext;
-	struct stat_arg *sarg = arg;
-	uint64_t *my = sarg->my;
-	uint64_t *cow = sarg->cow;
-
-	if (type == BTREE_EXT) {
-		ext = (struct sd_extent *)data;
-		if (ext->vdi_id == sarg->vid)
-			(*my)++;
-		else if (ext->vdi_id != 0)
-			(*cow)++;
-	}
-}
-
-static void stat_data_objs_btree(const struct sd_inode *inode,
-				 uint64_t *my_objs, uint64_t *cow_objs)
-{
-	struct stat_arg arg = {my_objs, cow_objs, inode->vdi_id};
-	traverse_btree(dog_bnode_reader, inode, stat_cb, &arg);
-}
-
-static void stat_data_objs_array(const struct sd_inode *inode,
-				 uint64_t *my_objs, uint64_t *cow_objs)
-{
-	int nr;
-	uint64_t my, cow, *p;
-	uint32_t vid = inode->vdi_id;
-
-	my = 0;
-	cow = 0;
-	nr = count_data_objs(inode);
-
-	if (nr % 2 != 0) {
-		if (is_data_obj_writeable(inode, 0))
-			my++;
-		else if (inode->data_vdi_id[0] != 0)
-			cow++;
-		p = (uint64_t *)(inode->data_vdi_id + 1);
-	} else
-		p = (uint64_t *)inode->data_vdi_id;
-
-	/*
-	 * To boost performance, this function checks data_vdi_id for each 64
-	 * bit integer.
-	 */
-	nr /= 2;
-	for (int i = 0; i < nr; i++) {
-		if (p[i] == 0)
-			continue;
-		if (p[i] == (((uint64_t)vid << 32) | vid)) {
-			my += 2;
-			continue;
-		}
-
-		/* Check the higher 32 bit */
-		if (p[i] >> 32 == vid)
-			my++;
-		else if ((p[i] & 0xFFFFFFFF00000000) != 0)
-			cow++;
-
-		/* Check the lower 32 bit */
-		if ((p[i] & 0xFFFFFFFF) == vid)
-			my++;
-		else if ((p[i] & 0xFFFFFFFF) != 0)
-			cow++;
-	}
-
-	*my_objs = my;
-	*cow_objs = cow;
-}
-
-/*
- * Get the number of objects.
- *
- * 'my_objs' means the number objects which belongs to this vdi.  'cow_objs'
- * means the number of the other objects.
- */
-static void stat_data_objs(const struct sd_inode *inode, uint64_t *my_objs,
-			   uint64_t *cow_objs)
-{
-	if (inode->store_policy == 0)
-		stat_data_objs_array(inode, my_objs, cow_objs);
-	else
-		stat_data_objs_btree(inode, my_objs, cow_objs);
-}
-
 static char *redundancy_scheme(uint8_t copy_nr, uint8_t policy)
 {
 	static char str[10];
@@ -218,7 +124,7 @@ static void print_vdi_list(uint32_t vid, const char *name, const char *tag,
 			 "%Y-%m-%d %H:%M", &tm);
 	}
 
-	stat_data_objs(i, &my_objs, &cow_objs);
+	sd_inode_stat(i, &my_objs, &cow_objs, dog_bnode_reader);
 
 	if (i->snap_id == 1 && i->parent_vdi_id != 0)
 		is_clone = true;
diff --git a/include/internal_proto.h b/include/internal_proto.h
index 2e5c81c..22ea897 100644
--- a/include/internal_proto.h
+++ b/include/internal_proto.h
@@ -263,6 +263,9 @@ struct sd_stat {
 	} r;
 };
 
+void sd_inode_stat(const struct sd_inode *inode, uint64_t *, uint64_t *,
+		   read_node_fn reader);
+
 #ifdef HAVE_TRACE
 
 #define TRACE_GRAPH_ENTRY  0x01
diff --git a/lib/sd_inode.c b/lib/sd_inode.c
index c842691..dd28456 100644
--- a/lib/sd_inode.c
+++ b/lib/sd_inode.c
@@ -731,3 +731,98 @@ void sd_inode_copy_vdis(write_node_fn writer, read_node_fn reader,
 		free(leaf_node);
 	}
 }
+
+struct stat_arg {
+	uint64_t *my;
+	uint64_t *cow;
+	uint32_t vid;
+};
+
+static void stat_cb(void *data, enum btree_node_type type, void *arg)
+{
+	struct sd_extent *ext;
+	struct stat_arg *sarg = arg;
+	uint64_t *my = sarg->my;
+	uint64_t *cow = sarg->cow;
+
+	if (type == BTREE_EXT) {
+		ext = (struct sd_extent *)data;
+		if (ext->vdi_id == sarg->vid)
+			(*my)++;
+		else if (ext->vdi_id != 0)
+			(*cow)++;
+	}
+}
+
+static void hypver_volume_stat(const struct sd_inode *inode,
+			       uint64_t *my_objs, uint64_t *cow_objs,
+			       read_node_fn reader)
+{
+	struct stat_arg arg = {my_objs, cow_objs, inode->vdi_id};
+	traverse_btree(reader, inode, stat_cb, &arg);
+}
+
+static void volume_stat(const struct sd_inode *inode, uint64_t *my_objs,
+			uint64_t *cow_objs)
+{
+	int nr;
+	uint64_t my, cow, *p;
+	uint32_t vid = inode->vdi_id;
+
+	my = 0;
+	cow = 0;
+	nr = count_data_objs(inode);
+
+	if (nr % 2 != 0) {
+		if (inode->data_vdi_id[0] != inode->vdi_id)
+			my++;
+		else if (inode->data_vdi_id[0] != 0)
+			cow++;
+		p = (uint64_t *)(inode->data_vdi_id + 1);
+	} else
+		p = (uint64_t *)inode->data_vdi_id;
+
+	/*
+	 * To boost performance, this function checks data_vdi_id for each 64
+	 * bit integer.
+	 */
+	nr /= 2;
+	for (int i = 0; i < nr; i++) {
+		if (p[i] == 0)
+			continue;
+		if (p[i] == (((uint64_t)vid << 32) | vid)) {
+			my += 2;
+			continue;
+		}
+
+		/* Check the higher 32 bit */
+		if (p[i] >> 32 == vid)
+			my++;
+		else if ((p[i] & 0xFFFFFFFF00000000) != 0)
+			cow++;
+
+		/* Check the lower 32 bit */
+		if ((p[i] & 0xFFFFFFFF) == vid)
+			my++;
+		else if ((p[i] & 0xFFFFFFFF) != 0)
+			cow++;
+	}
+
+	*my_objs = my;
+	*cow_objs = cow;
+}
+
+/*
+ * Get the number of objects.
+ *
+ * 'my_objs' means the number objects which belongs to this vdi.  'cow_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)
+{
+	if (inode->store_policy == 0)
+		volume_stat(inode, my_objs, cow_objs);
+	else
+		hypver_volume_stat(inode, my_objs, cow_objs, reader);
+}
-- 
1.8.1.2




More information about the sheepdog mailing list