[sheepdog] [PATCH 1/2] util: add find_zero_blocks()

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Jul 19 06:16:56 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This adds a helper function which does the similar thing as
trim_zero_blocks() but doesn't update the given buffer.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/util.h |    1 +
 lib/util.c     |   24 +++++++++++++++++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/include/util.h b/include/util.h
index b2c5395..583fa9e 100644
--- a/include/util.h
+++ b/include/util.h
@@ -95,6 +95,7 @@ void reraise_crash_signal(int signo, int status);
 pid_t gettid(void);
 bool is_xattr_enabled(const char *path);
 
+void find_zero_blocks(const void *buf, uint64_t *poffset, uint32_t *plen);
 void trim_zero_blocks(void *buf, uint64_t *offset, uint32_t *len);
 void untrim_zero_blocks(void *buf, uint64_t offset, uint32_t len,
 			uint32_t requested_len);
diff --git a/lib/util.c b/lib/util.c
index 69bedfb..5fe0bfa 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -355,7 +355,7 @@ int rmdir_r(char *dir_path)
 }
 
 /*
- * Trim zero blocks from the beginning and end of buffer
+ * Find zero blocks from the beginning and end of buffer
  *
  * The caller passes the offset of 'buf' with 'poffset' so that this funciton
  * can align the return values to BLOCK_SIZE.  'plen' points the length of the
@@ -365,10 +365,10 @@ int rmdir_r(char *dir_path)
  * buffer, this function also decreases the length on condition that '*plen' is
  * block-aligned.
  */
-void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen)
+void find_zero_blocks(const void *buf, uint64_t *poffset, uint32_t *plen)
 {
 	const uint8_t zero[BLOCK_SIZE] = {0};
-	uint8_t *p = buf;
+	const uint8_t *p = buf;
 	uint64_t start = *poffset;
 	uint64_t offset = 0;
 	uint32_t len = *plen;
@@ -385,8 +385,6 @@ void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen)
 		offset += size;
 		len -= size;
 	}
-	if (offset > 0)
-		memmove(buf, p + offset, len);
 
 	/* trim zero sectors from the end of buffer */
 	while (len >= BLOCK_SIZE) {
@@ -405,6 +403,22 @@ void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen)
 }
 
 /*
+ * Trim zero blocks from the beginning and end of buffer
+ *
+ * This function is similar to find_zero_blocks(), but this updates 'buf' so
+ * that the zero block are removed from the beginning of buffer.
+ */
+void trim_zero_blocks(void *buf, uint64_t *poffset, uint32_t *plen)
+{
+	uint8_t *p = buf;
+	uint64_t orig_offset = *poffset;
+
+	find_zero_blocks(buf, poffset, plen);
+	if (orig_offset < *poffset)
+		memmove(p, p + *poffset - orig_offset, *plen);
+}
+
+/*
  * Untrim zero blocks to the beginning and end of buffer
  *
  * 'offset' is the offset of 'buf' in the original buffer, 'len' is the length
-- 
1.7.9.5




More information about the sheepdog mailing list