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%
7543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * This file may be redistributed under the terms of the GNU Library
8543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * General Public License, version 2.
919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
1019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
1119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
12e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#if HAVE_SECURE_GETENV
13e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#define _GNU_SOURCE
14e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
15e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#if HAVE_SECURE_GETENV
16e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#define _GNU_SOURCE
17e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
1819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <stdio.h>
1919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
204cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
2119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <unistd.h>
224cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
2319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <fcntl.h>
2419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <time.h>
251d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_STAT_H
2619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/stat.h>
271d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
281d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_TYPES_H
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <sys/types.h>
301d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
31762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#ifdef HAVE_SYS_PRCTL_H
32762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#include <sys/prctl.h>
33762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#else
34762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#define PR_GET_DUMPABLE 3
35762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#endif
36762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#if (!defined(HAVE_PRCTL) && defined(linux))
37762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#include <sys/syscall.h>
38762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#endif
3919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
40b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
417b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o#include "ext2fs.h"
4219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
4319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
4419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * For checking structure magic numbers...
4519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
4619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
4719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#define EXT2_CHECK_MAGIC(struct, code) \
4819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	  if ((struct)->magic != (code)) return (code)
49efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
5019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostruct test_private_data {
5119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	int	magic;
5219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel real;
532a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	int flags;
542a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	FILE *outfile;
552a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	unsigned long block;
56a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	int read_abort_count, write_abort_count;
5719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*read_blk)(unsigned long block, int count, errcode_t err);
5819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*write_blk)(unsigned long block, int count, errcode_t err);
5919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	void (*set_blksize)(int blksize, errcode_t err);
60c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	void (*write_byte)(unsigned long block, int count, errcode_t err);
61c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	void (*read_blk64)(unsigned long long block, int count, errcode_t err);
62c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	void (*write_blk64)(unsigned long long block, int count, errcode_t err);
6319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
6419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
6519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel);
6619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel);
6719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize);
6819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
6919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *data);
7019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
7119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o				int count, const void *data);
72c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ostatic errcode_t test_read_blk64(io_channel channel, unsigned long long block,
73c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			       int count, void *data);
74c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ostatic errcode_t test_write_blk64(io_channel channel, unsigned long long block,
75c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o				int count, const void *data);
7619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel);
7731dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'ostatic errcode_t test_write_byte(io_channel channel, unsigned long offset,
7831dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'o				 int count, const void *buf);
79efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'ostatic errcode_t test_set_option(io_channel channel, const char *option,
802e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				 const char *arg);
816d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'ostatic errcode_t test_get_stats(io_channel channel, io_stats *stats);
82e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallstatic errcode_t test_discard(io_channel channel, unsigned long long block,
83e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			      unsigned long long count);
8419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
8519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic struct struct_io_manager struct_test_manager = {
8619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_ET_MAGIC_IO_MANAGER,
8719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	"Test I/O Manager",
8819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_open,
8919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_close,
9019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_set_blksize,
9119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_read_blk,
9219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	test_write_blk,
9331dbecd482405e0d3a67eb58e1a1c8cb9f2ad83eTheodore Ts'o	test_flush,
942e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	test_write_byte,
956d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	test_set_option,
966d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	test_get_stats,
97c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	test_read_blk64,
98c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	test_write_blk64,
99e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	test_discard,
10019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o};
10119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
10219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_manager = &struct_test_manager;
10319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
10419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
10519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * These global variable can be set by the test program as
10619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * necessary *before* calling test_open
10719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
10819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'oio_manager test_io_backing_manager = 0;
10919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_read_blk)
11019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
11119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_write_blk)
11219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
113c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ovoid (*test_io_cb_read_blk64)
114c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	(unsigned long long block, int count, errcode_t err) = 0;
115c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ovoid (*test_io_cb_write_blk64)
116c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	(unsigned long long block, int count, errcode_t err) = 0;
11719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ovoid (*test_io_cb_set_blksize)
11819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	(int blksize, errcode_t err) = 0;
119c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'ovoid (*test_io_cb_write_byte)
120c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	(unsigned long block, int count, errcode_t err) = 0;
12119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
1222a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o/*
1232a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o * Test flags
1242a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o */
1252a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_READ			0x01
1262a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_WRITE			0x02
1272a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_SET_BLKSIZE		0x04
1282a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o#define TEST_FLAG_FLUSH			0x08
129a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o#define TEST_FLAG_DUMP			0x10
1302e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o#define TEST_FLAG_SET_OPTION		0x20
131e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#define TEST_FLAG_DISCARD		0x40
1322a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1332a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'ostatic void test_dump_block(io_channel channel,
1342a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			    struct test_private_data *data,
13548e6e81362f264aee4f3945c14928efaf71a06c9Theodore Ts'o			    unsigned long block, const void *buf)
1362a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o{
1372a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	const unsigned char *cp;
1382a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	FILE *f = data->outfile;
1392a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	int	i;
1402a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	unsigned long	cksum = 0;
1412a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
1422a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
1432a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		cksum += *cp;
1442a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	}
14548e6e81362f264aee4f3945c14928efaf71a06c9Theodore Ts'o	fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
1462a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
1472a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		if ((i % 16) == 0)
1482a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			fprintf(f, "%04x: ", i);
1492a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
1502a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	}
1512a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o}
1522a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
153a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'ostatic void test_abort(io_channel channel, unsigned long block)
154a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o{
155a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	struct test_private_data *data;
156a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	FILE *f;
157a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
158a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
159a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	f = data->outfile;
160a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	test_flush(channel);
161a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
162a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	fprintf(f, "Aborting due to I/O to block %lu\n", block);
163a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	fflush(f);
164a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	abort();
165a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o}
166a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
167762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'ostatic char *safe_getenv(const char *arg)
168762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o{
16909f3eba28a4d3c050a548eab571b73352b131021Theodore Ts'o	if ((getuid() != geteuid()) || (getgid() != getegid()))
170762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o		return NULL;
171762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#if HAVE_PRCTL
172583d1f83284ddfe126d97e6db0020739a1609b5aTheodore Ts'o	if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
173762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o		return NULL;
174762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#else
175762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#if (defined(linux) && defined(SYS_prctl))
176583d1f83284ddfe126d97e6db0020739a1609b5aTheodore Ts'o	if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
177762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o		return NULL;
178762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#endif
179762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#endif
180762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o
181e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#if defined(HAVE_SECURE_GETENV)
182e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return secure_getenv(arg);
183e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#elif defined(HAVE___SECURE_GETENV)
1843af0a45628c0984d298a885f8381498cdf866221Theodore Ts'o	return __secure_getenv(arg);
185762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#else
1863af0a45628c0984d298a885f8381498cdf866221Theodore Ts'o	return getenv(arg);
187762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o#endif
188762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o}
189762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o
19019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_open(const char *name, int flags, io_channel *channel)
19119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
19219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io_channel	io = NULL;
19319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data = NULL;
19419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval;
195a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	char		*value;
19619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
19719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (name == 0)
19819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		return EXT2_ET_BAD_DEVICE_NAME;
199c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
2007b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
201e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		goto cleanup;
20219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(io, 0, sizeof(struct struct_io_channel));
20319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
204c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
205e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (retval)
20619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
20719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->manager = test_io_manager;
208c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
2097b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o	if (retval)
21019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		goto cleanup;
2117b4e4534f9361b21d3fafdd88a58f133decee38cTheodore Ts'o
21219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	strcpy(io->name, name);
21319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->private_data = data;
21419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->block_size = 1024;
21519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->read_error = 0;
21619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	io->write_error = 0;
217a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	io->refcount = 1;
21819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
21919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	memset(data, 0, sizeof(struct test_private_data));
22019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
22119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (test_io_backing_manager) {
22219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = test_io_backing_manager->open(name, flags,
22319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o						       &data->real);
22419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		if (retval)
22519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			goto cleanup;
22619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	} else
22719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->real = 0;
22819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->read_blk = 	test_io_cb_read_blk;
22919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->write_blk = 	test_io_cb_write_blk;
23019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data->set_blksize = 	test_io_cb_set_blksize;
231c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	data->write_byte = 	test_io_cb_write_byte;
232c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	data->read_blk64 = 	test_io_cb_read_blk64;
233c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	data->write_blk64 = 	test_io_cb_write_blk64;
2342a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
2352a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->outfile = NULL;
236762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o	if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
237a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->outfile = fopen(value, "w");
2382a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (!data->outfile)
2392a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		data->outfile = stderr;
2402a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
2412a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->flags = 0;
242762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o	if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
243a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->flags = strtoul(value, NULL, 0);
244efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
2452a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	data->block = 0;
246762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o	if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
247a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->block = strtoul(value, NULL, 0);
248a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
249a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data->read_abort_count = 0;
250762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o	if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
251a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->read_abort_count = strtoul(value, NULL, 0);
252a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o
253a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	data->write_abort_count = 0;
254762c7c65103615d976beeb4c8e2d1d9a79c87d86Theodore Ts'o	if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
255a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		data->write_abort_count = strtoul(value, NULL, 0);
256efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
257e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (data->real)
258e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		io->align = data->real->align;
259e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
26019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	*channel = io;
26119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return 0;
26219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
26319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ocleanup:
26419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (io)
265c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&io);
26619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data)
267c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&data);
26819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
26919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
27019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
27119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_close(io_channel channel)
27219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
27319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
27419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
27519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
27619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
27719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
27819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
27919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
280a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o	if (--channel->refcount > 0)
281a29f4d30f24d68f1f1c75548e020689ede532c05Theodore Ts'o		return 0;
282efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
28319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
28419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_close(data->real);
2852a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o
286c62dbdb9ec3244c8b324d8dbeb9ee14baac361f5Theodore Ts'o	if (data->outfile && data->outfile != stderr)
2872a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fclose(data->outfile);
288efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
289c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	ext2fs_free_mem(&channel->private_data);
29019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (channel->name)
291c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o		ext2fs_free_mem(&channel->name);
292c4e3d3f374b409500e3dd05c0b0eca6ac98a6b4eTheodore Ts'o	ext2fs_free_mem(&channel);
29319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
29419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
29519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
29619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_set_blksize(io_channel channel, int blksize)
29719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
29819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
29919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
30019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
30119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
30219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
30319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
30419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
305e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (data->real) {
30619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_set_blksize(data->real, blksize);
307e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		channel->align = data->real->align;
308e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	}
30919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->set_blksize)
31019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->set_blksize(blksize, retval);
3112a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_SET_BLKSIZE)
3122a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
3132a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: set_blksize(%d) returned %s\n",
3142a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			blksize, retval ? error_message(retval) : "OK");
3152a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	channel->block_size = blksize;
31619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
31719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
31819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
31919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
32019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_read_blk(io_channel channel, unsigned long block,
32119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, void *buf)
32219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
32319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
32419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
32519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
32619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
32719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
32819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
32919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
33019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
33119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_read_blk(data->real, block, count, buf);
33219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->read_blk)
33319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		data->read_blk(block, count, retval);
3342a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_READ)
3352a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
3362a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: read_blk(%lu, %d) returned %s\n",
3372a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
338a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if (data->block && data->block == block) {
339a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
340a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_dump_block(channel, data, block, buf);
341a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (--data->read_abort_count == 0)
342a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_abort(channel, block);
343efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	}
34419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
34519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
34619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
34719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_write_blk(io_channel channel, unsigned long block,
34819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o			       int count, const void *buf)
34919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
35019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
35119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	errcode_t	retval = 0;
35219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
35319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
35419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
35519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
35619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
35719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
35819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o		retval = io_channel_write_blk(data->real, block, count, buf);
359f20d0d57f73f7eb7fc801b85921cff4a3d90e385Theodore Ts'o	if (data->write_blk)
360f20d0d57f73f7eb7fc801b85921cff4a3d90e385Theodore Ts'o		data->write_blk(block, count, retval);
3612a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_WRITE)
3622a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
3632a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: write_blk(%lu, %d) returned %s\n",
3642a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
365a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	if (data->block && data->block == block) {
366a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
367a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_dump_block(channel, data, block, buf);
368a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o		if (--data->write_abort_count == 0)
369a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o			test_abort(channel, block);
370a002e7e200308eb010f55f19b673a53775a7cccdTheodore Ts'o	}
37119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	return retval;
37219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
37319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
374c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ostatic errcode_t test_read_blk64(io_channel channel, unsigned long long block,
375c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			       int count, void *buf)
376c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o{
377c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	struct test_private_data *data;
378c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	errcode_t	retval = 0;
379c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
380c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
381c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
382c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
383c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
384c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->real)
385c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		retval = io_channel_read_blk64(data->real, block, count, buf);
386c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->read_blk64)
387c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		data->read_blk64(block, count, retval);
388c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->flags & TEST_FLAG_READ)
389c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		fprintf(data->outfile,
390c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			"Test_io: read_blk64(%llu, %d) returned %s\n",
391c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
392c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->block && data->block == block) {
393c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
394c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			test_dump_block(channel, data, block, buf);
395c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		if (--data->read_abort_count == 0)
396c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			test_abort(channel, block);
397efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	}
398c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	return retval;
399c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o}
400c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
401c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'ostatic errcode_t test_write_blk64(io_channel channel, unsigned long long block,
402c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			       int count, const void *buf)
403c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o{
404c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	struct test_private_data *data;
405c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	errcode_t	retval = 0;
406c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
407c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
408c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
409c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
410c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
411c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->real)
412c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		retval = io_channel_write_blk64(data->real, block, count, buf);
413c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->write_blk64)
414c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		data->write_blk64(block, count, retval);
415c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->flags & TEST_FLAG_WRITE)
416c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		fprintf(data->outfile,
417c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			"Test_io: write_blk64(%llu, %d) returned %s\n",
418c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			block, count, retval ? error_message(retval) : "OK");
419c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	if (data->block && data->block == block) {
420c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		if (data->flags & TEST_FLAG_DUMP)
421c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			test_dump_block(channel, data, block, buf);
422c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o		if (--data->write_abort_count == 0)
423c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o			test_abort(channel, block);
424c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	}
425c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o	return retval;
426c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o}
427c7015e491fbd1aefde15faf9b30d086d9fc9d35eTheodore Ts'o
428c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'ostatic errcode_t test_write_byte(io_channel channel, unsigned long offset,
429c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o			       int count, const void *buf)
430c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o{
431c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	struct test_private_data *data;
432c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	errcode_t	retval = 0;
433c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
434c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
435c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	data = (struct test_private_data *) channel->private_data;
436c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
437c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
438c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	if (data->real && data->real->manager->write_byte)
439c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o		retval = io_channel_write_byte(data->real, offset, count, buf);
440c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	if (data->write_byte)
441c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o		data->write_byte(offset, count, retval);
4422a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_WRITE)
4432a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile,
4442a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			"Test_io: write_byte(%lu, %d) returned %s\n",
4452a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			offset, count, retval ? error_message(retval) : "OK");
446c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o	return retval;
447c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o}
448c180ac86533bcbfb1560bd4aa01464785a760f70Theodore Ts'o
44919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/*
45019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Flush data buffers to disk.
45119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */
45219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'ostatic errcode_t test_flush(io_channel channel)
45319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o{
45419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	struct test_private_data *data;
4559abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o	errcode_t	retval = 0;
456efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
45719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
45819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
45919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
46019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
46119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o	if (data->real)
4629abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o		retval = io_channel_flush(data->real);
463efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
4642a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o	if (data->flags & TEST_FLAG_FLUSH)
4652a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o		fprintf(data->outfile, "Test_io: flush() returned %s\n",
4662a29f1354f793557fa1829c9e85e74586e2907dbTheodore Ts'o			retval ? error_message(retval) : "OK");
467efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
4689abd2ce914f9373fb676f0bb620ffba3a0e3c49eTheodore Ts'o	return retval;
46919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o}
47019c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o
471efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'ostatic errcode_t test_set_option(io_channel channel, const char *option,
4722e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				 const char *arg)
4732e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o{
4742e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	struct test_private_data *data;
4752e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	errcode_t	retval = 0;
4762e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
4772e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
4782e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	data = (struct test_private_data *) channel->private_data;
4792e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
4802e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
4812e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o
4822e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	if (data->flags & TEST_FLAG_SET_OPTION)
483efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
4842e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			option, arg);
4852e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	if (data->real && data->real->manager->set_option) {
486efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o		retval = (data->real->manager->set_option)(data->real,
4872e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o							   option, arg);
4882e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		if (data->flags & TEST_FLAG_SET_OPTION)
4892e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			fprintf(data->outfile, "returned %s\n",
4902e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o				retval ? error_message(retval) : "OK");
4912e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	} else {
4922e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o		if (data->flags & TEST_FLAG_SET_OPTION)
4932e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o			fprintf(data->outfile, "not implemented\n");
4942e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	}
4952e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o	return retval;
4962e8ca9a26b0bd7dae546a3f9a98df67b043fe3beTheodore Ts'o}
4976d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o
4986d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'ostatic errcode_t test_get_stats(io_channel channel, io_stats *stats)
4996d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o{
5006d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	struct test_private_data *data;
5016d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	errcode_t	retval = 0;
5026d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o
5036d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
5046d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	data = (struct test_private_data *) channel->private_data;
5056d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
5066d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o
5076d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	if (data->real && data->real->manager->get_stats) {
5086d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o		retval = (data->real->manager->get_stats)(data->real, stats);
5096d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	}
5106d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o	return retval;
5116d96b00d57d236e2746f8245df6c8ea64abc64c1Theodore Ts'o}
512e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
513e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallstatic errcode_t test_discard(io_channel channel, unsigned long long block,
514e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			      unsigned long long count)
515e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
516e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	struct test_private_data *data;
517e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	errcode_t	retval = 0;
518e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
519e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
520e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	data = (struct test_private_data *) channel->private_data;
521e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
522e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
523e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (data->real)
524e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		retval = io_channel_discard(data->real, block, count);
525e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (data->flags & TEST_FLAG_DISCARD)
526e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		fprintf(data->outfile,
527e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			"Test_io: discard(%llu, %llu) returned %s\n",
528e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			block, count, retval ? error_message(retval) : "OK");
529e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return retval;
530e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
531