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.
13 #if HAVE_SECURE_GETENV
16 #if HAVE_SECURE_GETENV
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
35 #define PR_GET_DUMPABLE 3
37 #if (!defined(HAVE_PRCTL) && defined(linux))
38 #include <sys/syscall.h>
45 * For checking structure magic numbers...
48 #define EXT2_CHECK_MAGIC(struct, code) \
49 if ((struct)->magic != (code)) return (code)
51 struct test_private_data {
57 int read_abort_count, write_abort_count;
58 void (*read_blk)(unsigned long block, int count, errcode_t err);
59 void (*write_blk)(unsigned long block, int count, errcode_t err);
60 void (*set_blksize)(int blksize, errcode_t err);
61 void (*write_byte)(unsigned long block, int count, errcode_t err);
62 void (*read_blk64)(unsigned long long block, int count, errcode_t err);
63 void (*write_blk64)(unsigned long long block, int count, errcode_t err);
66 static errcode_t test_open(const char *name, int flags, io_channel *channel);
67 static errcode_t test_close(io_channel channel);
68 static errcode_t test_set_blksize(io_channel channel, int blksize);
69 static errcode_t test_read_blk(io_channel channel, unsigned long block,
70 int count, void *data);
71 static errcode_t test_write_blk(io_channel channel, unsigned long block,
72 int count, const void *data);
73 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
74 int count, void *data);
75 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
76 int count, const void *data);
77 static errcode_t test_flush(io_channel channel);
78 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
79 int count, const void *buf);
80 static errcode_t test_set_option(io_channel channel, const char *option,
82 static errcode_t test_get_stats(io_channel channel, io_stats *stats);
83 static errcode_t test_discard(io_channel channel, unsigned long long block,
84 unsigned long long count);
86 static struct struct_io_manager struct_test_manager = {
87 EXT2_ET_MAGIC_IO_MANAGER,
103 io_manager test_io_manager = &struct_test_manager;
106 * These global variable can be set by the test program as
107 * necessary *before* calling test_open
109 io_manager test_io_backing_manager = 0;
110 void (*test_io_cb_read_blk)
111 (unsigned long block, int count, errcode_t err) = 0;
112 void (*test_io_cb_write_blk)
113 (unsigned long block, int count, errcode_t err) = 0;
114 void (*test_io_cb_read_blk64)
115 (unsigned long long block, int count, errcode_t err) = 0;
116 void (*test_io_cb_write_blk64)
117 (unsigned long long block, int count, errcode_t err) = 0;
118 void (*test_io_cb_set_blksize)
119 (int blksize, errcode_t err) = 0;
120 void (*test_io_cb_write_byte)
121 (unsigned long block, int count, errcode_t err) = 0;
126 #define TEST_FLAG_READ 0x01
127 #define TEST_FLAG_WRITE 0x02
128 #define TEST_FLAG_SET_BLKSIZE 0x04
129 #define TEST_FLAG_FLUSH 0x08
130 #define TEST_FLAG_DUMP 0x10
131 #define TEST_FLAG_SET_OPTION 0x20
132 #define TEST_FLAG_DISCARD 0x40
134 static void test_dump_block(io_channel channel,
135 struct test_private_data *data,
136 unsigned long block, const void *buf)
138 const unsigned char *cp;
139 FILE *f = data->outfile;
141 unsigned long cksum = 0;
143 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
146 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
147 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
149 fprintf(f, "%04x: ", i);
150 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
154 static void test_abort(io_channel channel, unsigned long block)
156 struct test_private_data *data;
159 data = (struct test_private_data *) channel->private_data;
163 fprintf(f, "Aborting due to I/O to block %lu\n", block);
168 static char *safe_getenv(const char *arg)
170 if ((getuid() != geteuid()) || (getgid() != getegid()))
173 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
176 #if (defined(linux) && defined(SYS_prctl))
177 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
182 #if defined(HAVE_SECURE_GETENV)
183 return secure_getenv(arg);
184 #elif defined(HAVE___SECURE_GETENV)
185 return __secure_getenv(arg);
191 static errcode_t test_open(const char *name, int flags, io_channel *channel)
193 io_channel io = NULL;
194 struct test_private_data *data = NULL;
199 return EXT2_ET_BAD_DEVICE_NAME;
200 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
203 memset(io, 0, sizeof(struct struct_io_channel));
204 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
205 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
208 io->manager = test_io_manager;
209 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
213 strcpy(io->name, name);
214 io->private_data = data;
215 io->block_size = 1024;
220 memset(data, 0, sizeof(struct test_private_data));
221 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
222 if (test_io_backing_manager) {
223 retval = test_io_backing_manager->open(name, flags,
229 data->read_blk = test_io_cb_read_blk;
230 data->write_blk = test_io_cb_write_blk;
231 data->set_blksize = test_io_cb_set_blksize;
232 data->write_byte = test_io_cb_write_byte;
233 data->read_blk64 = test_io_cb_read_blk64;
234 data->write_blk64 = test_io_cb_write_blk64;
236 data->outfile = NULL;
237 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
238 data->outfile = fopen(value, "w");
240 data->outfile = stderr;
243 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
244 data->flags = strtoul(value, NULL, 0);
247 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
248 data->block = strtoul(value, NULL, 0);
250 data->read_abort_count = 0;
251 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
252 data->read_abort_count = strtoul(value, NULL, 0);
254 data->write_abort_count = 0;
255 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
256 data->write_abort_count = strtoul(value, NULL, 0);
259 io->align = data->real->align;
266 ext2fs_free_mem(&io);
268 ext2fs_free_mem(&data);
272 static errcode_t test_close(io_channel channel)
274 struct test_private_data *data;
275 errcode_t retval = 0;
277 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
278 data = (struct test_private_data *) channel->private_data;
279 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
281 if (--channel->refcount > 0)
285 retval = io_channel_close(data->real);
287 if (data->outfile && data->outfile != stderr)
288 fclose(data->outfile);
290 ext2fs_free_mem(&channel->private_data);
292 ext2fs_free_mem(&channel->name);
293 ext2fs_free_mem(&channel);
297 static errcode_t test_set_blksize(io_channel channel, int blksize)
299 struct test_private_data *data;
300 errcode_t retval = 0;
302 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
303 data = (struct test_private_data *) channel->private_data;
304 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
307 retval = io_channel_set_blksize(data->real, blksize);
308 channel->align = data->real->align;
310 if (data->set_blksize)
311 data->set_blksize(blksize, retval);
312 if (data->flags & TEST_FLAG_SET_BLKSIZE)
313 fprintf(data->outfile,
314 "Test_io: set_blksize(%d) returned %s\n",
315 blksize, retval ? error_message(retval) : "OK");
316 channel->block_size = blksize;
321 static errcode_t test_read_blk(io_channel channel, unsigned long block,
322 int count, void *buf)
324 struct test_private_data *data;
325 errcode_t retval = 0;
327 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
328 data = (struct test_private_data *) channel->private_data;
329 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
332 retval = io_channel_read_blk(data->real, block, count, buf);
334 data->read_blk(block, count, retval);
335 if (data->flags & TEST_FLAG_READ)
336 fprintf(data->outfile,
337 "Test_io: read_blk(%lu, %d) returned %s\n",
338 block, count, retval ? error_message(retval) : "OK");
339 if (data->block && data->block == block) {
340 if (data->flags & TEST_FLAG_DUMP)
341 test_dump_block(channel, data, block, buf);
342 if (--data->read_abort_count == 0)
343 test_abort(channel, block);
348 static errcode_t test_write_blk(io_channel channel, unsigned long block,
349 int count, const void *buf)
351 struct test_private_data *data;
352 errcode_t retval = 0;
354 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
355 data = (struct test_private_data *) channel->private_data;
356 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
359 retval = io_channel_write_blk(data->real, block, count, buf);
361 data->write_blk(block, count, retval);
362 if (data->flags & TEST_FLAG_WRITE)
363 fprintf(data->outfile,
364 "Test_io: write_blk(%lu, %d) returned %s\n",
365 block, count, retval ? error_message(retval) : "OK");
366 if (data->block && data->block == block) {
367 if (data->flags & TEST_FLAG_DUMP)
368 test_dump_block(channel, data, block, buf);
369 if (--data->write_abort_count == 0)
370 test_abort(channel, block);
375 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
376 int count, void *buf)
378 struct test_private_data *data;
379 errcode_t retval = 0;
381 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
382 data = (struct test_private_data *) channel->private_data;
383 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
386 retval = io_channel_read_blk64(data->real, block, count, buf);
387 if (data->read_blk64)
388 data->read_blk64(block, count, retval);
389 if (data->flags & TEST_FLAG_READ)
390 fprintf(data->outfile,
391 "Test_io: read_blk64(%llu, %d) returned %s\n",
392 block, count, retval ? error_message(retval) : "OK");
393 if (data->block && data->block == block) {
394 if (data->flags & TEST_FLAG_DUMP)
395 test_dump_block(channel, data, block, buf);
396 if (--data->read_abort_count == 0)
397 test_abort(channel, block);
402 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
403 int count, const void *buf)
405 struct test_private_data *data;
406 errcode_t retval = 0;
408 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
409 data = (struct test_private_data *) channel->private_data;
410 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
413 retval = io_channel_write_blk64(data->real, block, count, buf);
414 if (data->write_blk64)
415 data->write_blk64(block, count, retval);
416 if (data->flags & TEST_FLAG_WRITE)
417 fprintf(data->outfile,
418 "Test_io: write_blk64(%llu, %d) returned %s\n",
419 block, count, retval ? error_message(retval) : "OK");
420 if (data->block && data->block == block) {
421 if (data->flags & TEST_FLAG_DUMP)
422 test_dump_block(channel, data, block, buf);
423 if (--data->write_abort_count == 0)
424 test_abort(channel, block);
429 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
430 int count, const void *buf)
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);
439 if (data->real && data->real->manager->write_byte)
440 retval = io_channel_write_byte(data->real, offset, count, buf);
441 if (data->write_byte)
442 data->write_byte(offset, count, retval);
443 if (data->flags & TEST_FLAG_WRITE)
444 fprintf(data->outfile,
445 "Test_io: write_byte(%lu, %d) returned %s\n",
446 offset, count, retval ? error_message(retval) : "OK");
451 * Flush data buffers to disk.
453 static errcode_t test_flush(io_channel channel)
455 struct test_private_data *data;
456 errcode_t retval = 0;
458 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
459 data = (struct test_private_data *) channel->private_data;
460 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
463 retval = io_channel_flush(data->real);
465 if (data->flags & TEST_FLAG_FLUSH)
466 fprintf(data->outfile, "Test_io: flush() returned %s\n",
467 retval ? error_message(retval) : "OK");
472 static errcode_t test_set_option(io_channel channel, const char *option,
475 struct test_private_data *data;
476 errcode_t retval = 0;
478 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
479 data = (struct test_private_data *) channel->private_data;
480 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
483 if (data->flags & TEST_FLAG_SET_OPTION)
484 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
486 if (data->real && data->real->manager->set_option) {
487 retval = (data->real->manager->set_option)(data->real,
489 if (data->flags & TEST_FLAG_SET_OPTION)
490 fprintf(data->outfile, "returned %s\n",
491 retval ? error_message(retval) : "OK");
493 if (data->flags & TEST_FLAG_SET_OPTION)
494 fprintf(data->outfile, "not implemented\n");
499 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
501 struct test_private_data *data;
502 errcode_t retval = 0;
504 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
505 data = (struct test_private_data *) channel->private_data;
506 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
508 if (data->real && data->real->manager->get_stats) {
509 retval = (data->real->manager->get_stats)(data->real, stats);
514 static errcode_t test_discard(io_channel channel, unsigned long long block,
515 unsigned long long count)
517 struct test_private_data *data;
518 errcode_t retval = 0;
520 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
521 data = (struct test_private_data *) channel->private_data;
522 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
525 retval = io_channel_discard(data->real, block, count);
526 if (data->flags & TEST_FLAG_DISCARD)
527 fprintf(data->outfile,
528 "Test_io: discard(%llu, %llu) returned %s\n",
529 block, count, retval ? error_message(retval) : "OK");