[PATCH 2/4] Add a VFIL_fallocate function

Martin Blix Grydeland martin at varnish-software.com
Mon Oct 6 17:53:13 CEST 2014


This function uses fallocate to preallocate file system blocks,
returning error if the available space is exhausted.

Does nothing on platforms without fallocate
---
 configure.ac          |  1 +
 include/vfil.h        |  1 +
 lib/libvarnish/vfil.c | 16 ++++++++++++++++
 3 files changed, 18 insertions(+)

diff --git a/configure.ac b/configure.ac
index d4f5c5b..2facc53 100644
--- a/configure.ac
+++ b/configure.ac
@@ -205,6 +205,7 @@ AC_CHECK_FUNCS([getdtablesize])
 AC_CHECK_FUNCS([timegm])
 AC_CHECK_FUNCS([nanosleep])
 AC_CHECK_FUNCS([setppriv])
+AC_CHECK_FUNCS([fallocate])
 
 save_LIBS="${LIBS}"
 LIBS="${PTHREAD_LIBS}"
diff --git a/include/vfil.h b/include/vfil.h
index 533dfd8..8c4dcd6 100644
--- a/include/vfil.h
+++ b/include/vfil.h
@@ -34,3 +34,4 @@ int VFIL_tmpfile(char *);
 char *VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz);
 char *VFIL_readfd(int fd, ssize_t *sz);
 int VFIL_nonblocking(int fd);
+int VFIL_fallocate(int fd, off_t size);
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index 920a06f..cf734ea 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -136,3 +136,19 @@ VFIL_nonblocking(int fd)
 	assert(i != -1);
 	return (i);
 }
+
+int
+VFIL_fallocate(int fd, off_t size)
+{
+#ifdef HAVE_FALLOCATE
+	if (fallocate(fd, 0, 0, size) && errno == ENOSPC)
+		return (-1);
+	/* Ignore other errors */
+	return (0);
+#else
+	/* Do nothing */
+	(void)fd;
+	(void)size;
+	return (0);
+#endif
+}
-- 
2.1.1




More information about the varnish-dev mailing list