14d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o/*
24d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * flushb.c --- Hides system-dependent information for both syncing a
34d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * 	device to disk and to flush any buffers from disk cache.
4efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
54d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * Copyright (C) 2000 Theodore Ts'o.
64d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o *
74d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * %Begin-Header%
8543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * This file may be redistributed under the terms of the GNU Library
9543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * General Public License, version 2.
104d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * %End-Header%
114d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o */
124d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
134d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include <stdio.h>
144d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#if HAVE_ERRNO_H
154d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include <errno.h>
164d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
174d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#if HAVE_UNISTD_H
184d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include <unistd.h>
194d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
204d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#if HAVE_SYS_IOCTL_H
214d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include <sys/ioctl.h>
224d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
23d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#if HAVE_SYS_MOUNT_H
24b34cbddbd66baedc163d36dd281b63ef478f547aMatthias Andree#include <sys/param.h>
25d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#include <sys/mount.h>		/* This may define BLKFLSBUF */
26d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#endif
274d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
284d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include "ext2_fs.h"
294d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#include "ext2fs.h"
304d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
314d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o/*
32efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since
33d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o * not all portable header file does so for us.  This really should be
34d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o * fixed in the glibc header files.  (Recent glibcs appear to define
35d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o * BLKFLSBUF in sys/mount.h, but FDFLUSH still doesn't seem to be
36d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o * defined anywhere portable.)  Until then....
374d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o */
38d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#ifdef __linux__
394d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#ifndef BLKFLSBUF
40d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#define BLKFLSBUF	_IO(0x12,97)	/* flush buffer cache */
414d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
424d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#ifndef FDFLUSH
43d90f3494b219f9fe327eaa561faf75582f65ad9eTheodore Ts'o#define FDFLUSH		_IO(2,0x4b)	/* flush floppy disk */
444d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
454d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
464d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
474d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o/*
484d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * This function will sync a device/file, and optionally attempt to
494d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * flush the buffer cache.  The latter is basically only useful for
504d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o * system benchmarks and for torturing systems in burn-in tests.  :)
514d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o */
524d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'oerrcode_t ext2fs_sync_device(int fd, int flushb)
534d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o{
544d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	/*
554d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	 * We always sync the device in case we're running on old
564d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	 * kernels for which we can lose data if we don't.  (There
574d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	 * still is a race condition for those kernels, but this
584d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	 * reduces it greatly.)
594d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	 */
604d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	if (fsync (fd) == -1)
614d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o		return errno;
624d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
634d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	if (flushb) {
644d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o
654d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#ifdef BLKFLSBUF
6655ca9ae5ecc5968e272e900b3e6e815fdeae683bTheodore Ts'o		if (ioctl (fd, BLKFLSBUF, 0) == 0)
6755ca9ae5ecc5968e272e900b3e6e815fdeae683bTheodore Ts'o			return 0;
68e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#elif defined(__linux__)
69e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#warning BLKFLSBUF not defined
704d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
714d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#ifdef FDFLUSH
72e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		return ioctl(fd, FDFLUSH, 0);   /* In case this is a floppy */
73e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#elif defined(__linux__)
74e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#warning FDFLUSH not defined
754d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o#endif
764d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	}
774d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o	return 0;
784d0f3e17a5b7556505fe437680070fe3bb140d67Theodore Ts'o}
79