Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * flushb.c --- This routine flushes the disk buffers for a disk |
Theodore Ts'o | bf2d499 | 2000-12-09 14:46:20 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 1997, 2000, by Theodore Ts'o. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 5 | * |
Theodore Ts'o | 16fa86b | 2003-04-14 20:40:49 -0400 | [diff] [blame] | 6 | * WARNING: use of flushb on some older 2.2 kernels on a heavily loaded |
| 7 | * system will corrupt filesystems. This program is not really useful |
| 8 | * beyond for benchmarking scripts. |
| 9 | * |
| 10 | * %Begin-Header% |
| 11 | * This file may be redistributed under the terms of the GNU Public |
| 12 | * License. |
| 13 | * %End-Header% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 16 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/ioctl.h> |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 23 | #include <sys/mount.h> |
Theodore Ts'o | adfca56 | 2000-09-12 21:24:36 +0000 | [diff] [blame] | 24 | #include "../misc/nls-enable.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | d90f349 | 2001-06-22 21:01:17 -0400 | [diff] [blame] | 26 | /* For Linux, define BLKFLSBUF if necessary */ |
| 27 | #if (!defined(BLKFLSBUF) && defined(__linux__)) |
| 28 | #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */ |
Theodore Ts'o | bf2d499 | 2000-12-09 14:46:20 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 31 | const char *progname; |
| 32 | |
Theodore Ts'o | 5ba23cb | 2001-01-11 19:15:02 +0000 | [diff] [blame] | 33 | static void usage(void) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 35 | fprintf(stderr, _("Usage: %s disk\n"), progname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | exit(1); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | int main(int argc, char **argv) |
| 40 | { |
| 41 | int fd; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 42 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | progname = argv[0]; |
| 44 | if (argc != 2) |
| 45 | usage(); |
| 46 | |
| 47 | fd = open(argv[1], O_RDONLY, 0); |
| 48 | if (fd < 0) { |
| 49 | perror("open"); |
| 50 | exit(1); |
| 51 | } |
| 52 | /* |
| 53 | * Note: to reread the partition table, use the ioctl |
| 54 | * BLKRRPART instead of BLKFSLBUF. |
| 55 | */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 56 | #ifdef BLKFLSBUF |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 57 | if (ioctl(fd, BLKFLSBUF, 0) < 0) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 58 | perror("ioctl BLKFLSBUF"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | exit(1); |
| 60 | } |
| 61 | return 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 62 | #else |
| 63 | fprintf(stderr, |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 64 | _("BLKFLSBUF ioctl not supported! Can't flush buffers.\n")); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 65 | return 1; |
| 66 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 67 | } |