blob: 6100fc61430ca44fed12bc844a00cb027b3bae8a [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
Theodore Ts'od1154eb2011-09-18 17:34:37 -040016#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#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'od90f3492001-06-22 21:01:17 -040023#include <sys/mount.h>
Theodore Ts'oadfca562000-09-12 21:24:36 +000024#include "../misc/nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'od90f3492001-06-22 21:01:17 -040026/* For Linux, define BLKFLSBUF if necessary */
27#if (!defined(BLKFLSBUF) && defined(__linux__))
28#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
Theodore Ts'obf2d4992000-12-09 14:46:20 +000029#endif
30
Theodore Ts'o3839e651997-04-26 13:21:57 +000031const char *progname;
32
Theodore Ts'o5ba23cb2001-01-11 19:15:02 +000033static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000034{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000035 fprintf(stderr, _("Usage: %s disk\n"), progname);
Theodore Ts'o3839e651997-04-26 13:21:57 +000036 exit(1);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040037}
38
Theodore Ts'o3839e651997-04-26 13:21:57 +000039int main(int argc, char **argv)
40{
41 int fd;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040042
Theodore Ts'o3839e651997-04-26 13:21:57 +000043 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'o50e1e101997-04-26 13:58:21 +000056#ifdef BLKFLSBUF
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000058 perror("ioctl BLKFLSBUF");
Theodore Ts'o3839e651997-04-26 13:21:57 +000059 exit(1);
60 }
61 return 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000062#else
63 fprintf(stderr,
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000064 _("BLKFLSBUF ioctl not supported! Can't flush buffers.\n"));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000065 return 1;
66#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000067}