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);
78 static struct struct_io_manager struct_test_manager = {
79 EXT2_ET_MAGIC_IO_MANAGER,
94 io_manager test_io_manager = &struct_test_manager;
97 * These global variable can be set by the test program as
98 * necessary *before* calling test_open
100 io_manager test_io_backing_manager = 0;
101 void (*test_io_cb_read_blk)
102 (unsigned long block, int count, errcode_t err) = 0;
103 void (*test_io_cb_write_blk)
104 (unsigned long block, int count, errcode_t err) = 0;
105 void (*test_io_cb_read_blk64)
106 (unsigned long long block, int count, errcode_t err) = 0;
107 void (*test_io_cb_write_blk64)
108 (unsigned long long block, int count, errcode_t err) = 0;
109 void (*test_io_cb_set_blksize)
110 (int blksize, errcode_t err) = 0;
111 void (*test_io_cb_write_byte)
112 (unsigned long block, int count, errcode_t err) = 0;
117 #define TEST_FLAG_READ 0x01
118 #define TEST_FLAG_WRITE 0x02
119 #define TEST_FLAG_SET_BLKSIZE 0x04
120 #define TEST_FLAG_FLUSH 0x08
121 #define TEST_FLAG_DUMP 0x10
122 #define TEST_FLAG_SET_OPTION 0x20
124 static void test_dump_block(io_channel channel,
125 struct test_private_data *data,
126 unsigned long block, const void *buf)
128 const unsigned char *cp;
129 FILE *f = data->outfile;
131 unsigned long cksum = 0;
133 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
136 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
137 for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
139 fprintf(f, "%04x: ", i);
140 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
144 static void test_abort(io_channel channel, unsigned long block)
146 struct test_private_data *data;
149 data = (struct test_private_data *) channel->private_data;
153 fprintf(f, "Aborting due to I/O to block %lu\n", block);
158 static char *safe_getenv(const char *arg)
160 if ((getuid() != geteuid()) || (getgid() != getegid()))
163 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
166 #if (defined(linux) && defined(SYS_prctl))
167 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
172 #ifdef HAVE___SECURE_GETENV
173 return __secure_getenv(arg);
179 static errcode_t test_open(const char *name, int flags, io_channel *channel)
181 io_channel io = NULL;
182 struct test_private_data *data = NULL;
187 return EXT2_ET_BAD_DEVICE_NAME;
188 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
191 memset(io, 0, sizeof(struct struct_io_channel));
192 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
193 retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
195 retval = EXT2_ET_NO_MEMORY;
198 io->manager = test_io_manager;
199 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
203 strcpy(io->name, name);
204 io->private_data = data;
205 io->block_size = 1024;
210 memset(data, 0, sizeof(struct test_private_data));
211 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
212 if (test_io_backing_manager) {
213 retval = test_io_backing_manager->open(name, flags,
219 data->read_blk = test_io_cb_read_blk;
220 data->write_blk = test_io_cb_write_blk;
221 data->set_blksize = test_io_cb_set_blksize;
222 data->write_byte = test_io_cb_write_byte;
223 data->read_blk64 = test_io_cb_read_blk64;
224 data->write_blk64 = test_io_cb_write_blk64;
226 data->outfile = NULL;
227 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL)
228 data->outfile = fopen(value, "w");
230 data->outfile = stderr;
233 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL)
234 data->flags = strtoul(value, NULL, 0);
237 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL)
238 data->block = strtoul(value, NULL, 0);
240 data->read_abort_count = 0;
241 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL)
242 data->read_abort_count = strtoul(value, NULL, 0);
244 data->write_abort_count = 0;
245 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL)
246 data->write_abort_count = strtoul(value, NULL, 0);
253 ext2fs_free_mem(&io);
255 ext2fs_free_mem(&data);
259 static errcode_t test_close(io_channel channel)
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);
268 if (--channel->refcount > 0)
272 retval = io_channel_close(data->real);
274 if (data->outfile && data->outfile != stderr)
275 fclose(data->outfile);
277 ext2fs_free_mem(&channel->private_data);
279 ext2fs_free_mem(&channel->name);
280 ext2fs_free_mem(&channel);
284 static errcode_t test_set_blksize(io_channel channel, int blksize)
286 struct test_private_data *data;
287 errcode_t retval = 0;
289 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
290 data = (struct test_private_data *) channel->private_data;
291 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
294 retval = io_channel_set_blksize(data->real, blksize);
295 if (data->set_blksize)
296 data->set_blksize(blksize, retval);
297 if (data->flags & TEST_FLAG_SET_BLKSIZE)
298 fprintf(data->outfile,
299 "Test_io: set_blksize(%d) returned %s\n",
300 blksize, retval ? error_message(retval) : "OK");
301 channel->block_size = blksize;
306 static errcode_t test_read_blk(io_channel channel, unsigned long block,
307 int count, void *buf)
309 struct test_private_data *data;
310 errcode_t retval = 0;
312 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
313 data = (struct test_private_data *) channel->private_data;
314 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
317 retval = io_channel_read_blk(data->real, block, count, buf);
319 data->read_blk(block, count, retval);
320 if (data->flags & TEST_FLAG_READ)
321 fprintf(data->outfile,
322 "Test_io: read_blk(%lu, %d) returned %s\n",
323 block, count, retval ? error_message(retval) : "OK");
324 if (data->block && data->block == block) {
325 if (data->flags & TEST_FLAG_DUMP)
326 test_dump_block(channel, data, block, buf);
327 if (--data->read_abort_count == 0)
328 test_abort(channel, block);
333 static errcode_t test_write_blk(io_channel channel, unsigned long block,
334 int count, const void *buf)
336 struct test_private_data *data;
337 errcode_t retval = 0;
339 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
340 data = (struct test_private_data *) channel->private_data;
341 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
344 retval = io_channel_write_blk(data->real, block, count, buf);
346 data->write_blk(block, count, retval);
347 if (data->flags & TEST_FLAG_WRITE)
348 fprintf(data->outfile,
349 "Test_io: write_blk(%lu, %d) returned %s\n",
350 block, count, retval ? error_message(retval) : "OK");
351 if (data->block && data->block == block) {
352 if (data->flags & TEST_FLAG_DUMP)
353 test_dump_block(channel, data, block, buf);
354 if (--data->write_abort_count == 0)
355 test_abort(channel, block);
360 static errcode_t test_read_blk64(io_channel channel, unsigned long long block,
361 int count, void *buf)
363 struct test_private_data *data;
364 errcode_t retval = 0;
366 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
367 data = (struct test_private_data *) channel->private_data;
368 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
371 retval = io_channel_read_blk64(data->real, block, count, buf);
372 if (data->read_blk64)
373 data->read_blk64(block, count, retval);
374 if (data->flags & TEST_FLAG_READ)
375 fprintf(data->outfile,
376 "Test_io: read_blk64(%llu, %d) returned %s\n",
377 block, count, retval ? error_message(retval) : "OK");
378 if (data->block && data->block == block) {
379 if (data->flags & TEST_FLAG_DUMP)
380 test_dump_block(channel, data, block, buf);
381 if (--data->read_abort_count == 0)
382 test_abort(channel, block);
387 static errcode_t test_write_blk64(io_channel channel, unsigned long long block,
388 int count, const void *buf)
390 struct test_private_data *data;
391 errcode_t retval = 0;
393 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
394 data = (struct test_private_data *) channel->private_data;
395 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
398 retval = io_channel_write_blk64(data->real, block, count, buf);
399 if (data->write_blk64)
400 data->write_blk64(block, count, retval);
401 if (data->flags & TEST_FLAG_WRITE)
402 fprintf(data->outfile,
403 "Test_io: write_blk64(%llu, %d) returned %s\n",
404 block, count, retval ? error_message(retval) : "OK");
405 if (data->block && data->block == block) {
406 if (data->flags & TEST_FLAG_DUMP)
407 test_dump_block(channel, data, block, buf);
408 if (--data->write_abort_count == 0)
409 test_abort(channel, block);
414 static errcode_t test_write_byte(io_channel channel, unsigned long offset,
415 int count, const void *buf)
417 struct test_private_data *data;
418 errcode_t retval = 0;
420 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
421 data = (struct test_private_data *) channel->private_data;
422 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
424 if (data->real && data->real->manager->write_byte)
425 retval = io_channel_write_byte(data->real, offset, count, buf);
426 if (data->write_byte)
427 data->write_byte(offset, count, retval);
428 if (data->flags & TEST_FLAG_WRITE)
429 fprintf(data->outfile,
430 "Test_io: write_byte(%lu, %d) returned %s\n",
431 offset, count, retval ? error_message(retval) : "OK");
436 * Flush data buffers to disk.
438 static errcode_t test_flush(io_channel channel)
440 struct test_private_data *data;
441 errcode_t retval = 0;
443 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
444 data = (struct test_private_data *) channel->private_data;
445 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
448 retval = io_channel_flush(data->real);
450 if (data->flags & TEST_FLAG_FLUSH)
451 fprintf(data->outfile, "Test_io: flush() returned %s\n",
452 retval ? error_message(retval) : "OK");
457 static errcode_t test_set_option(io_channel channel, const char *option,
460 struct test_private_data *data;
461 errcode_t retval = 0;
463 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
464 data = (struct test_private_data *) channel->private_data;
465 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
468 if (data->flags & TEST_FLAG_SET_OPTION)
469 fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
471 if (data->real && data->real->manager->set_option) {
472 retval = (data->real->manager->set_option)(data->real,
474 if (data->flags & TEST_FLAG_SET_OPTION)
475 fprintf(data->outfile, "returned %s\n",
476 retval ? error_message(retval) : "OK");
478 if (data->flags & TEST_FLAG_SET_OPTION)
479 fprintf(data->outfile, "not implemented\n");
484 static errcode_t test_get_stats(io_channel channel, io_stats *stats)
486 struct test_private_data *data;
487 errcode_t retval = 0;
489 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
490 data = (struct test_private_data *) channel->private_data;
491 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
493 if (data->real && data->real->manager->get_stats) {
494 retval = (data->real->manager->get_stats)(data->real, stats);