[sheepdog] [PATCH stable-0.6 3/3] sheep: introduce xfallocate and xftruncate helpers

Hitoshi Mitake mitake.hitoshi at gmail.com
Sun Jul 14 18:35:16 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

These helpers retry the syscalls while the error is retry-able.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 include/util.h        |    2 ++
 lib/util.c            |   22 ++++++++++++++++++++++
 sheep/cluster/local.c |    6 +++---
 sheep/journal.c       |    2 +-
 sheep/migrate.c       |    2 +-
 sheep/plain_store.c   |    4 ++--
 6 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/include/util.h b/include/util.h
index 1c42ff8..2edb6e3 100644
--- a/include/util.h
+++ b/include/util.h
@@ -84,6 +84,8 @@ ssize_t xwrite(int fd, const void *buf, size_t len);
 ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
 ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);
 int xmkdir(const char *pathname, mode_t mode);
+int xfallocate(int fd, int mode, off_t offset, off_t len);
+int xftruncate(int fd, off_t length);
 void pstrcpy(char *buf, int buf_size, const char *str);
 int rmdir_r(char *dir_path);
 int purge_directory(char *dir_path);
diff --git a/lib/util.c b/lib/util.c
index 253a0c5..974c914 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -252,6 +252,28 @@ int xmkdir(const char *pathname, mode_t mode)
 	return 0;
 }
 
+int xfallocate(int fd, int mode, off_t offset, off_t len)
+{
+	int ret;
+
+	do {
+		ret = fallocate(fd, mode, offset, len);
+	} while (ret < 0 && (errno == EAGAIN || errno == EINTR));
+
+	return ret;
+}
+
+int xftruncate(int fd, off_t length)
+{
+	int ret;
+
+	do {
+		ret = ftruncate(fd, length);
+	} while (ret < 0 && (errno == EAGAIN || errno == EINTR));
+
+	return ret;
+}
+
 /*
  * Copy the string str to buf. If str length is bigger than buf_size -
  * 1 then it is clamped to buf_size - 1.
diff --git a/sheep/cluster/local.c b/sheep/cluster/local.c
index 2d298d8..02cbef2 100644
--- a/sheep/cluster/local.c
+++ b/sheep/cluster/local.c
@@ -213,7 +213,7 @@ static void shm_queue_init(void)
 
 	shm_queue_lock();
 
-	ret = ftruncate(shmfd, sizeof(*shm_queue));
+	ret = xftruncate(shmfd, sizeof(*shm_queue));
 	if (ret != 0)
 		panic("failed to truncate shmfile, %m");
 
@@ -229,10 +229,10 @@ static void shm_queue_init(void)
 		/* initialize shared memory */
 		block_event_pos = 0;
 		nonblock_event_pos = 0;
-		ret = ftruncate(shmfd, 0);
+		ret = xftruncate(shmfd, 0);
 		if (ret != 0)
 			panic("failed to truncate shmfile, %m");
-		ret = ftruncate(shmfd, sizeof(*shm_queue));
+		ret = xftruncate(shmfd, sizeof(*shm_queue));
 		if (ret != 0)
 			panic("failed to truncate shmfile, %m");
 	}
diff --git a/sheep/journal.c b/sheep/journal.c
index 50783f2..be0a9e7 100644
--- a/sheep/journal.c
+++ b/sheep/journal.c
@@ -336,7 +336,7 @@ static void *commit_data(void *ignored)
 		panic("%s", strerror(err));
 
 	sync();
-	if (ftruncate(jfile.commit_fd, 0) < 0)
+	if (xftruncate(jfile.commit_fd, 0) < 0)
 		panic("truncate %m");
 	if (prealloc(jfile.commit_fd, jfile_size) < 0)
 		panic("prealloc");
diff --git a/sheep/migrate.c b/sheep/migrate.c
index ad3903a..9cc963f 100644
--- a/sheep/migrate.c
+++ b/sheep/migrate.c
@@ -295,7 +295,7 @@ static int migrate_from_v0_to_v1(void)
 	}
 
 	/* 0.5.1 could wrongly extend the config file, so truncate it here */
-	ret = ftruncate(fd, sizeof(config));
+	ret = xftruncate(fd, sizeof(config));
 	if (ret != 0) {
 		sd_eprintf("failed to truncate config data, %m");
 		close(fd);
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index e6323ac..80a5ebd 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -282,14 +282,14 @@ int default_read(uint64_t oid, const struct siocb *iocb)
 /* Preallocate the whole object to get a better filesystem layout. */
 int prealloc(int fd, uint32_t size)
 {
-	int ret = fallocate(fd, 0, 0, size);
+	int ret = xfallocate(fd, 0, 0, size);
 	if (ret < 0) {
 		if (errno != ENOSYS && errno != EOPNOTSUPP) {
 			sd_eprintf("failed to preallocate space, %m");
 			return ret;
 		}
 
-		return ftruncate(fd, size);
+		return xftruncate(fd, size);
 	}
 
 	return 0;
-- 
1.7.5.1




More information about the sheepdog mailing list