Whamcloud - gitweb
libext2fs: don't use O_DIRECT for files on tmpfs
[tools/e2fsprogs.git] / lib / ext2fs / flushb.c
index 80a2117..bb7daf4 100644 (file)
@@ -1,15 +1,16 @@
 /*
  * flushb.c --- Hides system-dependent information for both syncing a
  *     device to disk and to flush any buffers from disk cache.
- * 
+ *
  * Copyright (C) 2000 Theodore Ts'o.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #if HAVE_ERRNO_H
 #include <errno.h>
@@ -21,6 +22,7 @@
 #include <sys/ioctl.h>
 #endif
 #if HAVE_SYS_MOUNT_H
+#include <sys/param.h>
 #include <sys/mount.h>         /* This may define BLKFLSBUF */
 #endif
 
@@ -28,7 +30,7 @@
 #include "ext2fs.h"
 
 /*
- * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since 
+ * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since
  * not all portable header file does so for us.  This really should be
  * fixed in the glibc header files.  (Recent glibcs appear to define
  * BLKFLSBUF in sys/mount.h, but FDFLUSH still doesn't seem to be
@@ -56,26 +58,31 @@ errcode_t ext2fs_sync_device(int fd, int flushb)
         * still is a race condition for those kernels, but this
         * reduces it greatly.)
         */
+#if defined(HAVE_FSYNC)
        if (fsync (fd) == -1)
                return errno;
+#endif
 
        if (flushb) {
+               errcode_t       retval = 0;
 
 #ifdef BLKFLSBUF
                if (ioctl (fd, BLKFLSBUF, 0) == 0)
                        return 0;
-#else
- #ifdef __GNUC__
-  #warning BLKFLSBUF not defined
- #endif
+               retval = errno;
+#elif defined(__linux__)
+#warning BLKFLSBUF not defined
 #endif
 #ifdef FDFLUSH
-               ioctl (fd, FDFLUSH, 0);   /* In case this is a floppy */
-#else
- #ifdef __GNUC__
-  #warning FDFLUSH not defined
- #endif
+               /* In case this is a floppy */
+               if (ioctl(fd, FDFLUSH, 0) == 0)
+                       return 0;
+               if (retval == 0)
+                       retval = errno;
+#elif defined(__linux__)
+#warning FDFLUSH not defined
 #endif
+               return retval;
        }
        return 0;
 }