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