2 * test_io.c --- This is the Test I/O interface.
4 * Copyright (C) 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
30 * For checking structure magic numbers...
33 #define EXT2_CHECK_MAGIC(struct, code) \
34 if ((struct)->magic != (code)) return (code)
36 struct test_private_data {
42 int read_abort_count, write_abort_count;
43 void (*read_blk)(unsigned long block, int count, errcode_t err);
44 void (*write_blk)(unsigned long block, int count, errcode_t err);
45 void (*set_blksize)(int blksize, errcode_t err);
46 void (*write_byte)(unsigned long block, int count, errcode_t err);
49 static errcode_t test_open(const char *name, int flags, io_channel *channel);
50 static errcode_t test_close(io_channel channel);
51 static errcode_t test_set_blksize(io_channel channel, int blksize);
52 static errcode_t test_read_blk(io_channel channel, unsigned long block,
53 int count, void *data);
54 static errcode_t test_write_blk(io_channel channel, unsigned long block,
55 int count, const void *data);
56 static errcode_t test_flush(io_channel channel);
57 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
58 int count, const void *buf);
60 static struct struct_io_manager struct_test_manager = {
61 EXT2_ET_MAGIC_IO_MANAGER,
73 io_manager test_io_manager = &struct_test_manager;
76 * These global variable can be set by the test program as
77 * necessary *before* calling test_open
79 io_manager test_io_backing_manager = 0;
80 void (*test_io_cb_read_blk)
81 (unsigned long block, int count, errcode_t err) = 0;
82 void (*test_io_cb_write_blk)
83 (unsigned long block, int count, errcode_t err) = 0;
84 void (*test_io_cb_set_blksize)
85 (int blksize, errcode_t err) = 0;
86 void (*test_io_cb_write_byte)
87 (unsigned long block, int count, errcode_t err) = 0;
92 #define TEST_FLAG_READ 0x01
93 #define TEST_FLAG_WRITE 0x02
94 #define TEST_FLAG_SET_BLKSIZE 0x04
95 #define TEST_FLAG_FLUSH 0x08
96 #define TEST_FLAG_DUMP 0x10
98 static void test_dump_block(io_channel channel,
99 struct test_private_data *data,
100 unsigned long block, const void *buf)
102 const unsigned char *cp;
103 FILE *f = data->outfile;
105 unsigned long cksum = 0;
107 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
110 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
111 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
113 fprintf(f, "%04x: ", i);
114 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
118 static void test_abort(io_channel channel, unsigned long block)
120 struct test_private_data *data;
123 data = (struct test_private_data *) channel->private_data;
127 fprintf(f, "Aborting due to I/O to block %lu\n", block);
132 static errcode_t test_open(const char *name, int flags, io_channel *channel)
134 io_channel io = NULL;
135 struct test_private_data *data = NULL;
140 return EXT2_ET_BAD_DEVICE_NAME;
141 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
144 memset(io, 0, sizeof(struct struct_io_channel));
145 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
146 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
148 retval = EXT2_ET_NO_MEMORY;
151 io->manager = test_io_manager;
152 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
156 strcpy(io->name, name);
157 io->private_data = data;
158 io->block_size = 1024;
163 memset(data, 0, sizeof(struct test_private_data));
164 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
165 if (test_io_backing_manager) {
166 retval = test_io_backing_manager->open(name, flags,
172 data->read_blk = test_io_cb_read_blk;
173 data->write_blk = test_io_cb_write_blk;
174 data->set_blksize = test_io_cb_set_blksize;
175 data->write_byte = test_io_cb_write_byte;
177 data->outfile = NULL;
178 if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
179 data->outfile = fopen(value, "w");
181 data->outfile = stderr;
184 if ((value = getenv("TEST_IO_FLAGS")) != NULL)
185 data->flags = strtoul(value, NULL, 0);
188 if ((value = getenv("TEST_IO_BLOCK")) != NULL)
189 data->block = strtoul(value, NULL, 0);
191 data->read_abort_count = 0;
192 if ((value = getenv("TEST_IO_READ_ABORT")) != NULL)
193 data->read_abort_count = strtoul(value, NULL, 0);
195 data->write_abort_count = 0;
196 if ((value = getenv("TEST_IO_WRITE_ABORT")) != NULL)
197 data->write_abort_count = strtoul(value, NULL, 0);
204 ext2fs_free_mem(&io);
206 ext2fs_free_mem(&data);
210 static errcode_t test_close(io_channel channel)
212 struct test_private_data *data;
213 errcode_t retval = 0;
215 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
216 data = (struct test_private_data *) channel->private_data;
217 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
219 if (--channel->refcount > 0)
223 retval = io_channel_close(data->real);
225 if (data->outfile && data->outfile != stderr)
226 fclose(data->outfile);
228 ext2fs_free_mem(&channel->private_data);
230 ext2fs_free_mem(&channel->name);
231 ext2fs_free_mem(&channel);
235 static errcode_t test_set_blksize(io_channel channel, int blksize)
237 struct test_private_data *data;
238 errcode_t retval = 0;
240 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
241 data = (struct test_private_data *) channel->private_data;
242 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
245 retval = io_channel_set_blksize(data->real, blksize);
246 if (data->set_blksize)
247 data->set_blksize(blksize, retval);
248 if (data->flags & TEST_FLAG_SET_BLKSIZE)
249 fprintf(data->outfile,
250 "Test_io: set_blksize(%d) returned %s\n",
251 blksize, retval ? error_message(retval) : "OK");
252 channel->block_size = blksize;
257 static errcode_t test_read_blk(io_channel channel, unsigned long block,
258 int count, void *buf)
260 struct test_private_data *data;
261 errcode_t retval = 0;
263 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
264 data = (struct test_private_data *) channel->private_data;
265 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
268 retval = io_channel_read_blk(data->real, block, count, buf);
270 data->read_blk(block, count, retval);
271 if (data->flags & TEST_FLAG_READ)
272 fprintf(data->outfile,
273 "Test_io: read_blk(%lu, %d) returned %s\n",
274 block, count, retval ? error_message(retval) : "OK");
275 if (data->block && data->block == block) {
276 if (data->flags & TEST_FLAG_DUMP)
277 test_dump_block(channel, data, block, buf);
278 if (--data->read_abort_count == 0)
279 test_abort(channel, block);
284 static errcode_t test_write_blk(io_channel channel, unsigned long block,
285 int count, const void *buf)
287 struct test_private_data *data;
288 errcode_t retval = 0;
290 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
291 data = (struct test_private_data *) channel->private_data;
292 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
295 retval = io_channel_write_blk(data->real, block, count, buf);
297 data->write_blk(block, count, retval);
298 if (data->flags & TEST_FLAG_WRITE)
299 fprintf(data->outfile,
300 "Test_io: write_blk(%lu, %d) returned %s\n",
301 block, count, retval ? error_message(retval) : "OK");
302 if (data->block && data->block == block) {
303 if (data->flags & TEST_FLAG_DUMP)
304 test_dump_block(channel, data, block, buf);
305 if (--data->write_abort_count == 0)
306 test_abort(channel, block);
311 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
312 int count, const void *buf)
314 struct test_private_data *data;
315 errcode_t retval = 0;
317 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
318 data = (struct test_private_data *) channel->private_data;
319 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
321 if (data->real && data->real->manager->write_byte)
322 retval = io_channel_write_byte(data->real, offset, count, buf);
323 if (data->write_byte)
324 data->write_byte(offset, count, retval);
325 if (data->flags & TEST_FLAG_WRITE)
326 fprintf(data->outfile,
327 "Test_io: write_byte(%lu, %d) returned %s\n",
328 offset, count, retval ? error_message(retval) : "OK");
333 * Flush data buffers to disk.
335 static errcode_t test_flush(io_channel channel)
337 struct test_private_data *data;
338 errcode_t retval = 0;
340 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
341 data = (struct test_private_data *) channel->private_data;
342 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
345 retval = io_channel_flush(data->real);
347 if (data->flags & TEST_FLAG_FLUSH)
348 fprintf(data->outfile, "Test_io: flush() returned %s\n",
349 retval ? error_message(retval) : "OK");