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 Library
8 * General Public License, version 2.
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_PRCTL_H
27 #include <sys/prctl.h>
29 #define PR_GET_DUMPABLE 3
31 #if (!defined(HAVE_PRCTL) && defined(linux))
32 #include <sys/syscall.h>
39 * For checking structure magic numbers...
42 #define EXT2_CHECK_MAGIC(struct, code) \
43 if ((struct)->magic != (code)) return (code)
45 struct test_private_data {
51 int read_abort_count, write_abort_count;
52 void (*read_blk)(unsigned long block, int count, errcode_t err);
53 void (*write_blk)(unsigned long block, int count, errcode_t err);
54 void (*set_blksize)(int blksize, errcode_t err);
55 void (*write_byte)(unsigned long block, int count, errcode_t err);
56 void (*read_blk64)(unsigned long long block, int count, errcode_t err);
57 void (*write_blk64)(unsigned long long block, int count, errcode_t err);
61 * These global variable can be set by the test program as
62 * necessary *before* calling test_open
64 io_manager test_io_backing_manager = 0;
65 void (*test_io_cb_read_blk)
66 (unsigned long block, int count, errcode_t err) = 0;
67 void (*test_io_cb_write_blk)
68 (unsigned long block, int count, errcode_t err) = 0;
69 void (*test_io_cb_read_blk64)
70 (unsigned long long block, int count, errcode_t err) = 0;
71 void (*test_io_cb_write_blk64)
72 (unsigned long long block, int count, errcode_t err) = 0;
73 void (*test_io_cb_set_blksize)
74 (int blksize, errcode_t err) = 0;
75 void (*test_io_cb_write_byte)
76 (unsigned long block, int count, errcode_t err) = 0;
81 #define TEST_FLAG_READ 0x01
82 #define TEST_FLAG_WRITE 0x02
83 #define TEST_FLAG_SET_BLKSIZE 0x04
84 #define TEST_FLAG_FLUSH 0x08
85 #define TEST_FLAG_DUMP 0x10
86 #define TEST_FLAG_SET_OPTION 0x20
87 #define TEST_FLAG_DISCARD 0x40
88 #define TEST_FLAG_READAHEAD 0x80
90 static void test_dump_block(io_channel channel,
91 struct test_private_data *data,
92 unsigned long block, const void *buf)
94 const unsigned char *cp;
95 FILE *f = data->outfile;
97 unsigned long cksum = 0;
99 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
102 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
103 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
105 fprintf(f, "%04x: ", i);
106 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
111 * Flush data buffers to disk.
113 static errcode_t test_flush(io_channel channel)
115 struct test_private_data *data;
116 errcode_t retval = 0;
118 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
119 data = (struct test_private_data *)channel->private_data;
120 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
123 retval = io_channel_flush(data->real);
125 if (data->flags & TEST_FLAG_FLUSH)
126 fprintf(data->outfile, "Test_io: flush() returned %s\n",
127 retval ? error_message(retval) : "OK");
132 static void test_abort(io_channel channel, unsigned long block)
134 struct test_private_data *data;
137 data = (struct test_private_data *) channel->private_data;
141 fprintf(f, "Aborting due to I/O to block %lu\n", block);
146 static char *safe_getenv(const char *arg)
148 if ((getuid() != geteuid()) || (getgid() != getegid()))
151 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
154 #if (defined(linux) && defined(SYS_prctl))
155 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
160 #if defined(HAVE_SECURE_GETENV)
161 return secure_getenv(arg);
162 #elif defined(HAVE___SECURE_GETENV)
163 return __secure_getenv(arg);
169 static errcode_t test_open(const char *name, int flags, io_channel *channel)
171 io_channel io = NULL;
172 struct test_private_data *data = NULL;
177 return EXT2_ET_BAD_DEVICE_NAME;
178 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
181 memset(io, 0, sizeof(struct struct_io_channel));
182 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
183 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
186 io->manager = test_io_manager;
187 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
191 strcpy(io->name, name);
192 io->private_data = data;
193 io->block_size = 1024;
198 memset(data, 0, sizeof(struct test_private_data));
199 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
200 if (test_io_backing_manager) {
201 retval = test_io_backing_manager->open(name, flags,
208 data->read_blk = test_io_cb_read_blk;
209 data->write_blk = test_io_cb_write_blk;
210 data->set_blksize = test_io_cb_set_blksize;
211 data->write_byte = test_io_cb_write_byte;
212 data->read_blk64 = test_io_cb_read_blk64;
213 data->write_blk64 = test_io_cb_write_blk64;
215 data->outfile = NULL;
216 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
217 data->outfile = fopen(value, "w");
219 data->outfile = stderr;
222 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
223 data->flags = strtoul(value, NULL, 0);
226 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
227 data->block = strtoul(value, NULL, 0);
229 data->read_abort_count = 0;
230 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
231 data->read_abort_count = strtoul(value, NULL, 0);
233 data->write_abort_count = 0;
234 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
235 data->write_abort_count = strtoul(value, NULL, 0);
238 io->align = data->real->align;
245 ext2fs_free_mem(&io);
247 ext2fs_free_mem(&data);
251 static errcode_t test_close(io_channel channel)
253 struct test_private_data *data;
254 errcode_t retval = 0;
256 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
257 data = (struct test_private_data *) channel->private_data;
258 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
260 if (--channel->refcount > 0)
264 retval = io_channel_close(data->real);
266 if (data->outfile && data->outfile != stderr)
267 fclose(data->outfile);
269 ext2fs_free_mem(&channel->private_data);
271 ext2fs_free_mem(&channel->name);
272 ext2fs_free_mem(&channel);
276 static errcode_t test_set_blksize(io_channel channel, int blksize)
278 struct test_private_data *data;
279 errcode_t retval = 0;
281 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
282 data = (struct test_private_data *) channel->private_data;
283 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
286 retval = io_channel_set_blksize(data->real, blksize);
287 channel->align = data->real->align;
289 if (data->set_blksize)
290 data->set_blksize(blksize, retval);
291 if (data->flags & TEST_FLAG_SET_BLKSIZE)
292 fprintf(data->outfile,
293 "Test_io: set_blksize(%d) returned %s\n",
294 blksize, retval ? error_message(retval) : "OK");
295 channel->block_size = blksize;
300 static errcode_t test_read_blk(io_channel channel, unsigned long block,
301 int count, void *buf)
303 struct test_private_data *data;
304 errcode_t retval = 0;
306 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
307 data = (struct test_private_data *) channel->private_data;
308 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
311 retval = io_channel_read_blk(data->real, block, count, buf);
313 data->read_blk(block, count, retval);
314 if (data->flags & TEST_FLAG_READ)
315 fprintf(data->outfile,
316 "Test_io: read_blk(%lu, %d) returned %s\n",
317 block, count, retval ? error_message(retval) : "OK");
318 if (data->block && data->block == block) {
319 if (data->flags & TEST_FLAG_DUMP)
320 test_dump_block(channel, data, block, buf);
321 if (--data->read_abort_count == 0)
322 test_abort(channel, block);
327 static errcode_t test_write_blk(io_channel channel, unsigned long block,
328 int count, const void *buf)
330 struct test_private_data *data;
331 errcode_t retval = 0;
333 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
334 data = (struct test_private_data *) channel->private_data;
335 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
338 retval = io_channel_write_blk(data->real, block, count, buf);
340 data->write_blk(block, count, retval);
341 if (data->flags & TEST_FLAG_WRITE)
342 fprintf(data->outfile,
343 "Test_io: write_blk(%lu, %d) returned %s\n",
344 block, count, retval ? error_message(retval) : "OK");
345 if (data->block && data->block == block) {
346 if (data->flags & TEST_FLAG_DUMP)
347 test_dump_block(channel, data, block, buf);
348 if (--data->write_abort_count == 0)
349 test_abort(channel, block);
354 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
355 int count, void *buf)
357 struct test_private_data *data;
358 errcode_t retval = 0;
360 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
361 data = (struct test_private_data *) channel->private_data;
362 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
365 retval = io_channel_read_blk64(data->real, block, count, buf);
366 if (data->read_blk64)
367 data->read_blk64(block, count, retval);
368 if (data->flags & TEST_FLAG_READ)
369 fprintf(data->outfile,
370 "Test_io: read_blk64(%llu, %d) returned %s\n",
371 block, count, retval ? error_message(retval) : "OK");
372 if (data->block && data->block == block) {
373 if (data->flags & TEST_FLAG_DUMP)
374 test_dump_block(channel, data, block, buf);
375 if (--data->read_abort_count == 0)
376 test_abort(channel, block);
381 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
382 int count, const void *buf)
384 struct test_private_data *data;
385 errcode_t retval = 0;
387 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
388 data = (struct test_private_data *) channel->private_data;
389 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
392 retval = io_channel_write_blk64(data->real, block, count, buf);
393 if (data->write_blk64)
394 data->write_blk64(block, count, retval);
395 if (data->flags & TEST_FLAG_WRITE)
396 fprintf(data->outfile,
397 "Test_io: write_blk64(%llu, %d) returned %s\n",
398 block, count, retval ? error_message(retval) : "OK");
399 if (data->block && data->block == block) {
400 if (data->flags & TEST_FLAG_DUMP)
401 test_dump_block(channel, data, block, buf);
402 if (--data->write_abort_count == 0)
403 test_abort(channel, block);
408 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
409 int count, const void *buf)
411 struct test_private_data *data;
412 errcode_t retval = 0;
414 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
415 data = (struct test_private_data *) channel->private_data;
416 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
418 if (data->real && data->real->manager->write_byte)
419 retval = io_channel_write_byte(data->real, offset, count, buf);
420 if (data->write_byte)
421 data->write_byte(offset, count, retval);
422 if (data->flags & TEST_FLAG_WRITE)
423 fprintf(data->outfile,
424 "Test_io: write_byte(%lu, %d) returned %s\n",
425 offset, count, retval ? error_message(retval) : "OK");
429 static errcode_t test_set_option(io_channel channel, const char *option,
432 struct test_private_data *data;
433 errcode_t retval = 0;
435 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
436 data = (struct test_private_data *) channel->private_data;
437 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
440 if (data->flags & TEST_FLAG_SET_OPTION)
441 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
443 if (data->real && data->real->manager->set_option) {
444 retval = (data->real->manager->set_option)(data->real,
446 if (data->flags & TEST_FLAG_SET_OPTION)
447 fprintf(data->outfile, "returned %s\n",
448 retval ? error_message(retval) : "OK");
450 if (data->flags & TEST_FLAG_SET_OPTION)
451 fprintf(data->outfile, "not implemented\n");
456 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
458 struct test_private_data *data;
459 errcode_t retval = 0;
461 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
462 data = (struct test_private_data *) channel->private_data;
463 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
465 if (data->real && data->real->manager->get_stats) {
466 retval = (data->real->manager->get_stats)(data->real, stats);
471 static errcode_t test_discard(io_channel channel, unsigned long long block,
472 unsigned long long count)
474 struct test_private_data *data;
475 errcode_t retval = 0;
477 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
478 data = (struct test_private_data *) channel->private_data;
479 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
482 retval = io_channel_discard(data->real, block, count);
483 if (data->flags & TEST_FLAG_DISCARD)
484 fprintf(data->outfile,
485 "Test_io: discard(%llu, %llu) returned %s\n",
486 block, count, retval ? error_message(retval) : "OK");
490 static errcode_t test_cache_readahead(io_channel channel,
491 unsigned long long block,
492 unsigned long long count)
494 struct test_private_data *data;
495 errcode_t retval = 0;
497 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
498 data = (struct test_private_data *) channel->private_data;
499 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
502 retval = io_channel_cache_readahead(data->real, block, count);
503 if (data->flags & TEST_FLAG_READAHEAD)
504 fprintf(data->outfile,
505 "Test_io: readahead(%llu, %llu) returned %s\n",
506 block, count, retval ? error_message(retval) : "OK");
510 static struct struct_io_manager struct_test_manager = {
511 .magic = EXT2_ET_MAGIC_IO_MANAGER,
512 .name = "Test I/O Manager",
515 .set_blksize = test_set_blksize,
516 .read_blk = test_read_blk,
517 .write_blk = test_write_blk,
519 .write_byte = test_write_byte,
520 .set_option = test_set_option,
521 .get_stats = test_get_stats,
522 .read_blk64 = test_read_blk64,
523 .write_blk64 = test_write_blk64,
524 .discard = test_discard,
525 .cache_readahead = test_cache_readahead,
528 io_manager test_io_manager = &struct_test_manager;