/*
 * unix_io.c --- This is the Unix I/O interface to the I/O manager.
 *
 * Implements a one-block write-through cache.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "et/com_err.h"
#include "ext2_err.h"
#include "io.h"

struct unix_private_data {
	int	dev;
	int	block_size;
	int	flags;
	char	*buf;
	int	buf_block_nr;
};

static errcode_t unix_open(char *name, int flags, io_channel *channel);
static errcode_t unix_close(io_channel channel);
static errcode_t unix_set_blksize(io_channel channel, int blksize);
static errcode_t unix_read_blk(io_channel channel, unsigned long block,
			       int count, void *data);
static errcode_t unix_write_blk(io_channel channel, unsigned long block,
				int count, void *data);
static errcode_t unix_flush(io_channel channel);

struct struct_io_manager struct_unix_manager = {
	"Unix I/O Manager",
	unix_open,
	unix_close,
	unix_set_blksize,
	unix_read_blk,
	unix_write_blk,
	unix_flush
};

io_manager unix_io_manager = &struct_unix_manager;

static errcode_t unix_open(char *name, int flags, io_channel *channel)
{
	io_channel	io = NULL;
	struct unix_private_data *data = NULL;
	errcode_t	retval;

	io = (io_channel) malloc(sizeof(struct struct_io_channel));
	if (!io)
		return ENOMEM;
	data = (struct unix_private_data *)
		malloc(sizeof(struct unix_private_data));
	if (!data) {
		retval = ENOMEM;
		goto cleanup;
	}
	io->manager = unix_io_manager;
	io->name = strdup(name);
	if (!io->name) {
		retval = ENOMEM;
		goto cleanup;
	}
	io->private_data = data;

	memset(data, 0, sizeof(struct unix_private_data));
	data->block_size = 1024;
	data->buf = malloc(data->block_size);
	data->buf_block_nr = -1;
	if (!data->buf) {
		retval = ENOMEM;
		goto cleanup;
	}
	data->dev = open(name, (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY);
	if (data->dev < 0) {
		retval = errno;
		goto cleanup;
	}
	*channel = io;
	return 0;

cleanup:
	if (io)
		free(io);
	if (data) {
		if (data->buf)
			free(data->buf);
		free(data);
	}
	return retval;
}

static errcode_t unix_close(io_channel channel)
{
	struct unix_private_data *data;
	errcode_t	retval = 0;

	data = (struct unix_private_data *) channel->private_data;
	if (close(data->dev) < 0)
		retval = errno;
	if (data->buf)
		free(data->buf);
	if (channel->private_data)
		free(channel->private_data);
	if (channel->name)
		free(channel->name);
	free(channel);
	return retval;
}

static errcode_t unix_set_blksize(io_channel channel, int blksize)
{
	struct unix_private_data *data;

	data = (struct unix_private_data *) channel->private_data;
	if (data->block_size != blksize) {
		data->block_size = blksize;
		free(data->buf);
		data->buf = malloc(blksize);
		if (!data->buf)
			return ENOMEM;
		data->buf_block_nr = -1;
	}
	return 0;
}


static errcode_t unix_read_blk(io_channel channel, unsigned long block,
			       int count, void *buf)
{
	struct unix_private_data *data;
	errcode_t	retval;

	data = (struct unix_private_data *) channel->private_data;
	if (count > 1) {
		if (lseek(data->dev, block * data->block_size, SEEK_SET) !=
		    block * data->block_size) {
			retval = errno;
			goto cleanup;
		}
		if (read(data->dev, buf, data->block_size * count) !=
		    data->block_size * count) {
			retval = EXT2_ET_SHORT_READ;
			goto cleanup;
		}
		return 0;
	}
	if (block != data->buf_block_nr) {
		if (lseek(data->dev, block * data->block_size, SEEK_SET) !=
		    block * data->block_size) {
			retval = errno;
			goto cleanup;
		}
		data->buf_block_nr = -1;
		if (read(data->dev, data->buf, data->block_size) !=
		    data->block_size) {
			retval = EXT2_ET_SHORT_READ;
			goto cleanup;
		}
		data->buf_block_nr = block;
	}
	memcpy(buf, data->buf, data->block_size);
	return 0;
cleanup:
	memset(buf, 0, data->block_size);
	return retval;
}

static errcode_t unix_write_blk(io_channel channel, unsigned long block,
				int count, void *buf)
{
	struct unix_private_data *data;

	data = (struct unix_private_data *) channel->private_data;

	if (count > 1) {
		data->buf_block_nr = -1;	/* Invalidate the cache */
		if (lseek(data->dev, block * data->block_size, SEEK_SET) !=
		    block * data->block_size)
			return errno;
		if (write(data->dev, buf, data->block_size * count) !=
		    data->block_size * count)
			return EXT2_ET_SHORT_WRITE;
		return 0;
	}

	/*
	 * If the block in the buffer matches the block we're writing,
	 * update it to ensure cache consistency.
	 */
	if (block == data->buf_block_nr)
		memcpy(data->buf, buf, data->block_size);
	
	if (lseek(data->dev, block * data->block_size, SEEK_SET) !=
	    block * data->block_size)
		return errno;
	if (write(data->dev, buf, data->block_size * count) !=
	    data->block_size * count)
		return EXT2_ET_SHORT_WRITE;
	return 0;
}

static errcode_t unix_flush(io_channel channel)
{
	return 0;
}

