[sheepdog] [PATCH UPDATE] farm: add flock in read_working_object()

levin li levin108 at gmail.com
Wed Jul 11 04:41:24 CEST 2012


From: levin li <xingke.lwp at taobao.com>

We should lock the object when read it in read_working_object
just as we did in farm_read

Signed-off-by: levin li <xingke.lwp at taobao.com>
---
 sheep/farm/farm.c |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
index 9e05e12..8169991 100644
--- a/sheep/farm/farm.c
+++ b/sheep/farm/farm.c
@@ -376,15 +376,27 @@ static void *read_working_object(uint64_t oid, uint64_t offset,
 		goto out;
 	}
 
+	if (flock(fd, LOCK_SH) < 0) {
+		eprintf("%m\n");
+		goto err;
+	}
+
 	size = xpread(fd, buf, length, offset);
+	if (flock(fd, LOCK_UN) < 0) {
+		eprintf("%m\n");
+		goto err;
+	}
+
 	if (length != size) {
 		eprintf("size %zu len %"PRIu32" off %"PRIu64" %m\n", size,
 			length, offset);
-		free(buf);
-		buf = NULL;
-		goto out;
+		goto err;
 	}
+	goto out;
 
+err:
+	free(buf);
+	buf = NULL;
 out:
 	if (fd > 0)
 		close(fd);
-- 
1.7.1




More information about the sheepdog mailing list