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.
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
28 #define PR_GET_DUMPABLE 3
30 #if (!defined(HAVE_PRCTL) && defined(linux))
31 #include <sys/syscall.h>
38 * For checking structure magic numbers...
41 #define EXT2_CHECK_MAGIC(struct, code) \
42 if ((struct)->magic != (code)) return (code)
44 struct test_private_data {
50 int read_abort_count, write_abort_count;
51 void (*read_blk)(unsigned long block, int count, errcode_t err);
52 void (*write_blk)(unsigned long block, int count, errcode_t err);
53 void (*set_blksize)(int blksize, errcode_t err);
54 void (*write_byte)(unsigned long block, int count, errcode_t err);
55 void (*read_blk64)(unsigned long long block, int count, errcode_t err);
56 void (*write_blk64)(unsigned long long block, int count, errcode_t err);
59 static errcode_t test_open(const char *name, int flags, io_channel *channel);
60 static errcode_t test_close(io_channel channel);
61 static errcode_t test_set_blksize(io_channel channel, int blksize);
62 static errcode_t test_read_blk(io_channel channel, unsigned long block,
63 int count, void *data);
64 static errcode_t test_write_blk(io_channel channel, unsigned long block,
65 int count, const void *data);
66 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
67 int count, void *data);
68 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
69 int count, const void *data);
70 static errcode_t test_flush(io_channel channel);
71 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
72 int count, const void *buf);
73 static errcode_t test_set_option(io_channel channel, const char *option,
75 static errcode_t test_get_stats(io_channel channel, io_stats *stats);
76 static errcode_t test_discard(io_channel channel, unsigned long long block,
77 unsigned long long count);
79 static struct struct_io_manager struct_test_manager = {
80 EXT2_ET_MAGIC_IO_MANAGER,
96 io_manager test_io_manager = &struct_test_manager;
99 * These global variable can be set by the test program as
100 * necessary *before* calling test_open
102 io_manager test_io_backing_manager = 0;
103 void (*test_io_cb_read_blk)
104 (unsigned long block, int count, errcode_t err) = 0;
105 void (*test_io_cb_write_blk)
106 (unsigned long block, int count, errcode_t err) = 0;
107 void (*test_io_cb_read_blk64)
108 (unsigned long long block, int count, errcode_t err) = 0;
109 void (*test_io_cb_write_blk64)
110 (unsigned long long block, int count, errcode_t err) = 0;
111 void (*test_io_cb_set_blksize)
112 (int blksize, errcode_t err) = 0;
113 void (*test_io_cb_write_byte)
114 (unsigned long block, int count, errcode_t err) = 0;
119 #define TEST_FLAG_READ 0x01
120 #define TEST_FLAG_WRITE 0x02
121 #define TEST_FLAG_SET_BLKSIZE 0x04
122 #define TEST_FLAG_FLUSH 0x08
123 #define TEST_FLAG_DUMP 0x10
124 #define TEST_FLAG_SET_OPTION 0x20
125 #define TEST_FLAG_DISCARD 0x40
127 static void test_dump_block(io_channel channel,
128 struct test_private_data *data,
129 unsigned long block, const void *buf)
131 const unsigned char *cp;
132 FILE *f = data->outfile;
134 unsigned long cksum = 0;
136 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
139 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
140 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
142 fprintf(f, "%04x: ", i);
143 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
147 static void test_abort(io_channel channel, unsigned long block)
149 struct test_private_data *data;
152 data = (struct test_private_data *) channel->private_data;
156 fprintf(f, "Aborting due to I/O to block %lu\n", block);
161 static char *safe_getenv(const char *arg)
163 if ((getuid() != geteuid()) || (getgid() != getegid()))
166 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
169 #if (defined(linux) && defined(SYS_prctl))
170 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
175 #ifdef HAVE___SECURE_GETENV
176 return __secure_getenv(arg);
182 static errcode_t test_open(const char *name, int flags, io_channel *channel)
184 io_channel io = NULL;
185 struct test_private_data *data = NULL;
190 return EXT2_ET_BAD_DEVICE_NAME;
191 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
194 memset(io, 0, sizeof(struct struct_io_channel));
195 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
196 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
198 retval = EXT2_ET_NO_MEMORY;
201 io->manager = test_io_manager;
202 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
206 strcpy(io->name, name);
207 io->private_data = data;
208 io->block_size = 1024;
213 memset(data, 0, sizeof(struct test_private_data));
214 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
215 if (test_io_backing_manager) {
216 retval = test_io_backing_manager->open(name, flags,
222 data->read_blk = test_io_cb_read_blk;
223 data->write_blk = test_io_cb_write_blk;
224 data->set_blksize = test_io_cb_set_blksize;
225 data->write_byte = test_io_cb_write_byte;
226 data->read_blk64 = test_io_cb_read_blk64;
227 data->write_blk64 = test_io_cb_write_blk64;
229 data->outfile = NULL;
230 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
231 data->outfile = fopen(value, "w");
233 data->outfile = stderr;
236 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
237 data->flags = strtoul(value, NULL, 0);
240 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
241 data->block = strtoul(value, NULL, 0);
243 data->read_abort_count = 0;
244 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
245 data->read_abort_count = strtoul(value, NULL, 0);
247 data->write_abort_count = 0;
248 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
249 data->write_abort_count = strtoul(value, NULL, 0);
256 ext2fs_free_mem(&io);
258 ext2fs_free_mem(&data);
262 static errcode_t test_close(io_channel channel)
264 struct test_private_data *data;
265 errcode_t retval = 0;
267 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
268 data = (struct test_private_data *) channel->private_data;
269 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
271 if (--channel->refcount > 0)
275 retval = io_channel_close(data->real);
277 if (data->outfile && data->outfile != stderr)
278 fclose(data->outfile);
280 ext2fs_free_mem(&channel->private_data);
282 ext2fs_free_mem(&channel->name);
283 ext2fs_free_mem(&channel);
287 static errcode_t test_set_blksize(io_channel channel, int blksize)
289 struct test_private_data *data;
290 errcode_t retval = 0;
292 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
293 data = (struct test_private_data *) channel->private_data;
294 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
297 retval = io_channel_set_blksize(data->real, blksize);
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");
439 * Flush data buffers to disk.
441 static errcode_t test_flush(io_channel channel)
443 struct test_private_data *data;
444 errcode_t retval = 0;
446 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
447 data = (struct test_private_data *) channel->private_data;
448 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
451 retval = io_channel_flush(data->real);
453 if (data->flags & TEST_FLAG_FLUSH)
454 fprintf(data->outfile, "Test_io: flush() returned %s\n",
455 retval ? error_message(retval) : "OK");
460 static errcode_t test_set_option(io_channel channel, const char *option,
463 struct test_private_data *data;
464 errcode_t retval = 0;
466 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
467 data = (struct test_private_data *) channel->private_data;
468 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
471 if (data->flags & TEST_FLAG_SET_OPTION)
472 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
474 if (data->real && data->real->manager->set_option) {
475 retval = (data->real->manager->set_option)(data->real,
477 if (data->flags & TEST_FLAG_SET_OPTION)
478 fprintf(data->outfile, "returned %s\n",
479 retval ? error_message(retval) : "OK");
481 if (data->flags & TEST_FLAG_SET_OPTION)
482 fprintf(data->outfile, "not implemented\n");
487 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
489 struct test_private_data *data;
490 errcode_t retval = 0;
492 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
493 data = (struct test_private_data *) channel->private_data;
494 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
496 if (data->real && data->real->manager->get_stats) {
497 retval = (data->real->manager->get_stats)(data->real, stats);
502 static errcode_t test_discard(io_channel channel, unsigned long long block,
503 unsigned long long count)
505 struct test_private_data *data;
506 errcode_t retval = 0;
508 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
509 data = (struct test_private_data *) channel->private_data;
510 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
513 retval = io_channel_discard(data->real, block, count);
514 if (data->flags & TEST_FLAG_DISCARD)
515 fprintf(data->outfile,
516 "Test_io: discard(%llu, %llu) returned %s\n",
517 block, count, retval ? error_message(retval) : "OK");