Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * flushb.c --- Hides system-dependent information for both syncing a |
| 3 | * device to disk and to flush any buffers from disk cache. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 4 | * |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 Theodore Ts'o. |
| 6 | * |
| 7 | * %Begin-Header% |
Theodore Ts'o | 543547a | 2010-05-17 21:31:56 -0400 | [diff] [blame] | 8 | * This file may be redistributed under the terms of the GNU Library |
| 9 | * General Public License, version 2. |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 10 | * %End-Header% |
| 11 | */ |
| 12 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 13 | #include "config.h" |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #if HAVE_ERRNO_H |
| 16 | #include <errno.h> |
| 17 | #endif |
| 18 | #if HAVE_UNISTD_H |
| 19 | #include <unistd.h> |
| 20 | #endif |
| 21 | #if HAVE_SYS_IOCTL_H |
| 22 | #include <sys/ioctl.h> |
| 23 | #endif |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 24 | #if HAVE_SYS_MOUNT_H |
Matthias Andree | b34cbdd | 2003-12-28 18:21:26 +0100 | [diff] [blame] | 25 | #include <sys/param.h> |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 26 | #include <sys/mount.h> /* This may define BLKFLSBUF */ |
| 27 | #endif |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 28 | |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 29 | #include "ext2_fs.h" |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 30 | #include "ext2fs.h" |
| 31 | |
| 32 | /* |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 33 | * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 34 | * not all portable header file does so for us. This really should be |
| 35 | * fixed in the glibc header files. (Recent glibcs appear to define |
| 36 | * BLKFLSBUF in sys/mount.h, but FDFLUSH still doesn't seem to be |
| 37 | * defined anywhere portable.) Until then.... |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 38 | */ |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 39 | #ifdef __linux__ |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 40 | #ifndef BLKFLSBUF |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 41 | #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */ |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 42 | #endif |
| 43 | #ifndef FDFLUSH |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 44 | #define FDFLUSH _IO(2,0x4b) /* flush floppy disk */ |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 45 | #endif |
| 46 | #endif |
| 47 | |
| 48 | /* |
| 49 | * This function will sync a device/file, and optionally attempt to |
| 50 | * flush the buffer cache. The latter is basically only useful for |
| 51 | * system benchmarks and for torturing systems in burn-in tests. :) |
| 52 | */ |
| 53 | errcode_t ext2fs_sync_device(int fd, int flushb) |
| 54 | { |
| 55 | /* |
| 56 | * We always sync the device in case we're running on old |
| 57 | * kernels for which we can lose data if we don't. (There |
| 58 | * still is a race condition for those kernels, but this |
| 59 | * reduces it greatly.) |
| 60 | */ |
| 61 | if (fsync (fd) == -1) |
| 62 | return errno; |
| 63 | |
| 64 | if (flushb) { |
| 65 | |
| 66 | #ifdef BLKFLSBUF |
Theodore Ts'o | 55ca9ae | 2002-10-31 12:21:05 -0500 | [diff] [blame] | 67 | if (ioctl (fd, BLKFLSBUF, 0) == 0) |
| 68 | return 0; |
Andreas Dilger | a1a7699 | 2011-06-11 11:50:01 -0400 | [diff] [blame] | 69 | #elif defined(__linux__) |
| 70 | #warning BLKFLSBUF not defined |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 71 | #endif |
| 72 | #ifdef FDFLUSH |
Theodore Ts'o | 55ca9ae | 2002-10-31 12:21:05 -0500 | [diff] [blame] | 73 | ioctl (fd, FDFLUSH, 0); /* In case this is a floppy */ |
Andreas Dilger | a1a7699 | 2011-06-11 11:50:01 -0400 | [diff] [blame] | 74 | #elif defined(__linux__) |
| 75 | #warning FDFLUSH not defined |
Theodore Ts'o | 4d0f3e1 | 2001-01-11 15:48:50 +0000 | [diff] [blame] | 76 | #endif |
| 77 | } |
| 78 | return 0; |
| 79 | } |