[Sheepdog] [PATCH 2/2 v2] object cache: fix race in	create_cache_object()
    Liu Yuan 
    namei.unix at gmail.com
       
    Mon Apr 16 16:21:31 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 |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index d75f532..e2cb9f4 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -337,7 +337,7 @@ 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_EXCL, fd, ret = SD_RES_SUCCESS;
 	struct strbuf buf;
 
 	strbuf_init(&buf, PATH_MAX);
@@ -346,6 +346,10 @@ static int create_cache_object(struct object_cache *oc, uint32_t idx, void *buff
 
 	fd = open(buf.buf, flags, def_fmode);
 	if (fd < 0) {
+		if (errno == EEXIST) {
+			dprintf("%08"PRIx32" already created\n", idx);
+			goto out;
+		}
 		ret = SD_RES_EIO;
 		goto out;
 	}
-- 
1.7.8.2
    
    
More information about the sheepdog
mailing list