test_io.c revision 4cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3
119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * test_io.c --- This is the Test I/O interface.
319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright (C) 1996 Theodore Ts'o.
519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * This file may be redistributed under the terms of the GNU Public
819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * License.
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
1019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
1119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
1219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdio.h>
1319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
144cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
1519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <unistd.h>
164cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
1719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdlib.h>
1819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <fcntl.h>
1919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <time.h>
2019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/stat.h>
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/types.h>
2219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#if HAVE_ERRNO_H
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <errno.h>
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#endif
2519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "et/com_err.h"
2719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "ext2fs/ext2_err.h"
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include "io.h"
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * For checking structure magic numbers...
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#define EXT2_CHECK_MAGIC(struct, code) \
3519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	  if ((struct)->magic != (code)) return (code)
3619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostruct test_private_data {
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int	magic;
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel real;
4019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*read_blk)(unsigned long block, int count, errcode_t err);
4119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*write_blk)(unsigned long block, int count, errcode_t err);
4219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*set_blksize)(int blksize, errcode_t err);
4319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
4419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
4519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel);
4619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel);
4719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize);
4819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
4919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *data);
5019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
5119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o				int count, const void *data);
5219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel);
5319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
5419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic struct struct_io_manager struct_test_manager = {
5519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_ET_MAGIC_IO_MANAGER,
5619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	"Test I/O Manager",
5719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_open,
5819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_close,
5919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_set_blksize,
6019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_read_blk,
6119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_write_blk,
6219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_flush
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
6419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_manager = &struct_test_manager;
6619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
6819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * These global variable can be set by the test program as
6919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * necessary *before* calling test_open
7019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
7119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_backing_manager = 0;
7219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_read_blk)
7319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
7419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_write_blk)
7519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
7619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_set_blksize)
7719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(int blksize, errcode_t err) = 0;
7819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
7919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel)
8019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
8119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel	io = NULL;
8219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data = NULL;
8319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval;
8419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
8519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (name == 0)
8619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return EXT2_ET_BAD_DEVICE_NAME;
8719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io = (io_channel) malloc(sizeof(struct struct_io_channel));
8819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (!io)
8919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return ENOMEM;
9019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(io, 0, sizeof(struct struct_io_channel));
9119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
9219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *)
9319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		malloc(sizeof(struct test_private_data));
9419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (!data) {
9519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = ENOMEM;
9619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
9719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
9819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->manager = test_io_manager;
9919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->name = malloc(strlen(name)+1);
10019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (!io->name) {
10119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = ENOMEM;
10219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
10319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
10419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	strcpy(io->name, name);
10519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->private_data = data;
10619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->block_size = 1024;
10719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->read_error = 0;
10819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->write_error = 0;
109a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	io->refcount = 1;
11019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
11119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(data, 0, sizeof(struct test_private_data));
11219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
11319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (test_io_backing_manager) {
11419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = test_io_backing_manager->open(name, flags,
11519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o						       &data->real);
11619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		if (retval)
11719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			goto cleanup;
11819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	} else
11919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->real = 0;
12019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->read_blk = 	test_io_cb_read_blk;
12119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->write_blk = 	test_io_cb_write_blk;
12219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->set_blksize = 	test_io_cb_set_blksize;
12319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
12419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	*channel = io;
12519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
12619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
12719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ocleanup:
12819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (io)
12919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		free(io);
13019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data)
13119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		free(data);
13219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
13319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
13419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
13519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel)
13619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
13719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
13819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
13919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
14019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
14119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
14219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
14319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
144a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (--channel->refcount > 0)
145a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		return 0;
146a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
14719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
14819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_close(data->real);
14919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
15019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->private_data)
15119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		free(channel->private_data);
15219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->name)
15319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		free(channel->name);
15419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	free(channel);
15519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
15619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
15719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
15819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize)
15919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
16019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
16119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
16219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
16319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
16419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
16519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
16619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
16719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
16819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_set_blksize(data->real, blksize);
16919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->set_blksize)
17019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->set_blksize(blksize, retval);
17119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
17219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: set_blksize(%d) returned %s\n",
17319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       blksize, retval ? error_message(retval) : "OK");
17419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
17519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
17619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
17719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
17819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
17919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *buf)
18019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
18119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
18219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
18319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
18419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
18519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
18619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
18719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
18819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
18919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_read_blk(data->real, block, count, buf);
19019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->read_blk)
19119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->read_blk(block, count, retval);
19219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
19319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: read_blk(%lu, %d) returned %s\n",
19419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       block, count, retval ? error_message(retval) : "OK");
19519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
19619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
19719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
19819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
19919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, const void *buf)
20019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
20119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
20219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
20319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
20519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
20619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
20719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
20919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_write_blk(data->real, block, count, buf);
21019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->write_blk)
21119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->write_blk(block, count, retval);
21219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
21319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: write_blk(%lu, %d) returned %s\n",
21419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       block, count, retval ? error_message(retval) : "OK");
21519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
21619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
21719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
21819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
21919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Flush data buffers to disk.
22019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
22119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel)
22219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
22319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
22419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
22519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
22619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
22719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
22819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
22919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
23019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return io_channel_flush(data->real);
23119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
23219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
23319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
234