From 61928e47565c16878a65af9d6c3d4c33d7061f72 Mon Sep 17 00:00:00 2001 From: Robert Zeh Date: Wed, 19 Dec 2018 15:25:09 -0600 Subject: [PATCH] Detect out of space errors with posix_fallocate By switching from ftruncate to posix_fallocate we error out early if there isn't enough disk space for the incoming file. If we use ftruncate a sparse file is created that is then mmapped. As the bytes come over the network and the file is populated we get a SIGBUS when the kernel is unable to allocate space on the disk. --- librdmacm/examples/rcopy.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/librdmacm/examples/rcopy.c b/librdmacm/examples/rcopy.c index 0bcae00a1..5801c547c 100644 --- a/librdmacm/examples/rcopy.c +++ b/librdmacm/examples/rcopy.c @@ -227,9 +227,10 @@ static int server_listen(void) return ret; } +static char *path = NULL; + static int server_open(int rs, struct msg_hdr *msg) { - char *path = NULL; int ret, len; printf("opening: "); @@ -264,8 +265,6 @@ static int server_open(int rs, struct msg_hdr *msg) ret = 0; out: - if (path) - free(path); msg_send_resp(rs, msg, ret); return ret; @@ -286,6 +285,10 @@ static void server_close(int rs, struct msg_hdr *msg) close(fd); fd = 0; } + + free(path); + path = NULL; + printf("done\n"); } @@ -312,9 +315,13 @@ static int server_write(int rs, struct msg_hdr *msg) if (ret != sizeof bytes) goto out; - ret = ftruncate(fd, bytes); - if (ret) + ret = posix_fallocate(fd, 0, bytes); + if (ret) { + printf("...error allocating file\n"); + close(fd); + unlink(path); goto out; + } file_addr = mmap(NULL, bytes, PROT_WRITE, MAP_SHARED, fd, 0); if (file_addr == (void *) -1) {