From 44311f4235463a8d4d3c4128242bbd7c9d2a203b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 14 Dec 2011 19:34:50 +0300 Subject: [PATCH] Detect and use fallocate/posix_fallocate. --- CMakeLists.txt | 3 +++ config.h.in | 11 ++++++++++- src/kvstorage_file.c | 1 + src/statfile.c | 2 ++ src/util.c | 13 +++++++++++++ src/util.h | 9 +++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) 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 #endif +/* Linux specific falloc.h */ +#ifdef HAVE_LINUX_FALLOC_H +#include +#endif + /* poll */ #ifdef HAVE_POLL_H #include 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 -- 2.39.5