git » libjio » commit 3072c48

libjio: Improve sync_file_range() detection code

author Alberto Bertogli
2009-07-07 03:19:12 UTC
committer Alberto Bertogli
2009-07-07 03:31:06 UTC
parent fd6fbd3bdbd9999a4ca8813d14964b350a759914

libjio: Improve sync_file_range() detection code

This patch makes the sync_file_range() detection code a little cleaner,
and also makes the stub functions work, just in case we want to use them
unconditionally in the future.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

libjio/compat.c +21 -31
libjio/compat.h +19 -3

diff --git a/libjio/compat.c b/libjio/compat.c
index 3ad4463..78e14f0 100644
--- a/libjio/compat.c
+++ b/libjio/compat.c
@@ -3,64 +3,53 @@
  * Compatibility functions
  */
 
-/* To get sync_file_range() we need to temporarily define _GNU_SOURCE, which
- * is not the nicest thing, but is not worth defining globally */
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#define _REMOVE_GNU_SOURCE
-#endif
-
-/* Must be down here because otherwise we might try to #include things twice:
- * once with _GNU_SOURCE and one without it */
 #include "compat.h"
+#include <sys/types.h>		/* off_t, size_t */
+#include <unistd.h>		/* fdatasync(), if available */
 
 
 /*
  * sync_file_range() support through an internal similar API
  */
 
-#include <fcntl.h>		/* sync_range_submit(), if possible */
-#include <sys/types.h>		/* off_t, size_t */
-
-#ifdef SYNC_FILE_RANGE_WRITE
-const int have_sync_range = 1;
+#ifdef LACK_SYNC_FILE_RANGE
+#warning "Using fdatasync() instead of sync_file_range()"
+const int have_sync_range = 0;
 
-/** Initiate write-out of the dirty pages in the range */
 int sync_range_submit(int fd, off_t offset, size_t nbytes)
 {
-	/* We don't need SYNC_FILE_RANGE_WAIT_BEFORE because we have exclusive
-	 * access to the range (guaranteed by the caller) */
-	return sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
+	return 0;
 }
 
-/** Wait for completion of the previously-submitted I/O in the given ranges.
- * Does NOT force the submission of any new I/O. */
 int sync_range_wait(int fd, off_t offset, size_t nbytes)
 {
-	return sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WAIT_BEFORE);
+	/* fdatasync() waits for the submitted I/O to complete, so it's enough
+	 * to call it once here */
+	return fdatasync(fd);
 }
 
 #else
 
-#warning "Using fdatasync() instead of sync_file_range()"
-const int have_sync_range = 0;
+/** Indicates whether we have a full implementation of sync_range_submit() and
+ * sync_range_wait(), so we can take advantage of it. */
+const int have_sync_range = 1;
 
+/** Initiate write-out of the dirty pages in the range */
 int sync_range_submit(int fd, off_t offset, size_t nbytes)
 {
-	return -1;
+	/* We don't need SYNC_FILE_RANGE_WAIT_BEFORE because we have exclusive
+	 * access to the range (guaranteed by the caller) */
+	return sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
 }
 
+/** Wait for completion of the previously-submitted I/O in the given ranges.
+ * Does NOT force the submission of any new I/O. */
 int sync_range_wait(int fd, off_t offset, size_t nbytes)
 {
-	return -1;
+	return sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WAIT_BEFORE);
 }
 
-#endif /* defined SYNC_FILE_RANGE_WRITE */
-
-/* It is no longer needed */
-#ifdef _REMOVE_GNU_SOURCE
-#undef _GNU_SOURCE
-#endif
+#endif /* defined LACK_SYNC_FILE_RANGE */
 
 
 /* When posix_fadvise() is not available, we just show a message since there
@@ -93,6 +82,7 @@ int clock_gettime(int clk_id, struct timespec *tp)
 
 #endif /* defined LACK_CLOCK_GETTIME */
 
+
 #ifdef LACK_FDATASYNC
 #warning "Using fsync() instead of fdatasync()"
 
diff --git a/libjio/compat.h b/libjio/compat.h
index ecfd9ff..427aa4d 100644
--- a/libjio/compat.h
+++ b/libjio/compat.h
@@ -4,12 +4,28 @@
 #ifndef _COMPAT_H
 #define _COMPAT_H
 
-#include <sys/types.h>		/* off_t, size_t */
-
 
 /* sync_file_range() is linux-specific, so we provide an internal similar API,
  * with a constant to be able to check for its presence; the implementation is
- * in compat.c */
+ * in compat.c.
+ *
+ * To get its constants we need to temporarily define _GNU_SOURCE, which is
+ * not the nicest thing, but is not worth defining globally. */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#define _REMOVE_GNU_SOURCE
+#endif
+#include <fcntl.h>		/* SYNC_FILE_RANGE_WRITE, if available */
+#ifdef _REMOVE_GNU_SOURCE
+#undef _REMOVE_GNU_SOURCE
+#undef _GNU_SOURCE
+#endif
+
+#ifndef SYNC_FILE_RANGE_WRITE
+#define LACK_SYNC_FILE_RANGE 1
+#endif
+
+#include <sys/types.h>		/* off_t, size_t */
 extern const int have_sync_range;
 int sync_range_submit(int fd, off_t offset, size_t nbytes);
 int sync_range_wait(int fd, off_t offset, size_t nbytes);