blob: 95ca6f8dc5082e0fb2fdb93cda7518a4b556a154 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * flushb.c --- This routine flushes the disk buffers for a disk
Theodore Ts'obf2d4992000-12-09 14:46:20 +00003 *
4 * Copyright 1997, 2000, by Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o16fa86b2003-04-14 20:40:49 -04006 * 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'o3839e651997-04-26 13:21:57 +000014 */
15
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19#include <stdlib.h>
20#include <fcntl.h>
21#include <sys/ioctl.h>
Theodore Ts'od90f3492001-06-22 21:01:17 -040022#include <sys/mount.h>
Theodore Ts'oadfca562000-09-12 21:24:36 +000023#include "../misc/nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000024
Theodore Ts'od90f3492001-06-22 21:01:17 -040025/* For Linux, define BLKFLSBUF if necessary */
26#if (!defined(BLKFLSBUF) && defined(__linux__))
27#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
Theodore Ts'obf2d4992000-12-09 14:46:20 +000028#endif
29
Theodore Ts'o3839e651997-04-26 13:21:57 +000030const char *progname;
31
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +000032static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000033{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000034 fprintf(stderr, _("Usage: %s disk\n"), progname);
Theodore Ts'o3839e651997-04-26 13:21:57 +000035 exit(1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040036}
37
Theodore Ts'o3839e651997-04-26 13:21:57 +000038int main(int argc, char **argv)
39{
40 int fd;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040041
Theodore Ts'o3839e651997-04-26 13:21:57 +000042 progname = argv[0];
43 if (argc != 2)
44 usage();
45
46 fd = open(argv[1], O_RDONLY, 0);
47 if (fd < 0) {
48 perror("open");
49 exit(1);
50 }
51 /*
52 * Note: to reread the partition table, use the ioctl
53 * BLKRRPART instead of BLKFSLBUF.
54 */
Theodore Ts'o50e1e101997-04-26 13:58:21 +000055#ifdef BLKFLSBUF
Theodore Ts'o3839e651997-04-26 13:21:57 +000056 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000057 perror("ioctl BLKFLSBUF");
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 exit(1);
59 }
60 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000061#else
62 fprintf(stderr,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000063 _("BLKFLSBUF ioctl not supported! Can't flush buffers.\n"));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000064 return 1;
65#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000066}