test_io.c revision 2e8ca9a26b0bd7dae546a3f9a98df67b043fe3be
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 <fcntl.h>
1819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <time.h>
191d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_STAT_H
2019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/stat.h>
211d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
221d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_TYPES_H
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/types.h>
241d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
2519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
26b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
277b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o#include "ext2fs.h"
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
3019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * For checking structure magic numbers...
3119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#define EXT2_CHECK_MAGIC(struct, code) \
3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	  if ((struct)->magic != (code)) return (code)
3519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostruct test_private_data {
3719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int	magic;
3819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel real;
392a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	int flags;
402a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	FILE *outfile;
412a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	unsigned long block;
42a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	int read_abort_count, write_abort_count;
4319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*read_blk)(unsigned long block, int count, errcode_t err);
4419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*write_blk)(unsigned long block, int count, errcode_t err);
4519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*set_blksize)(int blksize, errcode_t err);
46c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	void (*write_byte)(unsigned long block, int count, errcode_t err);
4719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
4819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
4919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel);
5019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel);
5119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize);
5219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
5319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *data);
5419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
5519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o				int count, const void *data);
5619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel);
5731dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'ostatic errcode_t test_write_byte(io_channel channel, unsigned long offset,
5831dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'o				 int count, const void *buf);
592e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'ostatic errcode_t test_set_option(io_channel channel, const char *option,
602e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				 const char *arg);
6119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic struct struct_io_manager struct_test_manager = {
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_ET_MAGIC_IO_MANAGER,
6419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	"Test I/O Manager",
6519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_open,
6619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_close,
6719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_set_blksize,
6819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_read_blk,
6919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_write_blk,
7031dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'o	test_flush,
712e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	test_write_byte,
722e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	test_set_option
7319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
7419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
7519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_manager = &struct_test_manager;
7619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
7719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
7819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * These global variable can be set by the test program as
7919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * necessary *before* calling test_open
8019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
8119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_backing_manager = 0;
8219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_read_blk)
8319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
8419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_write_blk)
8519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
8619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_set_blksize)
8719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(int blksize, errcode_t err) = 0;
88c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'ovoid (*test_io_cb_write_byte)
89c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
9019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
912a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o/*
922a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o * Test flags
932a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o */
942a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_READ			0x01
952a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_WRITE			0x02
962a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_SET_BLKSIZE		0x04
972a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_FLUSH			0x08
98a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o#define TEST_FLAG_DUMP			0x10
992e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o#define TEST_FLAG_SET_OPTION		0x20
1002a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1012a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'ostatic void test_dump_block(io_channel channel,
1022a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			    struct test_private_data *data,
10348e6e81362f264aee4f3945c14928efaf71a06c9Theodore Ts'o			    unsigned long block, const void *buf)
1042a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o{
1052a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	const unsigned char *cp;
1062a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	FILE *f = data->outfile;
1072a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	int	i;
1082a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	unsigned long	cksum = 0;
1092a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1102a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
1112a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		cksum += *cp;
1122a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	}
11348e6e81362f264aee4f3945c14928efaf71a06c9Theodore Ts'o	fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
1142a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
1152a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		if ((i % 16) == 0)
1162a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			fprintf(f, "%04x: ", i);
1172a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
1182a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	}
1192a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o}
1202a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
121a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'ostatic void test_abort(io_channel channel, unsigned long block)
122a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o{
123a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	struct test_private_data *data;
124a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	FILE *f;
125a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
126a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
127a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	f = data->outfile;
128a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	test_flush(channel);
129a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
130a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	fprintf(f, "Aborting due to I/O to block %lu\n", block);
131a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	fflush(f);
132a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	abort();
133a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o}
134a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
13519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel)
13619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
13719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel	io = NULL;
13819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data = NULL;
13919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval;
140a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	char		*value;
14119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
14219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (name == 0)
14319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return EXT2_ET_BAD_DEVICE_NAME;
144c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
1457b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
1467b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o		return retval;
14719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(io, 0, sizeof(struct struct_io_channel));
14819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
149c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
1507b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval) {
1511f0b6c1f895d189fea6999d0c07a7fee936a4baaTheodore Ts'o		retval = EXT2_ET_NO_MEMORY;
15219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
15319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	}
15419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->manager = test_io_manager;
155c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
1567b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
15719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
1587b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o
15919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	strcpy(io->name, name);
16019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->private_data = data;
16119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->block_size = 1024;
16219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->read_error = 0;
16319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->write_error = 0;
164a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	io->refcount = 1;
16519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
16619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(data, 0, sizeof(struct test_private_data));
16719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
16819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (test_io_backing_manager) {
16919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = test_io_backing_manager->open(name, flags,
17019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o						       &data->real);
17119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		if (retval)
17219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			goto cleanup;
17319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	} else
17419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->real = 0;
17519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->read_blk = 	test_io_cb_read_blk;
17619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->write_blk = 	test_io_cb_write_blk;
17719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->set_blksize = 	test_io_cb_set_blksize;
178c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	data->write_byte = 	test_io_cb_write_byte;
1792a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1802a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->outfile = NULL;
181a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
182a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->outfile = fopen(value, "w");
1832a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (!data->outfile)
1842a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		data->outfile = stderr;
1852a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1862a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->flags = 0;
187a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if ((value = getenv("TEST_IO_FLAGS")) != NULL)
188a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->flags = strtoul(value, NULL, 0);
1892a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1902a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->block = 0;
191a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if ((value = getenv("TEST_IO_BLOCK")) != NULL)
192a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->block = strtoul(value, NULL, 0);
193a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
194a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data->read_abort_count = 0;
195a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if ((value = getenv("TEST_IO_READ_ABORT")) != NULL)
196a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->read_abort_count = strtoul(value, NULL, 0);
197a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
198a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data->write_abort_count = 0;
199a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if ((value = getenv("TEST_IO_WRITE_ABORT")) != NULL)
200a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->write_abort_count = strtoul(value, NULL, 0);
20119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	*channel = io;
20319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
20419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
20519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ocleanup:
20619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (io)
207c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&io);
20819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data)
209c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&data);
21019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
21119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
21219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
21319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel)
21419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
21519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
21619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
21719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
21819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
21919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
22019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
22119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
222a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (--channel->refcount > 0)
223a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		return 0;
224a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o
22519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
22619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_close(data->real);
2272a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
228c62dbdb9ec3244c8b324d8dbeb9ee14baac361f5Theodore Ts'o	if (data->outfile && data->outfile != stderr)
2292a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fclose(data->outfile);
23019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
231c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	ext2fs_free_mem(&channel->private_data);
23219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->name)
233c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&channel->name);
234c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	ext2fs_free_mem(&channel);
23519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
23619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
23719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
23819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize)
23919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
24019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
24119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
24219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
24319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
24419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
24519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
24619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
24719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
24819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_set_blksize(data->real, blksize);
24919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->set_blksize)
25019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->set_blksize(blksize, retval);
2512a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_SET_BLKSIZE)
2522a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
2532a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: set_blksize(%d) returned %s\n",
2542a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			blksize, retval ? error_message(retval) : "OK");
2552a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	channel->block_size = blksize;
25619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
25719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
25819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
25919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
26019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
26119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *buf)
26219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
26319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
26419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
26519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
26619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
26719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
26819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
26919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
27019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
27119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_read_blk(data->real, block, count, buf);
27219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->read_blk)
27319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->read_blk(block, count, retval);
2742a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_READ)
2752a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
2762a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: read_blk(%lu, %d) returned %s\n",
2772a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
278a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if (data->block && data->block == block) {
279a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
280a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_dump_block(channel, data, block, buf);
281a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (--data->read_abort_count == 0)
282a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_abort(channel, block);
283a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	}
28419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
28519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
28619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
28719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
28819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, const void *buf)
28919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
29019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
29119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
29219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
29319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
29419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
29519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
29619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
29719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
29819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_write_blk(data->real, block, count, buf);
299f20d0d57f73f7eb7fc801b85921cff4a3d90e385Theodore Ts'o	if (data->write_blk)
300f20d0d57f73f7eb7fc801b85921cff4a3d90e385Theodore Ts'o		data->write_blk(block, count, retval);
3012a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_WRITE)
3022a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
3032a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: write_blk(%lu, %d) returned %s\n",
3042a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
305a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if (data->block && data->block == block) {
306a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
307a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_dump_block(channel, data, block, buf);
308a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (--data->write_abort_count == 0)
309a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_abort(channel, block);
310a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	}
31119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
31219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
31319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
314c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'ostatic errcode_t test_write_byte(io_channel channel, unsigned long offset,
315c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o			       int count, const void *buf)
316c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o{
317c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	struct test_private_data *data;
318c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	errcode_t	retval = 0;
319c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
320c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
321c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	data = (struct test_private_data *) channel->private_data;
322c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
323c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
324c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	if (data->real && data->real->manager->write_byte)
325c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o		retval = io_channel_write_byte(data->real, offset, count, buf);
326c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	if (data->write_byte)
327c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o		data->write_byte(offset, count, retval);
3282a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_WRITE)
3292a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
3302a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: write_byte(%lu, %d) returned %s\n",
3312a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			offset, count, retval ? error_message(retval) : "OK");
332c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	return retval;
333c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o}
334c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
33519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
33619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Flush data buffers to disk.
33719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
33819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel)
33919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
34019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
3419abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o	errcode_t	retval = 0;
34219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
34319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
34419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
34519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
34619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
34719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
3489abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o		retval = io_channel_flush(data->real);
3499abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o
3502a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_FLUSH)
3512a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile, "Test_io: flush() returned %s\n",
3522a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			retval ? error_message(retval) : "OK");
3539abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o
3549abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o	return retval;
35519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
35619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
3572e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'ostatic errcode_t test_set_option(io_channel channel, const char *option,
3582e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				 const char *arg)
3592e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o{
3602e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	struct test_private_data *data;
3612e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	errcode_t	retval = 0;
3622e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
3632e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
3642e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
3652e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
3662e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
3672e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
3682e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	if (data->flags & TEST_FLAG_SET_OPTION)
3692e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
3702e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			option, arg);
3712e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	if (data->real && data->real->manager->set_option) {
3722e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		retval = (data->real->manager->set_option)(data->real,
3732e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o							   option, arg);
3742e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		if (data->flags & TEST_FLAG_SET_OPTION)
3752e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			fprintf(data->outfile, "returned %s\n",
3762e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				retval ? error_message(retval) : "OK");
3772e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	} else {
3782e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		if (data->flags & TEST_FLAG_SET_OPTION)
3792e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			fprintf(data->outfile, "not implemented\n");
3802e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	}
3812e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	return retval;
3822e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o}
383