From 3dd4200569133042cbf914f8158b703a9bb9a9a7 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Mon, 25 Sep 2017 22:22:48 +0100
Subject: [PATCH] Fix error handling for posix_fallocate().

Commit 899bd785c0edf376077d3f5d65c316f92c1b64b5 introduced the use of
posix_fallocate() to pre-allocate tmpfs space, based on an earlier
proposal that used fallocate().  It assumed that posix_fallocate() set
errno like fallocate(), but in fact it doesn't.  So fix that.

Also remove the handling for ENOSYS which is not a documented return
value for posix_fallocate(), and shouldn't happen because
posix_fallocate() is supposed to provide a fallback strategy if
the underlying fallocate syscall is not available.

Author: Thomas Munro
Discussion: https://postgr.es/m/CAEepm%3D2Z%2BUrtAApqOaBbzko8oxcb8NBmx%2B2JnLnWGRVbzZAsXQ%40mail.gmail.com
---
 src/backend/storage/ipc/dsm_impl.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/src/backend/storage/ipc/dsm_impl.c b/src/backend/storage/ipc/dsm_impl.c
index a587906..e1b4143 100644
--- a/src/backend/storage/ipc/dsm_impl.c
+++ b/src/backend/storage/ipc/dsm_impl.c
@@ -425,17 +425,14 @@ dsm_impl_posix_resize(int fd, off_t size)
 		do
 		{
 			rc = posix_fallocate(fd, 0, size);
-		} while (rc == -1 && errno == EINTR);
+		} while (rc == EINTR);
 
-		if (rc != 0 && errno == ENOSYS)
-		{
-			/*
-			 * Kernel too old (< 2.6.23).  Rather than fail, just trust that
-			 * we won't hit the problem (it typically doesn't show up without
-			 * many-GB-sized requests, anyway).
-			 */
-			rc = 0;
-		}
+		/*
+		 * The caller expects errno to be set, but posix_fallocate() doesn't 
+		 * set it.  Instead it returns error numbers directly.  So set errno,
+		 * even though we'll also return rc to indicate success or failure.
+		 */
+		errno = rc;
 	}
 #endif							/* HAVE_POSIX_FALLOCATE && __linux__ */
 
-- 
1.8.3.1

