[sheepdog] [PATCH 01/10] sheep: introduce xfallocate and xftruncate helpers
MORITA Kazutaka
morita.kazutaka at gmail.com
Tue May 21 02:11:50 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>
---
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 1603796..8f92cf0 100644
--- a/include/util.h
+++ b/include/util.h
@@ -83,6 +83,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 61a1cb5..fac468a 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 ae7941b..0d354a7 100644
--- a/sheep/cluster/local.c
+++ b/sheep/cluster/local.c
@@ -212,7 +212,7 @@ static void shm_queue_init(void)
shm_queue_lock();
- ret = ftruncate(shmfd, sizeof(*shm_queue));
+ ret = xftruncate(shmfd, sizeof(*shm_queue));
assert(ret == 0);
shm_queue = mmap(NULL, sizeof(*shm_queue),
@@ -226,9 +226,9 @@ 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);
assert(ret == 0);
- ret = ftruncate(shmfd, sizeof(*shm_queue));
+ ret = xftruncate(shmfd, sizeof(*shm_queue));
assert(ret == 0);
}
diff --git a/sheep/journal.c b/sheep/journal.c
index e2fce5c..d3698aa 100644
--- a/sheep/journal.c
+++ b/sheep/journal.c
@@ -342,7 +342,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 e5c2f37..55d923d 100644
--- a/sheep/migrate.c
+++ b/sheep/migrate.c
@@ -175,7 +175,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 7f6b52d..2e8e20c 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -278,14 +278,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.9.5
More information about the sheepdog
mailing list