dovecot: If posix_fallocate() returns EINVAL, fallback to writing.

dovecot at dovecot.org dovecot at dovecot.org
Sun Oct 21 14:32:37 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/a9dfe05dfadd
changeset: 6585:a9dfe05dfadd
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Oct 21 14:32:34 2007 +0300
description:
If posix_fallocate() returns EINVAL, fallback to writing.

diffstat:

1 file changed, 16 insertions(+), 8 deletions(-)
src/lib/file-set-size.c |   24 ++++++++++++++++--------

diffs (51 lines):

diff -r 4fb613eb8ce9 -r a9dfe05dfadd src/lib/file-set-size.c
--- a/src/lib/file-set-size.c	Sun Oct 21 04:49:45 2007 +0300
+++ b/src/lib/file-set-size.c	Sun Oct 21 14:32:34 2007 +0300
@@ -10,11 +10,12 @@
 
 int file_set_size(int fd, off_t size)
 {
-#ifndef HAVE_POSIX_FALLOCATE
+#ifdef HAVE_POSIX_FALLOCATE
+	static bool posix_fallocate_supported = TRUE;
+#endif
 	char block[4096];
 	off_t offset;
 	ssize_t ret;
-#endif
 	struct stat st;
 
 	i_assert(size >= 0);
@@ -35,12 +36,20 @@ int file_set_size(int fd, off_t size)
 		return 0;
 
 #ifdef HAVE_POSIX_FALLOCATE
-	if (posix_fallocate(fd, st.st_size, size - st.st_size) < 0) {
-		if (!ENOSPACE(errno))
-			i_error("posix_fallocate() failed: %m");
-		return -1;
+	if (posix_fallocate_supported) {
+		if (posix_fallocate(fd, st.st_size, size - st.st_size) == 0)
+			return 0;
+
+		if (errno != EINVAL) {
+			if (!ENOSPACE(errno))
+				i_error("posix_fallocate() failed: %m");
+			return -1;
+		}
+		/* Solaris seems to fail with EINVAL if it's not supported
+		   by the kernel. Fallback to writing. */
+		posix_fallocate_supported = FALSE;
 	}
-#else
+#endif
 	/* start growing the file */
 	offset = st.st_size;
 	memset(block, 0, I_MIN((ssize_t)sizeof(block), size - offset));
@@ -56,6 +65,5 @@ int file_set_size(int fd, off_t size)
 		}
 		offset += size;
 	}
-#endif
 	return 0;
 }


More information about the dovecot-cvs mailing list