test_io.c revision 7b4e4534f9361b21d3fafdd88a58f133decee38c
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>
201d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_STAT_H
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/stat.h>
221d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
231d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_TYPES_H
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/types.h>
251d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
2619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
277b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o#include <linux/ext2_fs.h>
287b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o
297b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o#include "ext2fs.h"
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * For checking structure magic numbers...
3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#define EXT2_CHECK_MAGIC(struct, code) \
3619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	  if ((struct)->magic != (code)) return (code)
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostruct test_private_data {
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int	magic;
4019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel real;
4119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*read_blk)(unsigned long block, int count, errcode_t err);
4219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*write_blk)(unsigned long block, int count, errcode_t err);
4319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*set_blksize)(int blksize, errcode_t err);
4419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
4519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
4619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel);
4719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel);
4819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize);
4919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
5019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *data);
5119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
5219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o				int count, const void *data);
5319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel);
5419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
5519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic struct struct_io_manager struct_test_manager = {
5619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_ET_MAGIC_IO_MANAGER,
5719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	"Test I/O Manager",
5819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_open,
5919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_close,
6019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_set_blksize,
6119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_read_blk,
6219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_write_blk,
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_flush
6419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
6519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_manager = &struct_test_manager;
6719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
6919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * These global variable can be set by the test program as
7019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * necessary *before* calling test_open
7119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
7219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_backing_manager = 0;
7319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_read_blk)
7419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
7519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_write_blk)
7619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
7719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_set_blksize)
7819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(int blksize, errcode_t err) = 0;
7919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
8019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel)
8119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
8219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel	io = NULL;
8319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data = NULL;
8419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval;
8519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
8619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (name == 0)
8719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return EXT2_ET_BAD_DEVICE_NAME;
887b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct struct_io_channel),
897b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o				(void **) &io);
907b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
917b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		return retval;
9219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(io, 0, sizeof(struct struct_io_channel));
9319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
947b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct test_private_data),
957b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o				(void **) &data);
967b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval) {
97c555aebde40afdc0d15d674f2c81c0e05cfded3fTheodore Ts'o		retval = EXT2_NO_MEMORY;
9819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
9919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
10019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->manager = test_io_manager;
1017b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	retval = ext2fs_get_mem(strlen(name)+1, (void **) &io->name);
1027b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
10319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
1047b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o
10519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	strcpy(io->name, name);
10619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->private_data = data;
10719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->block_size = 1024;
10819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->read_error = 0;
10919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->write_error = 0;
110a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	io->refcount = 1;
11119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
11219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(data, 0, sizeof(struct test_private_data));
11319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
11419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (test_io_backing_manager) {
11519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = test_io_backing_manager->open(name, flags,
11619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o						       &data->real);
11719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		if (retval)
11819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			goto cleanup;
11919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	} else
12019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->real = 0;
12119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->read_blk = 	test_io_cb_read_blk;
12219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->write_blk = 	test_io_cb_write_blk;
12319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->set_blksize = 	test_io_cb_set_blksize;
12419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
12519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	*channel = io;
12619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
12719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
12819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ocleanup:
12919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (io)
1307b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		ext2fs_free_mem((void **) &io);
13119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data)
1327b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		ext2fs_free_mem((void **) &data);
13319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
13419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
13519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
13619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel)
13719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
13819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
13919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
14019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
14119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
14219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
14319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
14419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
145a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (--channel->refcount > 0)
146a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		return 0;
147a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
14819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
14919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_close(data->real);
15019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
15119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->private_data)
1527b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		ext2fs_free_mem((void **) &channel->private_data);
15319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->name)
1547b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		ext2fs_free_mem((void **) &channel->name);
1557b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	ext2fs_free_mem((void **) &channel);
15619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
15719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
15819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
15919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize)
16019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
16119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
16219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
16319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
16419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
16519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
16619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
16719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
16819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
16919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_set_blksize(data->real, blksize);
17019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->set_blksize)
17119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->set_blksize(blksize, retval);
17219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
17319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: set_blksize(%d) returned %s\n",
17419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       blksize, retval ? error_message(retval) : "OK");
17519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
17619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
17719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
17819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
17919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
18019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *buf)
18119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
18219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
18319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
18419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
18519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
18619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
18719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
18819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
18919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
19019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_read_blk(data->real, block, count, buf);
19119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->read_blk)
19219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->read_blk(block, count, retval);
19319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
19419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: read_blk(%lu, %d) returned %s\n",
19519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       block, count, retval ? error_message(retval) : "OK");
19619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
19719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
19819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
19919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
20019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, const void *buf)
20119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
20219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
20319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
20419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
20619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
20719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
20819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
21019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_write_blk(data->real, block, count, buf);
21119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->write_blk)
21219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->write_blk(block, count, retval);
21319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	else
21419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		printf("Test_io: write_blk(%lu, %d) returned %s\n",
21519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		       block, count, retval ? error_message(retval) : "OK");
21619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
21719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
21819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
21919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
22019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Flush data buffers to disk.
22119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
22219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel)
22319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
22419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
22519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
22619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
22719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
22819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
22919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
23019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
23119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return io_channel_flush(data->real);
23219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
23319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
23419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
235