[Sheepdog] [PATCH] object_cache: fix a race condition

Liu Yuan namei.unix at gmail.com
Sat Apr 28 15:35:49 CEST 2012


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

create_cache_object() need to get a lock to avoid race with
object_cache_rw().

This is to address below bug in the log:
Apr 28 17:48:12 do_io_request(931) 2, 47c33f00003f80 , 1
Apr 28 17:48:12 object_cache_rw(321) 00003f80, len 4096, off 1396736
Apr 28 17:48:12 client_rx_handler(427) HEAD, fd:280
Apr 28 17:48:12 client_rx_handler(432) DATA_INIT, fd:280
Apr 28 17:48:12 client_rx_handler(470) END, fd:280
Apr 28 17:48:12 queue_request(275) 2
Apr 28 17:48:12 do_io_request(931) 2, b1f0b000003f9b , 1
Apr 28 17:48:12 object_cache_pull(423) [local] 00003f9b
Apr 28 17:48:12 object_cache_pull(423) [local] 00003f9b
Apr 28 17:48:12 create_cache_object(352) 00003f9b already created
Apr 28 17:48:12 object_cache_rw(321) 00003f9b, len 16384, off 2940928
Apr 28 17:48:12 read_cache_object(307) size =0; count= 16384
Apr 28 17:48:12 do_io_request(956) failed: 2, b1f0b000003f9b , 1, 3
Apr 28 17:48:12 io_op_done(151) leaving sheepdog cluster

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

diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index a1b9e2a..0d00bb1 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -350,12 +350,22 @@ static int create_cache_object(struct object_cache *oc, uint32_t idx, void *buff
 		ret = SD_RES_EIO;
 		goto out;
 	}
+	if (flock(fd, LOCK_EX) < 0) {
+		ret = SD_RES_EIO;
+		eprintf("%m\n");
+		goto out_close;
+	}
 	ret = xpwrite(fd, buffer, buf_size, 0);
 	if (ret != buf_size) {
 		ret = SD_RES_EIO;
 		eprintf("failed, vid %"PRIx32", idx %"PRIx32"\n", oc->vid, idx);
 		goto out_close;
 	}
+	if (flock(fd, LOCK_UN) < 0) {
+		ret = SD_RES_EIO;
+		eprintf("%m\n");
+		goto out_close;
+	}
 
 	ret = SD_RES_SUCCESS;
 	dprintf("%08"PRIx32" size %zu\n", idx, buf_size);
-- 
1.7.8.2




More information about the sheepdog mailing list