1 /* vi: set sw=4 ts=4: */
3 * test_io.c --- This is the Test I/O interface.
5 * Copyright (C) 1996 Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
24 #include <sys/types.h>
31 * For checking structure magic numbers...
34 #define EXT2_CHECK_MAGIC(struct, code) \
35 if ((struct)->magic != (code)) return (code)
37 struct test_private_data {
43 int read_abort_count, write_abort_count;
44 void (*read_blk)(unsigned long block, int count, errcode_t err);
45 void (*write_blk)(unsigned long block, int count, errcode_t err);
46 void (*set_blksize)(int blksize, errcode_t err);
47 void (*write_byte)(unsigned long block, int count, errcode_t err);
50 static errcode_t test_open(const char *name, int flags, io_channel *channel);
51 static errcode_t test_close(io_channel channel);
52 static errcode_t test_set_blksize(io_channel channel, int blksize);
53 static errcode_t test_read_blk(io_channel channel, unsigned long block,
54 int count, void *data);
55 static errcode_t test_write_blk(io_channel channel, unsigned long block,
56 int count, const void *data);
57 static errcode_t test_flush(io_channel channel);
58 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
59 int count, const void *buf);
60 static errcode_t test_set_option(io_channel channel, const char *option,
63 static struct struct_io_manager struct_test_manager = {
64 EXT2_ET_MAGIC_IO_MANAGER,
76 io_manager test_io_manager = &struct_test_manager;
79 * These global variable can be set by the test program as
80 * necessary *before* calling test_open
82 io_manager test_io_backing_manager = 0;
83 void (*test_io_cb_read_blk)
84 (unsigned long block, int count, errcode_t err) = 0;
85 void (*test_io_cb_write_blk)
86 (unsigned long block, int count, errcode_t err) = 0;
87 void (*test_io_cb_set_blksize)
88 (int blksize, errcode_t err) = 0;
89 void (*test_io_cb_write_byte)
90 (unsigned long block, int count, errcode_t err) = 0;
95 #define TEST_FLAG_READ 0x01
96 #define TEST_FLAG_WRITE 0x02
97 #define TEST_FLAG_SET_BLKSIZE 0x04
98 #define TEST_FLAG_FLUSH 0x08
99 #define TEST_FLAG_DUMP 0x10
100 #define TEST_FLAG_SET_OPTION 0x20
102 static void test_dump_block(io_channel channel,
103 struct test_private_data *data,
104 unsigned long block, const void *buf)
106 const unsigned char *cp;
107 FILE *f = data->outfile;
109 unsigned long cksum = 0;
111 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
114 fprintf(f, "Contents of block %lu, checksum %08lu:\n", block, cksum);
115 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
117 fprintf(f, "%04x: ", i);
118 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
122 static void test_abort(io_channel channel, unsigned long block)
124 struct test_private_data *data;
127 data = (struct test_private_data *) channel->private_data;
131 fprintf(f, "Aborting due to I/O to block %lu\n", block);
136 static errcode_t test_open(const char *name, int flags, io_channel *channel)
138 io_channel io = NULL;
139 struct test_private_data *data = NULL;
144 return EXT2_ET_BAD_DEVICE_NAME;
145 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
148 memset(io, 0, sizeof(struct struct_io_channel));
149 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
150 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
152 retval = EXT2_ET_NO_MEMORY;
155 io->manager = test_io_manager;
156 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
160 strcpy(io->name, name);
161 io->private_data = data;
162 io->block_size = 1024;
167 memset(data, 0, sizeof(struct test_private_data));
168 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
169 if (test_io_backing_manager) {
170 retval = test_io_backing_manager->open(name, flags,
176 data->read_blk = test_io_cb_read_blk;
177 data->write_blk = test_io_cb_write_blk;
178 data->set_blksize = test_io_cb_set_blksize;
179 data->write_byte = test_io_cb_write_byte;
181 data->outfile = NULL;
182 if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
183 data->outfile = fopen(value, "w");
185 data->outfile = stderr;
188 if ((value = getenv("TEST_IO_FLAGS")) != NULL)
189 data->flags = strtoul(value, NULL, 0);
192 if ((value = getenv("TEST_IO_BLOCK")) != NULL)
193 data->block = strtoul(value, NULL, 0);
195 data->read_abort_count = 0;
196 if ((value = getenv("TEST_IO_READ_ABORT")) != NULL)
197 data->read_abort_count = strtoul(value, NULL, 0);
199 data->write_abort_count = 0;
200 if ((value = getenv("TEST_IO_WRITE_ABORT")) != NULL)
201 data->write_abort_count = strtoul(value, NULL, 0);
207 ext2fs_free_mem(&io);
208 ext2fs_free_mem(&data);
212 static errcode_t test_close(io_channel channel)
214 struct test_private_data *data;
215 errcode_t retval = 0;
217 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
218 data = (struct test_private_data *) channel->private_data;
219 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
221 if (--channel->refcount > 0)
225 retval = io_channel_close(data->real);
227 if (data->outfile && data->outfile != stderr)
228 fclose(data->outfile);
230 ext2fs_free_mem(&channel->private_data);
231 ext2fs_free_mem(&channel->name);
232 ext2fs_free_mem(&channel);
236 static errcode_t test_set_blksize(io_channel channel, int blksize)
238 struct test_private_data *data;
239 errcode_t retval = 0;
241 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
242 data = (struct test_private_data *) channel->private_data;
243 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
246 retval = io_channel_set_blksize(data->real, blksize);
247 if (data->set_blksize)
248 data->set_blksize(blksize, retval);
249 if (data->flags & TEST_FLAG_SET_BLKSIZE)
250 fprintf(data->outfile,
251 "Test_io: set_blksize(%d) returned %s\n",
252 blksize, retval ? error_message(retval) : "OK");
253 channel->block_size = blksize;
258 static errcode_t test_read_blk(io_channel channel, unsigned long block,
259 int count, void *buf)
261 struct test_private_data *data;
262 errcode_t retval = 0;
264 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
265 data = (struct test_private_data *) channel->private_data;
266 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
269 retval = io_channel_read_blk(data->real, block, count, buf);
271 data->read_blk(block, count, retval);
272 if (data->flags & TEST_FLAG_READ)
273 fprintf(data->outfile,
274 "Test_io: read_blk(%lu, %d) returned %s\n",
275 block, count, retval ? error_message(retval) : "OK");
276 if (data->block && data->block == block) {
277 if (data->flags & TEST_FLAG_DUMP)
278 test_dump_block(channel, data, block, buf);
279 if (--data->read_abort_count == 0)
280 test_abort(channel, block);
285 static errcode_t test_write_blk(io_channel channel, unsigned long block,
286 int count, const void *buf)
288 struct test_private_data *data;
289 errcode_t retval = 0;
291 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
292 data = (struct test_private_data *) channel->private_data;
293 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
296 retval = io_channel_write_blk(data->real, block, count, buf);
298 data->write_blk(block, count, retval);
299 if (data->flags & TEST_FLAG_WRITE)
300 fprintf(data->outfile,
301 "Test_io: write_blk(%lu, %d) returned %s\n",
302 block, count, retval ? error_message(retval) : "OK");
303 if (data->block && data->block == block) {
304 if (data->flags & TEST_FLAG_DUMP)
305 test_dump_block(channel, data, block, buf);
306 if (--data->write_abort_count == 0)
307 test_abort(channel, block);
312 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
313 int count, const void *buf)
315 struct test_private_data *data;
316 errcode_t retval = 0;
318 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
319 data = (struct test_private_data *) channel->private_data;
320 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
322 if (data->real && data->real->manager->write_byte)
323 retval = io_channel_write_byte(data->real, offset, count, buf);
324 if (data->write_byte)
325 data->write_byte(offset, count, retval);
326 if (data->flags & TEST_FLAG_WRITE)
327 fprintf(data->outfile,
328 "Test_io: write_byte(%lu, %d) returned %s\n",
329 offset, count, retval ? error_message(retval) : "OK");
334 * Flush data buffers to disk.
336 static errcode_t test_flush(io_channel channel)
338 struct test_private_data *data;
339 errcode_t retval = 0;
341 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
342 data = (struct test_private_data *) channel->private_data;
343 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
346 retval = io_channel_flush(data->real);
348 if (data->flags & TEST_FLAG_FLUSH)
349 fprintf(data->outfile, "Test_io: flush() returned %s\n",
350 retval ? error_message(retval) : "OK");
355 static errcode_t test_set_option(io_channel channel, const char *option,
358 struct test_private_data *data;
359 errcode_t retval = 0;
361 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
362 data = (struct test_private_data *) channel->private_data;
363 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
366 if (data->flags & TEST_FLAG_SET_OPTION)
367 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
369 if (data->real && data->real->manager->set_option) {
370 retval = (data->real->manager->set_option)(data->real,
372 if (data->flags & TEST_FLAG_SET_OPTION)
373 fprintf(data->outfile, "returned %s\n",
374 retval ? error_message(retval) : "OK");
376 if (data->flags & TEST_FLAG_SET_OPTION)
377 fprintf(data->outfile, "not implemented\n");