Move all test modules to fs_test directory
[lwext4.git] / fs_test / lwext4_generic.c
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
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <stdbool.h>
35 #include <inttypes.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sys/time.h>
39
40 #include <ext4.h>
41 #include "../blockdev/linux/ext4_filedev.h"
42 #include "../blockdev/windows/io_raw.h"
43 #include "common/test_lwext4.h"
44
45 #ifdef WIN32
46 #include <windows.h>
47 #endif
48
49 /**@brief   Input stream name.*/
50 char input_name[128] = "ext2";
51
52 /**@brief   Read-write size*/
53 static int rw_szie = 1024 * 1024;
54
55 /**@brief   Read-write size*/
56 static int rw_count = 10;
57
58 /**@brief   Directory test count*/
59 static int dir_cnt = 0;
60
61 /**@brief   Static or dynamic cache mode*/
62 static bool cache_mode = true;
63
64 /**@brief   Cleanup after test.*/
65 static bool cleanup_flag = false;
66
67 /**@brief   Block device stats.*/
68 static bool bstat = false;
69
70 /**@brief   Superblock stats.*/
71 static bool sbstat = false;
72
73 /**@brief   Indicates that input is windows partition.*/
74 static bool winpart = false;
75
76 /**@brief   Block device handle.*/
77 static struct ext4_blockdev *bd;
78
79 /**@brief   Static cache instance*/
80 EXT4_BCACHE_STATIC_INSTANCE(_lwext4_cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024);
81
82 /**@brief   Block cache handle.*/
83 static struct ext4_bcache *bc = &_lwext4_cache;
84
85 static const char *usage = "                                    \n\
86 Welcome in ext4 generic demo.                                   \n\
87 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
88 Usage:                                                          \n\
89 [-i] --input    - input file         (default = ext2)           \n\
90 [-w] --rw_size  - single R/W size    (default = 1024 * 1024)    \n\
91 [-c] --rw_count - R/W count          (default = 10)             \n\
92 [-a] --cache  - 0 static, 1 dynamic  (default = 1)              \n\
93 [-d] --dirs   - directory test count (default = 0)              \n\
94 [-l] --clean  - clean up after test                             \n\
95 [-b] --bstat  - block device stats                              \n\
96 [-t] --sbstat - superblock stats                                \n\
97 [-w] --wpart  - windows partition mode                          \n\
98 \n";
99
100 void io_timings_clear(void)
101 {
102 }
103
104 const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
105 {
106         return NULL;
107 }
108
109 uint32_t tim_get_ms(void)
110 {
111         struct timeval t;
112         gettimeofday(&t, NULL);
113         return (t.tv_sec * 1000) + (t.tv_usec / 1000);
114 }
115
116 uint64_t tim_get_us(void)
117 {
118         struct timeval t;
119         gettimeofday(&t, NULL);
120         return (t.tv_sec * 1000000) + (t.tv_usec);
121 }
122
123 static bool open_linux(void)
124 {
125         ext4_filedev_filename(input_name);
126         bd = ext4_filedev_get();
127         if (!bd) {
128                 printf("open_filedev: fail\n");
129                 return false;
130         }
131         return true;
132 }
133
134 static bool open_windows(void)
135 {
136 #ifdef WIN32
137         ext4_io_raw_filename(input_name);
138         bd = ext4_io_raw_dev_get();
139         if (!bd) {
140                 printf("open_winpartition: fail\n");
141                 return false;
142         }
143         return true;
144 #else
145         printf("open_winpartition: this mode should be used only under windows "
146                "!\n");
147         return false;
148 #endif
149 }
150
151
152 static bool parse_opt(int argc, char **argv)
153 {
154         int option_index = 0;
155         int c;
156
157         static struct option long_options[] = {
158             {"input", required_argument, 0, 'i'},
159             {"rw_size", required_argument, 0, 's'},
160             {"rw_count", required_argument, 0, 'c'},
161             {"cache", required_argument, 0, 'a'},
162             {"dirs", required_argument, 0, 'd'},
163             {"clean", no_argument, 0, 'l'},
164             {"bstat", no_argument, 0, 'b'},
165             {"sbstat", no_argument, 0, 't'},
166             {"wpart", no_argument, 0, 'w'},
167             {0, 0, 0, 0}};
168
169         while (-1 != (c = getopt_long(argc, argv, "i:s:c:q:d:lbtw",
170                                       long_options, &option_index))) {
171
172                 switch (c) {
173                 case 'i':
174                         strcpy(input_name, optarg);
175                         break;
176                 case 's':
177                         rw_szie = atoi(optarg);
178                         break;
179                 case 'c':
180                         rw_count = atoi(optarg);
181                         break;
182                 case 'a':
183                         cache_mode = atoi(optarg);
184                         break;
185                 case 'd':
186                         dir_cnt = atoi(optarg);
187                         break;
188                 case 'l':
189                         cleanup_flag = true;
190                         break;
191                 case 'b':
192                         bstat = true;
193                         break;
194                 case 't':
195                         sbstat = true;
196                         break;
197                 case 'w':
198                         winpart = true;
199                         break;
200                 default:
201                         printf("%s", usage);
202                         return false;
203                 }
204         }
205         return true;
206 }
207
208 int main(int argc, char **argv)
209 {
210         if (!parse_opt(argc, argv))
211                 return EXIT_FAILURE;
212
213         printf("test conditions:\n");
214         printf("\timput name: %s\n", input_name);
215         printf("\trw size: %d\n", rw_szie);
216         printf("\trw count: %d\n", rw_count);
217         printf("\tcache mode: %s\n", cache_mode ? "dynamic" : "static");
218
219         if (winpart) {
220                 if (!open_windows())
221                         return EXIT_FAILURE;
222         } else {
223                 if (!open_linux())
224                         return EXIT_FAILURE;
225         }
226
227
228         if (!test_lwext4_mount(bd, bc))
229                 return EXIT_FAILURE;
230
231         test_lwext4_cleanup();
232
233         if (sbstat)
234                 test_lwext4_mp_stats();
235
236         test_lwext4_dir_ls("/mp/");
237         fflush(stdout);
238         if (!test_lwext4_dir_test(dir_cnt))
239                 return EXIT_FAILURE;
240
241         fflush(stdout);
242         uint8_t *rw_buff = malloc(rw_szie);
243         if (!rw_buff)
244                 return EXIT_FAILURE;
245         if (!test_lwext4_file_test(rw_buff, rw_szie, rw_count))
246                 return EXIT_FAILURE;
247
248         fflush(stdout);
249         test_lwext4_dir_ls("/mp/");
250
251         if (sbstat)
252                 test_lwext4_mp_stats();
253
254         if (cleanup_flag)
255                 test_lwext4_cleanup();
256
257         if (bstat)
258                 test_lwext4_block_stats();
259
260         if (!test_lwext4_umount())
261                 return EXIT_FAILURE;
262
263         printf("\ntest finished\n");
264         return EXIT_SUCCESS;
265 }