test_io.c 10 KB

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