lwext4_generic.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <getopt.h>
  33. #include <stdbool.h>
  34. #include <inttypes.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <ext4.h>
  39. #include "../blockdev/linux/file_dev.h"
  40. #include "../blockdev/windows/file_windows.h"
  41. #include "common/test_lwext4.h"
  42. #ifdef WIN32
  43. #include <windows.h>
  44. #endif
  45. /**@brief Input stream name.*/
  46. char input_name[128] = "ext_images/ext2";
  47. /**@brief Read-write size*/
  48. static int rw_szie = 1024 * 1024;
  49. /**@brief Read-write size*/
  50. static int rw_count = 10;
  51. /**@brief Directory test count*/
  52. static int dir_cnt = 0;
  53. /**@brief Cleanup after test.*/
  54. static bool cleanup_flag = false;
  55. /**@brief Block device stats.*/
  56. static bool bstat = false;
  57. /**@brief Superblock stats.*/
  58. static bool sbstat = false;
  59. /**@brief Indicates that input is windows partition.*/
  60. static bool winpart = false;
  61. /**@brief Verbose mode*/
  62. static bool verbose = 0;
  63. /**@brief Block device handle.*/
  64. static struct ext4_blockdev *bd;
  65. /**@brief Block cache handle.*/
  66. static struct ext4_bcache *bc;
  67. static const char *usage = " \n\
  68. Welcome in ext4 generic demo. \n\
  69. Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
  70. Usage: \n\
  71. [-i] --input - input file (default = ext2) \n\
  72. [-w] --rw_size - single R/W size (default = 1024 * 1024) \n\
  73. [-c] --rw_count - R/W count (default = 10) \n\
  74. [-d] --dirs - directory test count (default = 0) \n\
  75. [-l] --clean - clean up after test \n\
  76. [-b] --bstat - block device stats \n\
  77. [-t] --sbstat - superblock stats \n\
  78. [-w] --wpart - windows partition mode \n\
  79. \n";
  80. void io_timings_clear(void)
  81. {
  82. }
  83. const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
  84. {
  85. return NULL;
  86. }
  87. uint32_t tim_get_ms(void)
  88. {
  89. struct timeval t;
  90. gettimeofday(&t, NULL);
  91. return (t.tv_sec * 1000) + (t.tv_usec / 1000);
  92. }
  93. uint64_t tim_get_us(void)
  94. {
  95. struct timeval t;
  96. gettimeofday(&t, NULL);
  97. return (t.tv_sec * 1000000) + (t.tv_usec);
  98. }
  99. static bool open_linux(void)
  100. {
  101. file_dev_name_set(input_name);
  102. bd = file_dev_get();
  103. if (!bd) {
  104. printf("open_filedev: fail\n");
  105. return false;
  106. }
  107. return true;
  108. }
  109. static bool open_windows(void)
  110. {
  111. #ifdef WIN32
  112. file_windows_name_set(input_name);
  113. bd = file_windows_dev_get();
  114. if (!bd) {
  115. printf("open_winpartition: fail\n");
  116. return false;
  117. }
  118. return true;
  119. #else
  120. printf("open_winpartition: this mode should be used only under windows "
  121. "!\n");
  122. return false;
  123. #endif
  124. }
  125. static bool open_filedev(void)
  126. {
  127. return winpart ? open_windows() : open_linux();
  128. }
  129. static bool parse_opt(int argc, char **argv)
  130. {
  131. int option_index = 0;
  132. int c;
  133. static struct option long_options[] = {
  134. {"input", required_argument, 0, 'i'},
  135. {"rw_size", required_argument, 0, 's'},
  136. {"rw_count", required_argument, 0, 'c'},
  137. {"dirs", required_argument, 0, 'd'},
  138. {"clean", no_argument, 0, 'l'},
  139. {"bstat", no_argument, 0, 'b'},
  140. {"sbstat", no_argument, 0, 't'},
  141. {"wpart", no_argument, 0, 'w'},
  142. {"verbose", no_argument, 0, 'v'},
  143. {"version", no_argument, 0, 'x'},
  144. {0, 0, 0, 0}};
  145. while (-1 != (c = getopt_long(argc, argv, "i:s:c:q:d:lbtwvx",
  146. long_options, &option_index))) {
  147. switch (c) {
  148. case 'i':
  149. strcpy(input_name, optarg);
  150. break;
  151. case 's':
  152. rw_szie = atoi(optarg);
  153. break;
  154. case 'c':
  155. rw_count = atoi(optarg);
  156. break;
  157. case 'd':
  158. dir_cnt = atoi(optarg);
  159. break;
  160. case 'l':
  161. cleanup_flag = true;
  162. break;
  163. case 'b':
  164. bstat = true;
  165. break;
  166. case 't':
  167. sbstat = true;
  168. break;
  169. case 'w':
  170. winpart = true;
  171. break;
  172. case 'v':
  173. verbose = true;
  174. break;
  175. case 'x':
  176. puts(VERSION);
  177. exit(0);
  178. break;
  179. default:
  180. printf("%s", usage);
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. int main(int argc, char **argv)
  187. {
  188. if (!parse_opt(argc, argv))
  189. return EXIT_FAILURE;
  190. printf("ext4_generic\n");
  191. printf("test conditions:\n");
  192. printf("\timput name: %s\n", input_name);
  193. printf("\trw size: %d\n", rw_szie);
  194. printf("\trw count: %d\n", rw_count);
  195. if (!open_filedev()) {
  196. printf("open_filedev error\n");
  197. return EXIT_FAILURE;
  198. }
  199. if (verbose)
  200. ext4_dmask_set(DEBUG_ALL);
  201. if (!test_lwext4_mount(bd, bc))
  202. return EXIT_FAILURE;
  203. test_lwext4_cleanup();
  204. if (sbstat)
  205. test_lwext4_mp_stats();
  206. test_lwext4_dir_ls("/mp/");
  207. fflush(stdout);
  208. if (!test_lwext4_dir_test(dir_cnt))
  209. return EXIT_FAILURE;
  210. fflush(stdout);
  211. uint8_t *rw_buff = malloc(rw_szie);
  212. if (!rw_buff) {
  213. free(rw_buff);
  214. return EXIT_FAILURE;
  215. }
  216. if (!test_lwext4_file_test(rw_buff, rw_szie, rw_count)) {
  217. free(rw_buff);
  218. return EXIT_FAILURE;
  219. }
  220. free(rw_buff);
  221. fflush(stdout);
  222. test_lwext4_dir_ls("/mp/");
  223. if (sbstat)
  224. test_lwext4_mp_stats();
  225. if (cleanup_flag)
  226. test_lwext4_cleanup();
  227. if (bstat)
  228. test_lwext4_block_stats();
  229. if (!test_lwext4_umount())
  230. return EXIT_FAILURE;
  231. printf("\ntest finished\n");
  232. return EXIT_SUCCESS;
  233. }