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);
59 static errcode_t test_set_option(io_channel channel, const char *option,
62 static struct struct_io_manager struct_test_manager = {
63 EXT2_ET_MAGIC_IO_MANAGER,
75 io_manager test_io_manager = &struct_test_manager;
78 * These global variable can be set by the test program as
79 * necessary *before* calling test_open
81 io_manager test_io_backing_manager = 0;
82 void (*test_io_cb_read_blk)
83 (unsigned long block, int count, errcode_t err) = 0;
84 void (*test_io_cb_write_blk)
85 (unsigned long block, int count, errcode_t err) = 0;
86 void (*test_io_cb_set_blksize)
87 (int blksize, errcode_t err) = 0;
88 void (*test_io_cb_write_byte)
89 (unsigned long block, int count, errcode_t err) = 0;
94 #define TEST_FLAG_READ 0x01
95 #define TEST_FLAG_WRITE 0x02
96 #define TEST_FLAG_SET_BLKSIZE 0x04
97 #define TEST_FLAG_FLUSH 0x08
98 #define TEST_FLAG_DUMP 0x10
99 #define TEST_FLAG_SET_OPTION 0x20
101 static void test_dump_block(io_channel channel,
102 struct test_private_data *data,
103 unsigned long block, const void *buf)
105 const unsigned char *cp;
106 FILE *f = data->outfile;
108 unsigned long cksum = 0;
110 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
113 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
114 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
116 fprintf(f, "%04x: ", i);
117 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
121 static void test_abort(io_channel channel, unsigned long block)
123 struct test_private_data *data;
126 data = (struct test_private_data *) channel->private_data;
130 fprintf(f, "Aborting due to I/O to block %lu\n", block);
135 static errcode_t test_open(const char *name, int flags, io_channel *channel)
137 io_channel io = NULL;
138 struct test_private_data *data = NULL;
143 return EXT2_ET_BAD_DEVICE_NAME;
144 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
147 memset(io, 0, sizeof(struct struct_io_channel));
148 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
149 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
151 retval = EXT2_ET_NO_MEMORY;
154 io->manager = test_io_manager;
155 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
159 strcpy(io->name, name);
160 io->private_data = data;
161 io->block_size = 1024;
166 memset(data, 0, sizeof(struct test_private_data));
167 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
168 if (test_io_backing_manager) {
169 retval = test_io_backing_manager->open(name, flags,
175 data->read_blk = test_io_cb_read_blk;
176 data->write_blk = test_io_cb_write_blk;
177 data->set_blksize = test_io_cb_set_blksize;
178 data->write_byte = test_io_cb_write_byte;
180 data->outfile = NULL;
181 if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
182 data->outfile = fopen(value, "w");
184 data->outfile = stderr;
187 if ((value = getenv("TEST_IO_FLAGS")) != NULL)
188 data->flags = strtoul(value, NULL, 0);
191 if ((value = getenv("TEST_IO_BLOCK")) != NULL)
192 data->block = strtoul(value, NULL, 0);
194 data->read_abort_count = 0;
195 if ((value = getenv("TEST_IO_READ_ABORT")) != NULL)
196 data->read_abort_count = strtoul(value, NULL, 0);
198 data->write_abort_count = 0;
199 if ((value = getenv("TEST_IO_WRITE_ABORT")) != NULL)
200 data->write_abort_count = strtoul(value, NULL, 0);
207 ext2fs_free_mem(&io);
209 ext2fs_free_mem(&data);
213 static errcode_t test_close(io_channel channel)
215 struct test_private_data *data;
216 errcode_t retval = 0;
218 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
219 data = (struct test_private_data *) channel->private_data;
220 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
222 if (--channel->refcount > 0)
226 retval = io_channel_close(data->real);
228 if (data->outfile && data->outfile != stderr)
229 fclose(data->outfile);
231 ext2fs_free_mem(&channel->private_data);
233 ext2fs_free_mem(&channel->name);
234 ext2fs_free_mem(&channel);
238 static errcode_t test_set_blksize(io_channel channel, int blksize)
240 struct test_private_data *data;
241 errcode_t retval = 0;
243 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
244 data = (struct test_private_data *) channel->private_data;
245 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
248 retval = io_channel_set_blksize(data->real, blksize);
249 if (data->set_blksize)
250 data->set_blksize(blksize, retval);
251 if (data->flags & TEST_FLAG_SET_BLKSIZE)
252 fprintf(data->outfile,
253 "Test_io: set_blksize(%d) returned %s\n",
254 blksize, retval ? error_message(retval) : "OK");
255 channel->block_size = blksize;
260 static errcode_t test_read_blk(io_channel channel, unsigned long block,
261 int count, void *buf)
263 struct test_private_data *data;
264 errcode_t retval = 0;
266 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
267 data = (struct test_private_data *) channel->private_data;
268 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
271 retval = io_channel_read_blk(data->real, block, count, buf);
273 data->read_blk(block, count, retval);
274 if (data->flags & TEST_FLAG_READ)
275 fprintf(data->outfile,
276 "Test_io: read_blk(%lu, %d) returned %s\n",
277 block, count, retval ? error_message(retval) : "OK");
278 if (data->block && data->block == block) {
279 if (data->flags & TEST_FLAG_DUMP)
280 test_dump_block(channel, data, block, buf);
281 if (--data->read_abort_count == 0)
282 test_abort(channel, block);
287 static errcode_t test_write_blk(io_channel channel, unsigned long block,
288 int count, const void *buf)
290 struct test_private_data *data;
291 errcode_t retval = 0;
293 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
294 data = (struct test_private_data *) channel->private_data;
295 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
298 retval = io_channel_write_blk(data->real, block, count, buf);
300 data->write_blk(block, count, retval);
301 if (data->flags & TEST_FLAG_WRITE)
302 fprintf(data->outfile,
303 "Test_io: write_blk(%lu, %d) returned %s\n",
304 block, count, retval ? error_message(retval) : "OK");
305 if (data->block && data->block == block) {
306 if (data->flags & TEST_FLAG_DUMP)
307 test_dump_block(channel, data, block, buf);
308 if (--data->write_abort_count == 0)
309 test_abort(channel, block);
314 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
315 int count, const void *buf)
317 struct test_private_data *data;
318 errcode_t retval = 0;
320 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
321 data = (struct test_private_data *) channel->private_data;
322 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
324 if (data->real && data->real->manager->write_byte)
325 retval = io_channel_write_byte(data->real, offset, count, buf);
326 if (data->write_byte)
327 data->write_byte(offset, count, retval);
328 if (data->flags & TEST_FLAG_WRITE)
329 fprintf(data->outfile,
330 "Test_io: write_byte(%lu, %d) returned %s\n",
331 offset, count, retval ? error_message(retval) : "OK");
336 * Flush data buffers to disk.
338 static errcode_t test_flush(io_channel channel)
340 struct test_private_data *data;
341 errcode_t retval = 0;
343 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
344 data = (struct test_private_data *) channel->private_data;
345 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
348 retval = io_channel_flush(data->real);
350 if (data->flags & TEST_FLAG_FLUSH)
351 fprintf(data->outfile, "Test_io: flush() returned %s\n",
352 retval ? error_message(retval) : "OK");
357 static errcode_t test_set_option(io_channel channel, const char *option,
360 struct test_private_data *data;
361 errcode_t retval = 0;
363 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
364 data = (struct test_private_data *) channel->private_data;
365 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
368 if (data->flags & TEST_FLAG_SET_OPTION)
369 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
371 if (data->real && data->real->manager->set_option) {
372 retval = (data->real->manager->set_option)(data->real,
374 if (data->flags & TEST_FLAG_SET_OPTION)
375 fprintf(data->outfile, "returned %s\n",
376 retval ? error_message(retval) : "OK");
378 if (data->flags & TEST_FLAG_SET_OPTION)
379 fprintf(data->outfile, "not implemented\n");