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
89 #define TEST_FLAG_ZEROOUT 0x100
91 static void test_dump_block(io_channel channel,
92 struct test_private_data *data,
93 unsigned long block, const void *buf)
95 const unsigned char *cp;
96 FILE *f = data->outfile;
98 unsigned long cksum = 0;
100 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
103 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
104 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
106 fprintf(f, "%04x: ", i);
107 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
112 * Flush data buffers to disk.
114 static errcode_t test_flush(io_channel channel)
116 struct test_private_data *data;
117 errcode_t retval = 0;
119 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
120 data = (struct test_private_data *)channel->private_data;
121 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
124 retval = io_channel_flush(data->real);
126 if (data->flags & TEST_FLAG_FLUSH)
127 fprintf(data->outfile, "Test_io: flush() returned %s\n",
128 retval ? error_message(retval) : "OK");
133 static void test_abort(io_channel channel, unsigned long block)
135 struct test_private_data *data;
138 data = (struct test_private_data *) channel->private_data;
142 fprintf(f, "Aborting due to I/O to block %lu\n", block);
147 static char *safe_getenv(const char *arg)
150 if ((getuid() != geteuid()) || (getgid() != getegid()))
154 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
157 #if (defined(linux) && defined(SYS_prctl))
158 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
163 #if defined(HAVE_SECURE_GETENV)
164 return secure_getenv(arg);
165 #elif defined(HAVE___SECURE_GETENV)
166 return __secure_getenv(arg);
172 static errcode_t test_open(const char *name, int flags, io_channel *channel)
174 io_channel io = NULL;
175 struct test_private_data *data = NULL;
180 return EXT2_ET_BAD_DEVICE_NAME;
181 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
184 memset(io, 0, sizeof(struct struct_io_channel));
185 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
186 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
189 io->manager = test_io_manager;
190 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
194 strcpy(io->name, name);
195 io->private_data = data;
196 io->block_size = 1024;
202 memset(data, 0, sizeof(struct test_private_data));
203 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
204 if (test_io_backing_manager) {
205 retval = test_io_backing_manager->open(name, flags,
212 data->read_blk = test_io_cb_read_blk;
213 data->write_blk = test_io_cb_write_blk;
214 data->set_blksize = test_io_cb_set_blksize;
215 data->write_byte = test_io_cb_write_byte;
216 data->read_blk64 = test_io_cb_read_blk64;
217 data->write_blk64 = test_io_cb_write_blk64;
219 data->outfile = NULL;
220 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
221 data->outfile = fopen(value, "w");
223 data->outfile = stderr;
226 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
227 data->flags = strtoul(value, NULL, 0);
230 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
231 data->block = strtoul(value, NULL, 0);
233 data->read_abort_count = 0;
234 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
235 data->read_abort_count = strtoul(value, NULL, 0);
237 data->write_abort_count = 0;
238 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
239 data->write_abort_count = strtoul(value, NULL, 0);
242 io->align = data->real->align;
243 if (data->real->flags & CHANNEL_FLAGS_THREADS)
244 io->flags |= CHANNEL_FLAGS_THREADS;
252 ext2fs_free_mem(&io->name);
254 ext2fs_free_mem(&io);
256 ext2fs_free_mem(&data);
260 static errcode_t test_close(io_channel channel)
262 struct test_private_data *data;
263 errcode_t retval = 0;
265 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
266 data = (struct test_private_data *) channel->private_data;
267 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
269 if (--channel->refcount > 0)
273 retval = io_channel_close(data->real);
275 if (data->outfile && data->outfile != stderr)
276 fclose(data->outfile);
278 ext2fs_free_mem(&channel->private_data);
280 ext2fs_free_mem(&channel->name);
281 ext2fs_free_mem(&channel);
285 static errcode_t test_set_blksize(io_channel channel, int blksize)
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_set_blksize(data->real, blksize);
296 channel->align = data->real->align;
298 if (data->set_blksize)
299 data->set_blksize(blksize, retval);
300 if (data->flags & TEST_FLAG_SET_BLKSIZE)
301 fprintf(data->outfile,
302 "Test_io: set_blksize(%d) returned %s\n",
303 blksize, retval ? error_message(retval) : "OK");
304 channel->block_size = blksize;
309 static errcode_t test_read_blk(io_channel channel, unsigned long block,
310 int count, void *buf)
312 struct test_private_data *data;
313 errcode_t retval = 0;
315 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
316 data = (struct test_private_data *) channel->private_data;
317 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
320 retval = io_channel_read_blk(data->real, block, count, buf);
322 data->read_blk(block, count, retval);
323 if (data->flags & TEST_FLAG_READ)
324 fprintf(data->outfile,
325 "Test_io: read_blk(%lu, %d) returned %s\n",
326 block, count, retval ? error_message(retval) : "OK");
327 if (data->block && data->block == block) {
328 if (data->flags & TEST_FLAG_DUMP)
329 test_dump_block(channel, data, block, buf);
330 if (--data->read_abort_count == 0)
331 test_abort(channel, block);
336 static errcode_t test_write_blk(io_channel channel, unsigned long block,
337 int count, const void *buf)
339 struct test_private_data *data;
340 errcode_t retval = 0;
342 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
343 data = (struct test_private_data *) channel->private_data;
344 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
347 retval = io_channel_write_blk(data->real, block, count, buf);
349 data->write_blk(block, count, retval);
350 if (data->flags & TEST_FLAG_WRITE)
351 fprintf(data->outfile,
352 "Test_io: write_blk(%lu, %d) returned %s\n",
353 block, count, retval ? error_message(retval) : "OK");
354 if (data->block && data->block == block) {
355 if (data->flags & TEST_FLAG_DUMP)
356 test_dump_block(channel, data, block, buf);
357 if (--data->write_abort_count == 0)
358 test_abort(channel, block);
363 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
364 int count, void *buf)
366 struct test_private_data *data;
367 errcode_t retval = 0;
369 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
370 data = (struct test_private_data *) channel->private_data;
371 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
374 retval = io_channel_read_blk64(data->real, block, count, buf);
375 if (data->read_blk64)
376 data->read_blk64(block, count, retval);
377 if (data->flags & TEST_FLAG_READ)
378 fprintf(data->outfile,
379 "Test_io: read_blk64(%llu, %d) returned %s\n",
380 block, count, retval ? error_message(retval) : "OK");
381 if (data->block && data->block == block) {
382 if (data->flags & TEST_FLAG_DUMP)
383 test_dump_block(channel, data, block, buf);
384 if (--data->read_abort_count == 0)
385 test_abort(channel, block);
390 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
391 int count, const void *buf)
393 struct test_private_data *data;
394 errcode_t retval = 0;
396 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
397 data = (struct test_private_data *) channel->private_data;
398 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
401 retval = io_channel_write_blk64(data->real, block, count, buf);
402 if (data->write_blk64)
403 data->write_blk64(block, count, retval);
404 if (data->flags & TEST_FLAG_WRITE)
405 fprintf(data->outfile,
406 "Test_io: write_blk64(%llu, %d) returned %s\n",
407 block, count, retval ? error_message(retval) : "OK");
408 if (data->block && data->block == block) {
409 if (data->flags & TEST_FLAG_DUMP)
410 test_dump_block(channel, data, block, buf);
411 if (--data->write_abort_count == 0)
412 test_abort(channel, block);
417 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
418 int count, const void *buf)
420 struct test_private_data *data;
421 errcode_t retval = 0;
423 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
424 data = (struct test_private_data *) channel->private_data;
425 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
427 if (data->real && data->real->manager->write_byte)
428 retval = io_channel_write_byte(data->real, offset, count, buf);
429 if (data->write_byte)
430 data->write_byte(offset, count, retval);
431 if (data->flags & TEST_FLAG_WRITE)
432 fprintf(data->outfile,
433 "Test_io: write_byte(%lu, %d) returned %s\n",
434 offset, count, retval ? error_message(retval) : "OK");
438 static errcode_t test_set_option(io_channel channel, const char *option,
441 struct test_private_data *data;
442 errcode_t retval = 0;
444 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
445 data = (struct test_private_data *) channel->private_data;
446 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
449 if (data->flags & TEST_FLAG_SET_OPTION)
450 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
452 if (data->real && data->real->manager->set_option) {
453 retval = (data->real->manager->set_option)(data->real,
455 if (data->flags & TEST_FLAG_SET_OPTION)
456 fprintf(data->outfile, "returned %s\n",
457 retval ? error_message(retval) : "OK");
459 if (data->flags & TEST_FLAG_SET_OPTION)
460 fprintf(data->outfile, "not implemented\n");
465 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
467 struct test_private_data *data;
468 errcode_t retval = 0;
470 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
471 data = (struct test_private_data *) channel->private_data;
472 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
474 if (data->real && data->real->manager->get_stats) {
475 retval = (data->real->manager->get_stats)(data->real, stats);
480 static errcode_t test_discard(io_channel channel, unsigned long long block,
481 unsigned long long count)
483 struct test_private_data *data;
484 errcode_t retval = 0;
486 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
487 data = (struct test_private_data *) channel->private_data;
488 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
491 retval = io_channel_discard(data->real, block, count);
492 if (data->flags & TEST_FLAG_DISCARD)
493 fprintf(data->outfile,
494 "Test_io: discard(%llu, %llu) returned %s\n",
495 block, count, retval ? error_message(retval) : "OK");
499 static errcode_t test_cache_readahead(io_channel channel,
500 unsigned long long block,
501 unsigned long long count)
503 struct test_private_data *data;
504 errcode_t retval = 0;
506 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
507 data = (struct test_private_data *) channel->private_data;
508 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
511 retval = io_channel_cache_readahead(data->real, block, count);
512 if (data->flags & TEST_FLAG_READAHEAD)
513 fprintf(data->outfile,
514 "Test_io: readahead(%llu, %llu) returned %s\n",
515 block, count, retval ? error_message(retval) : "OK");
519 static errcode_t test_zeroout(io_channel channel, unsigned long long block,
520 unsigned long long count)
522 struct test_private_data *data;
523 errcode_t retval = 0;
525 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
526 data = (struct test_private_data *) channel->private_data;
527 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
530 retval = io_channel_zeroout(data->real, block, count);
531 if (data->flags & TEST_FLAG_ZEROOUT)
532 fprintf(data->outfile,
533 "Test_io: zeroout(%llu, %llu) returned %s\n",
534 block, count, retval ? error_message(retval) : "OK");
538 static struct struct_io_manager struct_test_manager = {
539 .magic = EXT2_ET_MAGIC_IO_MANAGER,
540 .name = "Test I/O Manager",
543 .set_blksize = test_set_blksize,
544 .read_blk = test_read_blk,
545 .write_blk = test_write_blk,
547 .write_byte = test_write_byte,
548 .set_option = test_set_option,
549 .get_stats = test_get_stats,
550 .read_blk64 = test_read_blk64,
551 .write_blk64 = test_write_blk64,
552 .discard = test_discard,
553 .cache_readahead = test_cache_readahead,
554 .zeroout = test_zeroout,
557 io_manager test_io_manager = &struct_test_manager;