blob: c4406fd1e604d59be548f73a4e116d6c5b00126c [file] [log] [blame]
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +00001/*
2 * flushb.c --- Hides system-dependent information for both syncing a
3 * device to disk and to flush any buffers from disk cache.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04004 *
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +00005 * Copyright (C) 2000 Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000010 * %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'od90f3492001-06-22 21:01:17 -040023#if HAVE_SYS_MOUNT_H
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010024#include <sys/param.h>
Theodore Ts'od90f3492001-06-22 21:01:17 -040025#include <sys/mount.h> /* This may define BLKFLSBUF */
26#endif
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000027
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000028#include "ext2_fs.h"
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000029#include "ext2fs.h"
30
31/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -040032 * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since
Theodore Ts'od90f3492001-06-22 21:01:17 -040033 * not all portable header file does so for us. This really should be
34 * fixed in the glibc header files. (Recent glibcs appear to define
35 * BLKFLSBUF in sys/mount.h, but FDFLUSH still doesn't seem to be
36 * defined anywhere portable.) Until then....
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000037 */
Theodore Ts'od90f3492001-06-22 21:01:17 -040038#ifdef __linux__
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000039#ifndef BLKFLSBUF
Theodore Ts'od90f3492001-06-22 21:01:17 -040040#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000041#endif
42#ifndef FDFLUSH
Theodore Ts'od90f3492001-06-22 21:01:17 -040043#define FDFLUSH _IO(2,0x4b) /* flush floppy disk */
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000044#endif
45#endif
46
47/*
48 * This function will sync a device/file, and optionally attempt to
49 * flush the buffer cache. The latter is basically only useful for
50 * system benchmarks and for torturing systems in burn-in tests. :)
51 */
52errcode_t ext2fs_sync_device(int fd, int flushb)
53{
54 /*
55 * We always sync the device in case we're running on old
56 * kernels for which we can lose data if we don't. (There
57 * still is a race condition for those kernels, but this
58 * reduces it greatly.)
59 */
60 if (fsync (fd) == -1)
61 return errno;
62
63 if (flushb) {
64
65#ifdef BLKFLSBUF
Theodore Ts'o55ca9ae2002-10-31 12:21:05 -050066 if (ioctl (fd, BLKFLSBUF, 0) == 0)
67 return 0;
JP Abgralle0ed7402014-03-19 19:08:39 -070068#elif defined(__linux__)
69#warning BLKFLSBUF not defined
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000070#endif
71#ifdef FDFLUSH
JP Abgralle0ed7402014-03-19 19:08:39 -070072 return ioctl(fd, FDFLUSH, 0); /* In case this is a floppy */
73#elif defined(__linux__)
74#warning FDFLUSH not defined
Theodore Ts'o4d0f3e12001-01-11 15:48:50 +000075#endif
76 }
77 return 0;
78}