test_io.c revision 624e8ebe3058bad9af6e719b7f9e7afab7d3fe30
1/* 2 * test_io.c --- This is the Test I/O interface. 3 * 4 * Copyright (C) 1996 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#include <string.h> 14#if HAVE_UNISTD_H 15#include <unistd.h> 16#endif 17#include <fcntl.h> 18#include <time.h> 19#if HAVE_SYS_STAT_H 20#include <sys/stat.h> 21#endif 22#if HAVE_SYS_TYPES_H 23#include <sys/types.h> 24#endif 25#ifdef HAVE_SYS_PRCTL_H 26#include <sys/prctl.h> 27#else 28#define PR_GET_DUMPABLE 3 29#endif 30#if (!defined(HAVE_PRCTL) && defined(linux)) 31#include <sys/syscall.h> 32#endif 33 34#include "ext2_fs.h" 35#include "ext2fs.h" 36 37/* 38 * For checking structure magic numbers... 39 */ 40 41#define EXT2_CHECK_MAGIC(struct, code) \ 42 if ((struct)->magic != (code)) return (code) 43 44struct test_private_data { 45 int magic; 46 io_channel real; 47 int flags; 48 FILE *outfile; 49 unsigned long block; 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); 57}; 58 59static errcode_t test_open(const char *name, int flags, io_channel *channel); 60static errcode_t test_close(io_channel channel); 61static errcode_t test_set_blksize(io_channel channel, int blksize); 62static errcode_t test_read_blk(io_channel channel, unsigned long block, 63 int count, void *data); 64static errcode_t test_write_blk(io_channel channel, unsigned long block, 65 int count, const void *data); 66static errcode_t test_read_blk64(io_channel channel, unsigned long long block, 67 int count, void *data); 68static errcode_t test_write_blk64(io_channel channel, unsigned long long block, 69 int count, const void *data); 70static errcode_t test_flush(io_channel channel); 71static errcode_t test_write_byte(io_channel channel, unsigned long offset, 72 int count, const void *buf); 73static errcode_t test_set_option(io_channel channel, const char *option, 74 const char *arg); 75static errcode_t test_get_stats(io_channel channel, io_stats *stats); 76static errcode_t test_discard(io_channel channel, unsigned long long block, 77 unsigned long long count); 78 79static struct struct_io_manager struct_test_manager = { 80 EXT2_ET_MAGIC_IO_MANAGER, 81 "Test I/O Manager", 82 test_open, 83 test_close, 84 test_set_blksize, 85 test_read_blk, 86 test_write_blk, 87 test_flush, 88 test_write_byte, 89 test_set_option, 90 test_get_stats, 91 test_read_blk64, 92 test_write_blk64, 93 test_discard, 94}; 95 96io_manager test_io_manager = &struct_test_manager; 97 98/* 99 * These global variable can be set by the test program as 100 * necessary *before* calling test_open 101 */ 102io_manager test_io_backing_manager = 0; 103void (*test_io_cb_read_blk) 104 (unsigned long block, int count, errcode_t err) = 0; 105void (*test_io_cb_write_blk) 106 (unsigned long block, int count, errcode_t err) = 0; 107void (*test_io_cb_read_blk64) 108 (unsigned long long block, int count, errcode_t err) = 0; 109void (*test_io_cb_write_blk64) 110 (unsigned long long block, int count, errcode_t err) = 0; 111void (*test_io_cb_set_blksize) 112 (int blksize, errcode_t err) = 0; 113void (*test_io_cb_write_byte) 114 (unsigned long block, int count, errcode_t err) = 0; 115 116/* 117 * Test flags 118 */ 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 126 127static void test_dump_block(io_channel channel, 128 struct test_private_data *data, 129 unsigned long block, const void *buf) 130{ 131 const unsigned char *cp; 132 FILE *f = data->outfile; 133 int i; 134 unsigned long cksum = 0; 135 136 for (i=0, cp = buf; i < channel->block_size; i++, cp++) { 137 cksum += *cp; 138 } 139 fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum); 140 for (i=0, cp = buf; i < channel->block_size; i++, cp++) { 141 if ((i % 16) == 0) 142 fprintf(f, "%04x: ", i); 143 fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' '); 144 } 145} 146 147static void test_abort(io_channel channel, unsigned long block) 148{ 149 struct test_private_data *data; 150 FILE *f; 151 152 data = (struct test_private_data *) channel->private_data; 153 f = data->outfile; 154 test_flush(channel); 155 156 fprintf(f, "Aborting due to I/O to block %lu\n", block); 157 fflush(f); 158 abort(); 159} 160 161static char *safe_getenv(const char *arg) 162{ 163 if ((getuid() != geteuid()) || (getgid() != getegid())) 164 return NULL; 165#if HAVE_PRCTL 166 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) 167 return NULL; 168#else 169#if (defined(linux) && defined(SYS_prctl)) 170 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) 171 return NULL; 172#endif 173#endif 174 175#ifdef HAVE___SECURE_GETENV 176 return __secure_getenv(arg); 177#else 178 return getenv(arg); 179#endif 180} 181 182static errcode_t test_open(const char *name, int flags, io_channel *channel) 183{ 184 io_channel io = NULL; 185 struct test_private_data *data = NULL; 186 errcode_t retval; 187 char *value; 188 189 if (name == 0) 190 return EXT2_ET_BAD_DEVICE_NAME; 191 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io); 192 if (retval) 193 goto cleanup; 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); 197 if (retval) 198 goto cleanup; 199 io->manager = test_io_manager; 200 retval = ext2fs_get_mem(strlen(name)+1, &io->name); 201 if (retval) 202 goto cleanup; 203 204 strcpy(io->name, name); 205 io->private_data = data; 206 io->block_size = 1024; 207 io->read_error = 0; 208 io->write_error = 0; 209 io->refcount = 1; 210 211 memset(data, 0, sizeof(struct test_private_data)); 212 data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL; 213 if (test_io_backing_manager) { 214 retval = test_io_backing_manager->open(name, flags, 215 &data->real); 216 if (retval) 217 goto cleanup; 218 } else 219 data->real = 0; 220 data->read_blk = test_io_cb_read_blk; 221 data->write_blk = test_io_cb_write_blk; 222 data->set_blksize = test_io_cb_set_blksize; 223 data->write_byte = test_io_cb_write_byte; 224 data->read_blk64 = test_io_cb_read_blk64; 225 data->write_blk64 = test_io_cb_write_blk64; 226 227 data->outfile = NULL; 228 if ((value = safe_getenv("TEST_IO_LOGFILE")) != NULL) 229 data->outfile = fopen(value, "w"); 230 if (!data->outfile) 231 data->outfile = stderr; 232 233 data->flags = 0; 234 if ((value = safe_getenv("TEST_IO_FLAGS")) != NULL) 235 data->flags = strtoul(value, NULL, 0); 236 237 data->block = 0; 238 if ((value = safe_getenv("TEST_IO_BLOCK")) != NULL) 239 data->block = strtoul(value, NULL, 0); 240 241 data->read_abort_count = 0; 242 if ((value = safe_getenv("TEST_IO_READ_ABORT")) != NULL) 243 data->read_abort_count = strtoul(value, NULL, 0); 244 245 data->write_abort_count = 0; 246 if ((value = safe_getenv("TEST_IO_WRITE_ABORT")) != NULL) 247 data->write_abort_count = strtoul(value, NULL, 0); 248 249 *channel = io; 250 return 0; 251 252cleanup: 253 if (io) 254 ext2fs_free_mem(&io); 255 if (data) 256 ext2fs_free_mem(&data); 257 return retval; 258} 259 260static errcode_t test_close(io_channel channel) 261{ 262 struct test_private_data *data; 263 errcode_t retval = 0; 264 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); 268 269 if (--channel->refcount > 0) 270 return 0; 271 272 if (data->real) 273 retval = io_channel_close(data->real); 274 275 if (data->outfile && data->outfile != stderr) 276 fclose(data->outfile); 277 278 ext2fs_free_mem(&channel->private_data); 279 if (channel->name) 280 ext2fs_free_mem(&channel->name); 281 ext2fs_free_mem(&channel); 282 return retval; 283} 284 285static errcode_t test_set_blksize(io_channel channel, int blksize) 286{ 287 struct test_private_data *data; 288 errcode_t retval = 0; 289 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); 293 294 if (data->real) 295 retval = io_channel_set_blksize(data->real, blksize); 296 if (data->set_blksize) 297 data->set_blksize(blksize, retval); 298 if (data->flags & TEST_FLAG_SET_BLKSIZE) 299 fprintf(data->outfile, 300 "Test_io: set_blksize(%d) returned %s\n", 301 blksize, retval ? error_message(retval) : "OK"); 302 channel->block_size = blksize; 303 return retval; 304} 305 306 307static errcode_t test_read_blk(io_channel channel, unsigned long block, 308 int count, void *buf) 309{ 310 struct test_private_data *data; 311 errcode_t retval = 0; 312 313 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 314 data = (struct test_private_data *) channel->private_data; 315 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 316 317 if (data->real) 318 retval = io_channel_read_blk(data->real, block, count, buf); 319 if (data->read_blk) 320 data->read_blk(block, count, retval); 321 if (data->flags & TEST_FLAG_READ) 322 fprintf(data->outfile, 323 "Test_io: read_blk(%lu, %d) returned %s\n", 324 block, count, retval ? error_message(retval) : "OK"); 325 if (data->block && data->block == block) { 326 if (data->flags & TEST_FLAG_DUMP) 327 test_dump_block(channel, data, block, buf); 328 if (--data->read_abort_count == 0) 329 test_abort(channel, block); 330 } 331 return retval; 332} 333 334static errcode_t test_write_blk(io_channel channel, unsigned long block, 335 int count, const void *buf) 336{ 337 struct test_private_data *data; 338 errcode_t retval = 0; 339 340 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 341 data = (struct test_private_data *) channel->private_data; 342 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 343 344 if (data->real) 345 retval = io_channel_write_blk(data->real, block, count, buf); 346 if (data->write_blk) 347 data->write_blk(block, count, retval); 348 if (data->flags & TEST_FLAG_WRITE) 349 fprintf(data->outfile, 350 "Test_io: write_blk(%lu, %d) returned %s\n", 351 block, count, retval ? error_message(retval) : "OK"); 352 if (data->block && data->block == block) { 353 if (data->flags & TEST_FLAG_DUMP) 354 test_dump_block(channel, data, block, buf); 355 if (--data->write_abort_count == 0) 356 test_abort(channel, block); 357 } 358 return retval; 359} 360 361static errcode_t test_read_blk64(io_channel channel, unsigned long long block, 362 int count, void *buf) 363{ 364 struct test_private_data *data; 365 errcode_t retval = 0; 366 367 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 368 data = (struct test_private_data *) channel->private_data; 369 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 370 371 if (data->real) 372 retval = io_channel_read_blk64(data->real, block, count, buf); 373 if (data->read_blk64) 374 data->read_blk64(block, count, retval); 375 if (data->flags & TEST_FLAG_READ) 376 fprintf(data->outfile, 377 "Test_io: read_blk64(%llu, %d) returned %s\n", 378 block, count, retval ? error_message(retval) : "OK"); 379 if (data->block && data->block == block) { 380 if (data->flags & TEST_FLAG_DUMP) 381 test_dump_block(channel, data, block, buf); 382 if (--data->read_abort_count == 0) 383 test_abort(channel, block); 384 } 385 return retval; 386} 387 388static errcode_t test_write_blk64(io_channel channel, unsigned long long block, 389 int count, const void *buf) 390{ 391 struct test_private_data *data; 392 errcode_t retval = 0; 393 394 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 395 data = (struct test_private_data *) channel->private_data; 396 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 397 398 if (data->real) 399 retval = io_channel_write_blk64(data->real, block, count, buf); 400 if (data->write_blk64) 401 data->write_blk64(block, count, retval); 402 if (data->flags & TEST_FLAG_WRITE) 403 fprintf(data->outfile, 404 "Test_io: write_blk64(%llu, %d) returned %s\n", 405 block, count, retval ? error_message(retval) : "OK"); 406 if (data->block && data->block == block) { 407 if (data->flags & TEST_FLAG_DUMP) 408 test_dump_block(channel, data, block, buf); 409 if (--data->write_abort_count == 0) 410 test_abort(channel, block); 411 } 412 return retval; 413} 414 415static errcode_t test_write_byte(io_channel channel, unsigned long offset, 416 int count, const void *buf) 417{ 418 struct test_private_data *data; 419 errcode_t retval = 0; 420 421 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 422 data = (struct test_private_data *) channel->private_data; 423 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 424 425 if (data->real && data->real->manager->write_byte) 426 retval = io_channel_write_byte(data->real, offset, count, buf); 427 if (data->write_byte) 428 data->write_byte(offset, count, retval); 429 if (data->flags & TEST_FLAG_WRITE) 430 fprintf(data->outfile, 431 "Test_io: write_byte(%lu, %d) returned %s\n", 432 offset, count, retval ? error_message(retval) : "OK"); 433 return retval; 434} 435 436/* 437 * Flush data buffers to disk. 438 */ 439static errcode_t test_flush(io_channel channel) 440{ 441 struct test_private_data *data; 442 errcode_t retval = 0; 443 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); 447 448 if (data->real) 449 retval = io_channel_flush(data->real); 450 451 if (data->flags & TEST_FLAG_FLUSH) 452 fprintf(data->outfile, "Test_io: flush() returned %s\n", 453 retval ? error_message(retval) : "OK"); 454 455 return retval; 456} 457 458static errcode_t test_set_option(io_channel channel, const char *option, 459 const char *arg) 460{ 461 struct test_private_data *data; 462 errcode_t retval = 0; 463 464 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 465 data = (struct test_private_data *) channel->private_data; 466 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 467 468 469 if (data->flags & TEST_FLAG_SET_OPTION) 470 fprintf(data->outfile, "Test_io: set_option(%s, %s) ", 471 option, arg); 472 if (data->real && data->real->manager->set_option) { 473 retval = (data->real->manager->set_option)(data->real, 474 option, arg); 475 if (data->flags & TEST_FLAG_SET_OPTION) 476 fprintf(data->outfile, "returned %s\n", 477 retval ? error_message(retval) : "OK"); 478 } else { 479 if (data->flags & TEST_FLAG_SET_OPTION) 480 fprintf(data->outfile, "not implemented\n"); 481 } 482 return retval; 483} 484 485static errcode_t test_get_stats(io_channel channel, io_stats *stats) 486{ 487 struct test_private_data *data; 488 errcode_t retval = 0; 489 490 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); 491 data = (struct test_private_data *) channel->private_data; 492 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL); 493 494 if (data->real && data->real->manager->get_stats) { 495 retval = (data->real->manager->get_stats)(data->real, stats); 496 } 497 return retval; 498} 499 500static errcode_t test_discard(io_channel channel, unsigned long long block, 501 unsigned long long count) 502{ 503 struct test_private_data *data; 504 errcode_t retval = 0; 505 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); 509 510 if (data->real) 511 retval = io_channel_discard(data->real, block, count); 512 if (data->flags & TEST_FLAG_DISCARD) 513 fprintf(data->outfile, 514 "Test_io: discard(%llu, %llu) returned %s\n", 515 block, count, retval ? error_message(retval) : "OK"); 516 return retval; 517} 518