[sheepdog-users] [PATCH stable-0.7] sheep: remove sha1_from_buffer()

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed Dec 18 06:51:38 CET 2013


From: Liu Yuan <namei.unix at gmail.com>

This function is buggy yet tricky to debug. For now we can't pass following
script:

for i in `seq 0 2`; do
	sheep/sheep -D -n -z $i -p $((7000+$i)) -c local store/$i
done
sleep 1
dog/dog cluster format -c 3
qemu-img convert linux-0.2.img sheepdog:test
dog/dog vdi snapshot test -s snap
dog/dog cluster snapshot save snap1 snapshot
md5sum /tmp/store/0/obj/{007c2b2500000000,007c2b2500000001,007c2b2500000002,007c2b2500000003,007c2b2500000004}
dog/dog cluster snapshot load snap1 snapshot
md5sum /tmp/store/0/obj/{007c2b2500000000,007c2b2500000001,007c2b2500000002,007c2b2500000003,007c2b2500000004}
dog/dog vdi read test | md5sum
md5sum linux-0.2.img

-----------------------------------------

Instead of debugging of it, this patch tries to remove it since the speedup
from sha1_from_buffer is very limited because we don't have many zero filled
objects.

Reviewed-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/farm/sha1_file.c |    4 ++--
 include/sha1.h       |    2 +-
 lib/sha1.c           |   24 ++----------------------
 sheep/plain_store.c  |    2 +-
 4 files changed, 6 insertions(+), 26 deletions(-)

diff --git a/dog/farm/sha1_file.c b/dog/farm/sha1_file.c
index 3ba2519..ec2ccfb 100644
--- a/dog/farm/sha1_file.c
+++ b/dog/farm/sha1_file.c
@@ -88,7 +88,7 @@ int sha1_file_write(void *buf, size_t len, unsigned char *outsha1)
 {
 	unsigned char sha1[SHA1_DIGEST_SIZE];
 
-	sha1_from_buffer(buf, len, sha1);
+	get_buffer_sha1(buf, len, sha1);
 	if (sha1_buffer_write(sha1, buf, len) < 0)
 		return -1;
 	if (outsha1)
@@ -101,7 +101,7 @@ static int verify_sha1_file(const unsigned char *sha1,
 {
 	unsigned char tmp[SHA1_DIGEST_SIZE];
 
-	sha1_from_buffer(buf, len, tmp);
+	get_buffer_sha1(buf, len, tmp);
 	if (memcmp((char *)tmp, (char *)sha1, SHA1_DIGEST_SIZE) != 0) {
 		sd_err("failed, %s != %s", sha1_to_hex(sha1), sha1_to_hex(tmp));
 		return -1;
diff --git a/include/sha1.h b/include/sha1.h
index a778aea..7fa7855 100644
--- a/include/sha1.h
+++ b/include/sha1.h
@@ -27,6 +27,6 @@ void sha1_init(void *ctx);
 void sha1_update(void *ctx, const uint8_t *data, unsigned int len);
 void sha1_final(void *ctx, uint8_t *out);
 const char *sha1_to_hex(const unsigned char *sha1);
-void sha1_from_buffer(const void *buf, size_t size, unsigned char *sha1);
+void get_buffer_sha1(unsigned char *buf, unsigned len, unsigned char *sha1);
 
 #endif
diff --git a/lib/sha1.c b/lib/sha1.c
index 85db278..f02692a 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -211,31 +211,11 @@ const char *sha1_to_hex(const unsigned char *sha1)
 	return buffer;
 }
 
-/*
- * Calculate a sha1 message digest based on the content of 'buf'
- *
- * We can uniquely generate the original buffer from
- * - the trimmed buffer
- * - the orignal buffer length
- * - the trimmed buffer length
- * - the trimmed buffer offset
- *
- * This calculates a unique sha1 digest faster than the naive calculation when
- * the content of 'buf' is sparse.  The result will be set in 'sha1'.
- */
-void sha1_from_buffer(const void *buf, size_t size, unsigned char *sha1)
+void get_buffer_sha1(unsigned char *buf, unsigned len, unsigned char *sha1)
 {
 	struct sha1_ctx c;
-	uint64_t offset = 0;
-	uint32_t length = size;
 
 	sha1_init(&c);
-	sha1_update(&c, (uint8_t *)&length, sizeof(length));
-
-	find_zero_blocks(buf, &offset, &length);
-
-	sha1_update(&c, (uint8_t *)&length, sizeof(length));
-	sha1_update(&c, (uint8_t *)&offset, sizeof(offset));
-	sha1_update(&c, buf, length);
+	sha1_update(&c, buf, len);
 	sha1_final(&c, sha1);
 }
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index 6a54b60..f0bcdf5 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -560,7 +560,7 @@ int default_get_hash(uint64_t oid, uint32_t epoch, uint8_t *sha1)
 		return ret;
 	}
 
-	sha1_from_buffer(buf, length, sha1);
+	get_buffer_sha1(buf, length, sha1);
 	free(buf);
 
 	sd_debug("the message digest of %"PRIx64" at epoch %d is %s", oid,
-- 
1.7.10.4




More information about the sheepdog-users mailing list