Blame lib/ext2fs/flushb.c

Packit a62e42
/*
Packit a62e42
 * flushb.c --- Hides system-dependent information for both syncing a
Packit a62e42
 * 	device to disk and to flush any buffers from disk cache.
Packit a62e42
 *
Packit a62e42
 * Copyright (C) 2000 Theodore Ts'o.
Packit a62e42
 *
Packit a62e42
 * %Begin-Header%
Packit a62e42
 * This file may be redistributed under the terms of the GNU Library
Packit a62e42
 * General Public License, version 2.
Packit a62e42
 * %End-Header%
Packit a62e42
 */
Packit a62e42
Packit a62e42
#include "config.h"
Packit a62e42
#include <stdio.h>
Packit a62e42
#if HAVE_ERRNO_H
Packit a62e42
#include <errno.h>
Packit a62e42
#endif
Packit a62e42
#if HAVE_UNISTD_H
Packit a62e42
#include <unistd.h>
Packit a62e42
#endif
Packit a62e42
#if HAVE_SYS_IOCTL_H
Packit a62e42
#include <sys/ioctl.h>
Packit a62e42
#endif
Packit a62e42
#if HAVE_SYS_MOUNT_H
Packit a62e42
#include <sys/param.h>
Packit a62e42
#include <sys/mount.h>		/* This may define BLKFLSBUF */
Packit a62e42
#endif
Packit a62e42
Packit a62e42
#include "ext2_fs.h"
Packit a62e42
#include "ext2fs.h"
Packit a62e42
Packit a62e42
/*
Packit a62e42
 * For Linux, define BLKFLSBUF and FDFLUSH if necessary, since
Packit a62e42
 * not all portable header file does so for us.  This really should be
Packit a62e42
 * fixed in the glibc header files.  (Recent glibcs appear to define
Packit a62e42
 * BLKFLSBUF in sys/mount.h, but FDFLUSH still doesn't seem to be
Packit a62e42
 * defined anywhere portable.)  Until then....
Packit a62e42
 */
Packit a62e42
#ifdef __linux__
Packit a62e42
#ifndef BLKFLSBUF
Packit a62e42
#define BLKFLSBUF	_IO(0x12,97)	/* flush buffer cache */
Packit a62e42
#endif
Packit a62e42
#ifndef FDFLUSH
Packit a62e42
#define FDFLUSH		_IO(2,0x4b)	/* flush floppy disk */
Packit a62e42
#endif
Packit a62e42
#endif
Packit a62e42
Packit a62e42
/*
Packit a62e42
 * This function will sync a device/file, and optionally attempt to
Packit a62e42
 * flush the buffer cache.  The latter is basically only useful for
Packit a62e42
 * system benchmarks and for torturing systems in burn-in tests.  :)
Packit a62e42
 */
Packit a62e42
errcode_t ext2fs_sync_device(int fd, int flushb)
Packit a62e42
{
Packit a62e42
	/*
Packit a62e42
	 * We always sync the device in case we're running on old
Packit a62e42
	 * kernels for which we can lose data if we don't.  (There
Packit a62e42
	 * still is a race condition for those kernels, but this
Packit a62e42
	 * reduces it greatly.)
Packit a62e42
	 */
Packit a62e42
#if defined(HAVE_FSYNC)
Packit a62e42
	if (fsync (fd) == -1)
Packit a62e42
		return errno;
Packit a62e42
#endif
Packit a62e42
Packit a62e42
	if (flushb) {
Packit a62e42
		errcode_t	retval = 0;
Packit a62e42
Packit a62e42
#ifdef BLKFLSBUF
Packit a62e42
		if (ioctl (fd, BLKFLSBUF, 0) == 0)
Packit a62e42
			return 0;
Packit a62e42
		retval = errno;
Packit a62e42
#elif defined(__linux__)
Packit a62e42
#warning BLKFLSBUF not defined
Packit a62e42
#endif
Packit a62e42
#ifdef FDFLUSH
Packit a62e42
		/* In case this is a floppy */
Packit a62e42
		if (ioctl(fd, FDFLUSH, 0) == 0)
Packit a62e42
			return 0;
Packit a62e42
		if (retval == 0)
Packit a62e42
			retval = errno;
Packit a62e42
#elif defined(__linux__)
Packit a62e42
#warning FDFLUSH not defined
Packit a62e42
#endif
Packit a62e42
		return retval;
Packit a62e42
	}
Packit a62e42
	return 0;
Packit a62e42
}