blob: e42982653f4b7abb1f1f90c8d553ece505dffe22 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * flushb.c --- Hides system-dependent information for both syncing a
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 * device to disk and to flush any buffers from disk cache.
5 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00006 * Copyright (C) 2000 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
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
24#if HAVE_SYS_MOUNT_H
25#include <sys/param.h>
26#include <sys/mount.h> /* This may define BLKFLSBUF */
27#endif
28
29#include "ext2_fs.h"
30#include "ext2fs.h"
31
32/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033 * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since
Mike Frysinger1fd98e02005-05-09 22:10:42 +000034 * 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....
38 */
39#ifdef __linux__
40#ifndef BLKFLSBUF
41#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
42#endif
43#ifndef FDFLUSH
44#define FDFLUSH _IO(2,0x4b) /* flush floppy disk */
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 */
53errcode_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
67 if (ioctl (fd, BLKFLSBUF, 0) == 0)
68 return 0;
69#else
70#ifdef __GNUC__
Mike Frysingerf8855132006-03-28 02:35:56 +000071# warning BLKFLSBUF not defined
Mike Frysinger1fd98e02005-05-09 22:10:42 +000072#endif /* __GNUC__ */
73#endif
74#ifdef FDFLUSH
75 ioctl (fd, FDFLUSH, 0); /* In case this is a floppy */
76#else
77#ifdef __GNUC__
Mike Frysingerf8855132006-03-28 02:35:56 +000078# warning FDFLUSH not defined
Mike Frysinger1fd98e02005-05-09 22:10:42 +000079#endif /* __GNUC__ */
80#endif
81 }
82 return 0;
83}