[Sheepdog] [PATCH 2/2] object cache: fix race in create_cache_object()

Liu Yuan namei.unix at gmail.com
Mon Apr 16 16:16:48 CEST 2012


From: Liu Yuan <tailai.ly at taobao.com>

Object cache layer allows concurrent RWs on the same oid to achieve
the best performance. We should check if object is already created or
we will get an EIO from object_cache_rw() in the race condition.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/object_cache.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index d75f532..e2c1a34 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -337,13 +337,18 @@ out:
 static int create_cache_object(struct object_cache *oc, uint32_t idx, void *buffer,
 		size_t buf_size)
 {
-	int flags = def_open_flags | O_CREAT | O_TRUNC, fd, ret;
+	int flags = def_open_flags | O_CREAT | O_TRUNC, fd, ret = SD_RES_SUCCESS;
 	struct strbuf buf;
 
 	strbuf_init(&buf, PATH_MAX);
 	strbuf_addstr(&buf, cache_dir);
 	strbuf_addf(&buf, "/%06"PRIx32"/%08"PRIx32, oc->vid, idx);
 
+	if (object_cache_lookup(oc, idx, 0) == 0) {
+		dprintf("%08"PRIx32" already created\n", idx);
+		goto out;
+	}
+
 	fd = open(buf.buf, flags, def_fmode);
 	if (fd < 0) {
 		ret = SD_RES_EIO;
-- 
1.7.8.2




More information about the sheepdog mailing list