[sheepdog] [PATCH] sheep: use ftruncate instead of writing to the last sector

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Fri Oct 5 04:47:02 CEST 2012


This also moves err_to_sderr to outside prealloc because we cannot
know the oid of the file inside prealloc.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 sheep/object_cache.c |    4 ++--
 sheep/plain_store.c  |   44 ++++++++++++--------------------------------
 2 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index f2f164c..6fd505c 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -712,9 +712,9 @@ static int object_cache_lookup(struct object_cache *oc, uint32_t idx,
 		data_length = SD_DATA_OBJ_SIZE;
 
 	ret = prealloc(fd, data_length);
-	if (ret != SD_RES_SUCCESS) {
+	if (ret < 0)
 		ret = SD_RES_EIO;
-	} else
+	else
 		add_to_object_cache(oc, idx, writeback);
 
 	close(fd);
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index ac73938..9e59a80 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -274,30 +274,6 @@ int default_read(uint64_t oid, struct siocb *iocb)
 	return ret;
 }
 
-static int write_last_sector(int fd, uint32_t length)
-{
-	const int size = SECTOR_SIZE;
-	char *buf;
-	int ret;
-	off_t off = length - size;
-
-	buf = valloc(size);
-	if (!buf) {
-		eprintf("failed to allocate memory\n");
-		return SD_RES_NO_MEM;
-	}
-	memset(buf, 0, size);
-
-	ret = xpwrite(fd, buf, size, off);
-	if (ret != size)
-		ret = err_to_sderr(0, errno); /* FIXME: set oid */
-	else
-		ret = SD_RES_SUCCESS;
-	free(buf);
-
-	return ret;
-}
-
 /*
  * Preallocate the whole object to get a better filesystem layout.
  */
@@ -305,13 +281,15 @@ int prealloc(int fd, uint32_t size)
 {
 	int ret = fallocate(fd, 0, 0, size);
 	if (ret < 0) {
-		if (errno != ENOSYS && errno != EOPNOTSUPP)
-			ret = err_to_sderr(0, errno); /* FIXME: set oid */
-		else
-			ret = write_last_sector(fd, size);
-	} else
-		ret = SD_RES_SUCCESS;
-	return ret;
+		if (errno != ENOSYS && errno != EOPNOTSUPP) {
+			eprintf("failed to preallocate space, %m\n");
+			return ret;
+		}
+
+		return ftruncate(fd, size);
+	}
+
+	return 0;
 }
 
 int default_create_and_write(uint64_t oid, struct siocb *iocb)
@@ -341,8 +319,10 @@ int default_create_and_write(uint64_t oid, struct siocb *iocb)
 
 	if (iocb->offset != 0 || iocb->length != get_objsize(oid)) {
 		ret = prealloc(fd, get_objsize(oid));
-		if (ret != SD_RES_SUCCESS)
+		if (ret < 0) {
+			ret = err_to_sderr(oid, errno);
 			goto out;
+		}
 	}
 
 	ret = xpwrite(fd, iocb->buf, len, iocb->offset);
-- 
1.7.2.5




More information about the sheepdog mailing list