summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt3
-rw-r--r--config.h.in11
-rw-r--r--src/kvstorage_file.c1
-rw-r--r--src/statfile.c2
-rw-r--r--src/util.c13
-rw-r--r--src/util.h9
6 files changed, 38 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6bf8884a8..3e6c4fd79 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -656,6 +656,7 @@ CHECK_INCLUDE_FILES(grp.h HAVE_GRP_H)
CHECK_INCLUDE_FILES(glob.h HAVE_GLOB_H)
CHECK_INCLUDE_FILES(poll.h HAVE_POLL_H)
CHECK_INCLUDE_FILES(sys/sendfile.h HAVE_SYS_SENDFILE_H)
+CHECK_INCLUDE_FILES(linux/falloc.h HAVE_LINUX_FALLOC_H)
# Some dependencies
IF(HAVE_SYS_WAIT_H)
@@ -690,6 +691,8 @@ CHECK_SYMBOL_EXISTS(MAP_ANON sys/mman.h HAVE_MMAP_ANON)
CHECK_SYMBOL_EXISTS(MAP_NOCORE sys/mman.h HAVE_MMAP_NOCORE)
CHECK_SYMBOL_EXISTS(O_DIRECT fcntl.h HAVE_O_DIRECT)
CHECK_SYMBOL_EXISTS(posix_fadvise fcntl.h HAVE_FADVISE)
+CHECK_SYMBOL_EXISTS(posix_fallocate fcntl.h HAVE_POSIX_FALLOCATE)
+CHECK_SYMBOL_EXISTS(fallocate fcntl.h HAVE_FALLOCATE)
CHECK_SYMBOL_EXISTS(fdatasync unistd.h HAVE_FDATASYNC)
CHECK_SYMBOL_EXISTS(_SC_NPROCESSORS_ONLN unistd.h HAVE_SC_NPROCESSORS_ONLN)
IF(HAVE_SIGINFO_H)
diff --git a/config.h.in b/config.h.in
index 999340f07..aeefa9b0a 100644
--- a/config.h.in
+++ b/config.h.in
@@ -85,7 +85,7 @@
#endif /* HAVE_ENDIAN_H */
#if !defined(BYTE_ORDER) || (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN)
- error "Undefined or unknown BYTE_ORDER";
+ # error "Undefined or unknown BYTE_ORDER"
#endif
@@ -123,6 +123,10 @@
#cmakedefine HAVE_O_DIRECT 1
#cmakedefine HAVE_FADVISE 1
+
+#cmakedefine HAVE_FALLOCATE 1
+#cmakedefine HAVE_POSIX_FALLOCATE 1
+
#cmakedefine HAVE_FDATASYNC 1
#cmakedefine HAVE_COMPATIBLE_QUEUE_H 1
@@ -307,6 +311,11 @@
#include <fcntl.h>
#endif
+/* Linux specific falloc.h */
+#ifdef HAVE_LINUX_FALLOC_H
+#include <linux/falloc.h>
+#endif
+
/* poll */
#ifdef HAVE_POLL_H
#include <poll.h>
diff --git a/src/kvstorage_file.c b/src/kvstorage_file.c
index 154ccf279..73e5a8a88 100644
--- a/src/kvstorage_file.c
+++ b/src/kvstorage_file.c
@@ -142,6 +142,7 @@ file_open_fd (const gchar *path, gsize *len, gint flags)
if ((flags & O_CREAT) != 0) {
/* Open file */
if ((fd = open (path, flags, S_IRUSR|S_IWUSR|S_IRGRP)) != -1) {
+ rspamd_fallocate (fd, 0, *len);
#ifdef HAVE_FADVISE
posix_fadvise (fd, 0, *len, POSIX_FADV_SEQUENTIAL);
#endif
diff --git a/src/statfile.c b/src/statfile.c
index d9d42d983..b5b06419e 100644
--- a/src/statfile.c
+++ b/src/statfile.c
@@ -465,6 +465,8 @@ statfile_pool_create (statfile_pool_t * pool, gchar *filename, size_t size)
return -1;
}
+ rspamd_fallocate (fd, 0, sizeof (header) + sizeof (section) + sizeof (block) * nblocks);
+
header.create_time = (guint64) time (NULL);
if (write (fd, &header, sizeof (header)) == -1) {
msg_info ("cannot write header to file %s, error %d, %s", filename, errno, strerror (errno));
diff --git a/src/util.c b/src/util.c
index dea3976f1..c5ec0145e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1381,6 +1381,19 @@ rspamd_strtoul (const gchar *s, gsize len, gulong *value)
return TRUE;
}
+gint
+rspamd_fallocate (gint fd, off_t offset, off_t len)
+{
+#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_KEEP_SIZE)
+ return fallocate (fd, FALLOC_FL_KEEP_SIZE, offset, len);
+#elif defined(HAVE_POSIX_FALLOCATE)
+ return posix_fallocate (fd, offset, len);
+#else
+ /* Return 0 as nothing can be done on this system */
+ return 0;
+#endif
+}
+
/*
* vi:ts=4
*/
diff --git a/src/util.h b/src/util.h
index 165b84334..c4fcc80f4 100644
--- a/src/util.h
+++ b/src/util.h
@@ -241,4 +241,13 @@ gboolean rspamd_strtol (const gchar *s, gsize len, glong *value);
*/
gboolean rspamd_strtoul (const gchar *s, gsize len, gulong *value);
+/**
+ * Try to allocate a file on filesystem (using fallocate or posix_fallocate)
+ * @param fd descriptor
+ * @param offset offset of file
+ * @param len length to allocate
+ * @return -1 in case of failure
+ */
+gint rspamd_fallocate (gint fd, off_t offset, off_t len);
+
#endif