3
0

test_io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #if HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #if HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. /*
  27. * For checking structure magic numbers...
  28. */
  29. #define EXT2_CHECK_MAGIC(struct, code) \
  30. if ((struct)->magic != (code)) return (code)
  31. struct test_private_data {
  32. int magic;
  33. io_channel real;
  34. int flags;
  35. FILE *outfile;
  36. unsigned long block;
  37. int read_abort_count, write_abort_count;
  38. void (*read_blk)(unsigned long block, int count, errcode_t err);
  39. void (*write_blk)(unsigned long block, int count, errcode_t err);
  40. void (*set_blksize)(int blksize, errcode_t err);
  41. void (*write_byte)(unsigned long block, int count, errcode_t err);
  42. };
  43. static errcode_t test_open(const char *name, int flags, io_channel *channel);
  44. static errcode_t test_close(io_channel channel);
  45. static errcode_t test_set_blksize(io_channel channel, int blksize);
  46. static errcode_t test_read_blk(io_channel channel, unsigned long block,
  47. int count, void *data);
  48. static errcode_t test_write_blk(io_channel channel, unsigned long block,
  49. int count, const void *data);
  50. static errcode_t test_flush(io_channel channel);
  51. static errcode_t test_write_byte(io_channel channel, unsigned long offset,
  52. int count, const void *buf);
  53. static errcode_t test_set_option(io_channel channel, const char *option,
  54. const char *arg);
  55. static struct struct_io_manager struct_test_manager = {
  56. EXT2_ET_MAGIC_IO_MANAGER,
  57. "Test I/O Manager",
  58. test_open,
  59. test_close,
  60. test_set_blksize,
  61. test_read_blk,
  62. test_write_blk,
  63. test_flush,
  64. test_write_byte,
  65. test_set_option
  66. };
  67. io_manager test_io_manager = &struct_test_manager;
  68. /*
  69. * These global variable can be set by the test program as
  70. * necessary *before* calling test_open
  71. */
  72. io_manager test_io_backing_manager = 0;
  73. void (*test_io_cb_read_blk)
  74. (unsigned long block, int count, errcode_t err) = 0;
  75. void (*test_io_cb_write_blk)
  76. (unsigned long block, int count, errcode_t err) = 0;
  77. void (*test_io_cb_set_blksize)
  78. (int blksize, errcode_t err) = 0;
  79. void (*test_io_cb_write_byte)
  80. (unsigned long block, int count, errcode_t err) = 0;
  81. /*
  82. * Test flags
  83. */
  84. #define TEST_FLAG_READ 0x01
  85. #define TEST_FLAG_WRITE 0x02
  86. #define TEST_FLAG_SET_BLKSIZE 0x04
  87. #define TEST_FLAG_FLUSH 0x08
  88. #define TEST_FLAG_DUMP 0x10
  89. #define TEST_FLAG_SET_OPTION 0x20
  90. static void test_dump_block(io_channel channel,
  91. struct test_private_data *data,
  92. unsigned long block, const void *buf)
  93. {
  94. const unsigned char *cp;
  95. FILE *f = data->outfile;
  96. int i;
  97. unsigned long cksum = 0;
  98. for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
  99. cksum += *cp;
  100. }
  101. fprintf(f, "Contents of block %lu, checksum %08lu: \n", block, cksum);
  102. for (i=0, cp = buf; i < channel->block_size; i++, cp++) {
  103. if ((i % 16) == 0)
  104. fprintf(f, "%04x: ", i);
  105. fprintf(f, "%02x%c", *cp, ((i % 16) == 15) ? '\n' : ' ');
  106. }
  107. }
  108. static void test_abort(io_channel channel, unsigned long block)
  109. {
  110. struct test_private_data *data;
  111. FILE *f;
  112. data = (struct test_private_data *) channel->private_data;
  113. f = data->outfile;
  114. test_flush(channel);
  115. fprintf(f, "Aborting due to I/O to block %lu\n", block);
  116. fflush(f);
  117. abort();
  118. }
  119. static errcode_t test_open(const char *name, int flags, io_channel *channel)
  120. {
  121. io_channel io = NULL;
  122. struct test_private_data *data = NULL;
  123. errcode_t retval;
  124. char *value;
  125. if (name == 0)
  126. return EXT2_ET_BAD_DEVICE_NAME;
  127. retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
  128. if (retval)
  129. return retval;
  130. memset(io, 0, sizeof(struct struct_io_channel));
  131. io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
  132. retval = ext2fs_get_mem(sizeof(struct test_private_data), &data);
  133. if (retval) {
  134. retval = EXT2_ET_NO_MEMORY;
  135. goto cleanup;
  136. }
  137. io->manager = test_io_manager;
  138. retval = ext2fs_get_mem(strlen(name)+1, &io->name);
  139. if (retval)
  140. goto cleanup;
  141. strcpy(io->name, name);
  142. io->private_data = data;
  143. io->block_size = 1024;
  144. io->read_error = 0;
  145. io->write_error = 0;
  146. io->refcount = 1;
  147. memset(data, 0, sizeof(struct test_private_data));
  148. data->magic = EXT2_ET_MAGIC_TEST_IO_CHANNEL;
  149. if (test_io_backing_manager) {
  150. retval = test_io_backing_manager->open(name, flags,
  151. &data->real);
  152. if (retval)
  153. goto cleanup;
  154. } else
  155. data->real = 0;
  156. data->read_blk = test_io_cb_read_blk;
  157. data->write_blk = test_io_cb_write_blk;
  158. data->set_blksize = test_io_cb_set_blksize;
  159. data->write_byte = test_io_cb_write_byte;
  160. data->outfile = NULL;
  161. if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
  162. data->outfile = fopen(value, "w");
  163. if (!data->outfile)
  164. data->outfile = stderr;
  165. data->flags = 0;
  166. if ((value = getenv("TEST_IO_FLAGS")) != NULL)
  167. data->flags = strtoul(value, NULL, 0);
  168. data->block = 0;
  169. if ((value = getenv("TEST_IO_BLOCK")) != NULL)
  170. data->block = strtoul(value, NULL, 0);
  171. data->read_abort_count = 0;
  172. if ((value = getenv("TEST_IO_READ_ABORT")) != NULL)
  173. data->read_abort_count = strtoul(value, NULL, 0);
  174. data->write_abort_count = 0;
  175. if ((value = getenv("TEST_IO_WRITE_ABORT")) != NULL)
  176. data->write_abort_count = strtoul(value, NULL, 0);
  177. *channel = io;
  178. return 0;
  179. cleanup:
  180. if (io)
  181. ext2fs_free_mem(&io);
  182. if (data)
  183. ext2fs_free_mem(&data);
  184. return retval;
  185. }
  186. static errcode_t test_close(io_channel channel)
  187. {
  188. struct test_private_data *data;
  189. errcode_t retval = 0;
  190. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  191. data = (struct test_private_data *) channel->private_data;
  192. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  193. if (--channel->refcount > 0)
  194. return 0;
  195. if (data->real)
  196. retval = io_channel_close(data->real);
  197. if (data->outfile && data->outfile != stderr)
  198. fclose(data->outfile);
  199. ext2fs_free_mem(&channel->private_data);
  200. if (channel->name)
  201. ext2fs_free_mem(&channel->name);
  202. ext2fs_free_mem(&channel);
  203. return retval;
  204. }
  205. static errcode_t test_set_blksize(io_channel channel, int blksize)
  206. {
  207. struct test_private_data *data;
  208. errcode_t retval = 0;
  209. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  210. data = (struct test_private_data *) channel->private_data;
  211. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  212. if (data->real)
  213. retval = io_channel_set_blksize(data->real, blksize);
  214. if (data->set_blksize)
  215. data->set_blksize(blksize, retval);
  216. if (data->flags & TEST_FLAG_SET_BLKSIZE)
  217. fprintf(data->outfile,
  218. "Test_io: set_blksize(%d) returned %s\n",
  219. blksize, retval ? error_message(retval) : "OK");
  220. channel->block_size = blksize;
  221. return retval;
  222. }
  223. static errcode_t test_read_blk(io_channel channel, unsigned long block,
  224. int count, void *buf)
  225. {
  226. struct test_private_data *data;
  227. errcode_t retval = 0;
  228. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  229. data = (struct test_private_data *) channel->private_data;
  230. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  231. if (data->real)
  232. retval = io_channel_read_blk(data->real, block, count, buf);
  233. if (data->read_blk)
  234. data->read_blk(block, count, retval);
  235. if (data->flags & TEST_FLAG_READ)
  236. fprintf(data->outfile,
  237. "Test_io: read_blk(%lu, %d) returned %s\n",
  238. block, count, retval ? error_message(retval) : "OK");
  239. if (data->block && data->block == block) {
  240. if (data->flags & TEST_FLAG_DUMP)
  241. test_dump_block(channel, data, block, buf);
  242. if (--data->read_abort_count == 0)
  243. test_abort(channel, block);
  244. }
  245. return retval;
  246. }
  247. static errcode_t test_write_blk(io_channel channel, unsigned long block,
  248. int count, const void *buf)
  249. {
  250. struct test_private_data *data;
  251. errcode_t retval = 0;
  252. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  253. data = (struct test_private_data *) channel->private_data;
  254. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  255. if (data->real)
  256. retval = io_channel_write_blk(data->real, block, count, buf);
  257. if (data->write_blk)
  258. data->write_blk(block, count, retval);
  259. if (data->flags & TEST_FLAG_WRITE)
  260. fprintf(data->outfile,
  261. "Test_io: write_blk(%lu, %d) returned %s\n",
  262. block, count, retval ? error_message(retval) : "OK");
  263. if (data->block && data->block == block) {
  264. if (data->flags & TEST_FLAG_DUMP)
  265. test_dump_block(channel, data, block, buf);
  266. if (--data->write_abort_count == 0)
  267. test_abort(channel, block);
  268. }
  269. return retval;
  270. }
  271. static errcode_t test_write_byte(io_channel channel, unsigned long offset,
  272. int count, const void *buf)
  273. {
  274. struct test_private_data *data;
  275. errcode_t retval = 0;
  276. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  277. data = (struct test_private_data *) channel->private_data;
  278. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  279. if (data->real && data->real->manager->write_byte)
  280. retval = io_channel_write_byte(data->real, offset, count, buf);
  281. if (data->write_byte)
  282. data->write_byte(offset, count, retval);
  283. if (data->flags & TEST_FLAG_WRITE)
  284. fprintf(data->outfile,
  285. "Test_io: write_byte(%lu, %d) returned %s\n",
  286. offset, count, retval ? error_message(retval) : "OK");
  287. return retval;
  288. }
  289. /*
  290. * Flush data buffers to disk.
  291. */
  292. static errcode_t test_flush(io_channel channel)
  293. {
  294. struct test_private_data *data;
  295. errcode_t retval = 0;
  296. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  297. data = (struct test_private_data *) channel->private_data;
  298. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  299. if (data->real)
  300. retval = io_channel_flush(data->real);
  301. if (data->flags & TEST_FLAG_FLUSH)
  302. fprintf(data->outfile, "Test_io: flush() returned %s\n",
  303. retval ? error_message(retval) : "OK");
  304. return retval;
  305. }
  306. static errcode_t test_set_option(io_channel channel, const char *option,
  307. const char *arg)
  308. {
  309. struct test_private_data *data;
  310. errcode_t retval = 0;
  311. EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
  312. data = (struct test_private_data *) channel->private_data;
  313. EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
  314. if (data->flags & TEST_FLAG_SET_OPTION)
  315. fprintf(data->outfile, "Test_io: set_option(%s, %s) ",
  316. option, arg);
  317. if (data->real && data->real->manager->set_option) {
  318. retval = (data->real->manager->set_option)(data->real,
  319. option, arg);
  320. if (data->flags & TEST_FLAG_SET_OPTION)
  321. fprintf(data->outfile, "returned %s\n",
  322. retval ? error_message(retval) : "OK");
  323. } else {
  324. if (data->flags & TEST_FLAG_SET_OPTION)
  325. fprintf(data->outfile, "not implemented\n");
  326. }
  327. return retval;
  328. }