[CRIU] [PATCH 4/4] deduplication: add punching via fallocate for list of regions

Tikhomirov Pavel snorcht at gmail.com
Wed Oct 9 07:06:28 PDT 2013


Signed-off-by: Tikhomirov Pavel <snorcht at gmail.com>
---
 cr-dedup.c         |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 include/cr-dedup.h |    9 +++++++++
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/cr-dedup.c b/cr-dedup.c
index 645f435..a93cff1 100755
--- a/cr-dedup.c
+++ b/cr-dedup.c
@@ -21,7 +21,12 @@ int cr_dedup(pid_t pid)
 	unsigned long va, i, off;
 	struct iovec iov, iov2;
 
-	ret = open_page_read(pid, &pr);
+	struct list_head *pos, *q;
+	struct to_punch * to_punch_temp = NULL;
+	struct to_punch * tmp;
+	LIST_HEAD(to_punch_list);
+
+	ret = open_page_rw(pid, &pr);
 	if (ret)
 		return -1;
 	
@@ -50,6 +55,8 @@ int cr_dedup(pid_t pid)
 		goto exit;
 
 	off = 0;
+	to_punch_temp = (struct to_punch *)malloc(sizeof(struct to_punch));
+	to_punch_temp->off = -1;
 	while (1) {
 		unsigned long nr;
 		if (iov.iov_base != iov2.iov_base) {
@@ -99,7 +106,20 @@ int cr_dedup(pid_t pid)
 			if (memcmp(buf, buf2, PAGE_SIZE) != 0) {
 				pr_info("Pages Not Equal : %lu\n", va);
 			} else {
-
+				if (to_punch_temp->off == -1) {
+					to_punch_temp->off = off;
+					to_punch_temp->nr_pages = 1;
+				} else {
+					if (to_punch_temp->off + to_punch_temp->nr_pages == off)
+						to_punch_temp->nr_pages++;
+					else {
+						list_add(&(to_punch_temp->list), &(to_punch_list));
+						to_punch_temp = (struct to_punch *)malloc(sizeof(struct to_punch));
+						to_punch_temp->off = off;
+						to_punch_temp->nr_pages = 1;
+						//write temp to list
+					}
+				}
 			}
 			
 			off++;
@@ -123,6 +143,22 @@ int cr_dedup(pid_t pid)
 		if (ret <= 0)
 			break;
 	}
+
+	if (to_punch_temp->off != -1) {
+		list_add(&(to_punch_temp->list), &(to_punch_list));
+		//write temp to list
+	}
+
+	//Punching is here
+	list_for_each(pos, &to_punch_list){
+		tmp= list_entry(pos, struct to_punch, list);
+		pr_err("Punch hole: off=%lu/nr_pages=%lu\n", tmp->off, tmp->nr_pages);
+		ret = fallocate(pr.fd_pg, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, tmp->off * PAGE_SIZE, tmp->nr_pages * PAGE_SIZE);
+		if (ret != 0) {
+			pr_err("Error fallocate : %d\n", errno);
+			goto exit;
+		}
+	}
 exit:
 	pr_info("Dedup test END\n");
 
@@ -141,5 +177,16 @@ exit:
 			return ret;
 	}
 
+	if (to_punch_temp)
+		if(to_punch_temp->off == -1)
+			free(to_punch_temp);
+
+	//cleen list
+	list_for_each_safe(pos, q, &to_punch_list){
+		to_punch_temp = list_entry(pos, struct to_punch, list);
+		list_del(pos);
+		free(to_punch_temp);
+	}
+
 	return 0;
 }
diff --git a/include/cr-dedup.h b/include/cr-dedup.h
index 4a66e03..b1d7848 100644
--- a/include/cr-dedup.h
+++ b/include/cr-dedup.h
@@ -1,6 +1,15 @@
 #ifndef __CR_DEDUP_H__
 #define __CR_DEDUP_H__
 
+#define FALLOC_FL_KEEP_SIZE     0x01 /* default is extend size */
+#define FALLOC_FL_PUNCH_HOLE    0x02 /* de-allocates range */
+
+struct to_punch{
+        struct list_head list;
+        unsigned long off;
+        unsigned long nr_pages;
+};
+
 int cr_dedup(pid_t pid);
 
 #endif
-- 
1.7.9.5



More information about the CRIU mailing list