ext4_fs_set_xattr, ext4_fs_get_xattr and ext4_fs_remove_xattr introduced.(EXPERIMENTA...
[lwext4.git] / demos / generic / 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 "../../blockdev/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   File write buffer*/
77 static uint8_t *wr_buff;
78
79 /**@brief   File read buffer.*/
80 static uint8_t *rd_buff;
81
82 /**@brief   Block device handle.*/
83 static struct ext4_blockdev *bd;
84
85 /**@brief   Static cache instance*/
86 EXT4_BCACHE_STATIC_INSTANCE(_lwext4_cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024);
87
88 /**@brief   Block cache handle.*/
89 static struct ext4_bcache *bc = &_lwext4_cache;
90
91 static const char *usage = "                                    \n\
92 Welcome in ext4 generic demo.                                   \n\
93 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
94 Usage:                                                          \n\
95     --i   - input file              (default = ext2)            \n\
96     --rws - single R/W size         (default = 1024 * 1024)     \n\
97     --rwc - R/W count               (default = 10)              \n\
98     --cache  - 0 static, 1 dynamic  (default = 1)               \n\
99     --dirs   - directory test count (default = 0)               \n\
100     --clean  - clean up after test                              \n\
101     --bstat  - block device stats                               \n\
102     --sbstat - superblock stats                                 \n\
103     --wpart  - windows partition mode                           \n\
104 \n";
105
106 void io_timings_clear(void)
107 {
108 }
109
110 const struct ext4_io_stats *io_timings_get(uint32_t time_sum_ms)
111 {
112         return NULL;
113 }
114
115 uint32_t tim_get_ms(void)
116 {
117         struct timeval t;
118         gettimeofday(&t, NULL);
119         return (t.tv_sec * 1000) + (t.tv_usec / 1000);
120 }
121
122 uint64_t tim_get_us(void)
123 {
124         struct timeval t;
125         gettimeofday(&t, NULL);
126         return (t.tv_sec * 1000000) + (t.tv_usec);
127 }
128
129 static bool open_linux(void)
130 {
131         ext4_filedev_filename(input_name);
132         bd = ext4_filedev_get();
133         if (!bd) {
134                 printf("open_filedev: fail\n");
135                 return false;
136         }
137         return true;
138 }
139
140 static bool open_windows(void)
141 {
142 #ifdef WIN32
143         ext4_io_raw_filename(input_name);
144         bd = ext4_io_raw_dev_get();
145         if (!bd) {
146                 printf("open_winpartition: fail\n");
147                 return false;
148         }
149         return true;
150 #else
151         printf("open_winpartition: this mode should be used only under windows "
152                "!\n");
153         return false;
154 #endif
155 }
156
157
158 static bool parse_opt(int argc, char **argv)
159 {
160         int option_index = 0;
161         int c;
162
163         static struct option long_options[] = {
164             {"in", required_argument, 0, 'a'},
165             {"rws", required_argument, 0, 'b'},
166             {"rwc", required_argument, 0, 'c'},
167             {"cache", required_argument, 0, 'd'},
168             {"dirs", required_argument, 0, 'e'},
169             {"clean", no_argument, 0, 'f'},
170             {"bstat", no_argument, 0, 'g'},
171             {"sbstat", no_argument, 0, 'h'},
172             {"wpart", no_argument, 0, 'i'},
173             {0, 0, 0, 0}};
174
175         while (-1 != (c = getopt_long(argc, argv, "a:b:c:d:e:fghi",
176                                       long_options, &option_index))) {
177
178                 switch (c) {
179                 case 'a':
180                         strcpy(input_name, optarg);
181                         break;
182                 case 'b':
183                         rw_szie = atoi(optarg);
184                         break;
185                 case 'c':
186                         rw_count = atoi(optarg);
187                         break;
188                 case 'd':
189                         cache_mode = atoi(optarg);
190                         break;
191                 case 'e':
192                         dir_cnt = atoi(optarg);
193                         break;
194                 case 'f':
195                         cleanup_flag = true;
196                         break;
197                 case 'g':
198                         bstat = true;
199                         break;
200                 case 'h':
201                         sbstat = true;
202                         break;
203                 case 'i':
204                         winpart = true;
205                         break;
206                 default:
207                         printf("%s", usage);
208                         return false;
209                 }
210         }
211         return true;
212 }
213
214 int main(int argc, char **argv)
215 {
216         if (!parse_opt(argc, argv))
217                 return EXIT_FAILURE;
218
219         printf("test conditions:\n");
220         printf("\timput name: %s\n", input_name);
221         printf("\trw size: %d\n", rw_szie);
222         printf("\trw count: %d\n", rw_count);
223         printf("\tcache mode: %s\n", cache_mode ? "dynamic" : "static");
224
225         if (winpart) {
226                 if (!open_windows())
227                         return EXIT_FAILURE;
228         } else {
229                 if (!open_linux())
230                         return EXIT_FAILURE;
231         }
232
233
234         if (!test_lwext4_mount(bd, bc))
235                 return EXIT_FAILURE;
236
237         test_lwext4_cleanup();
238
239         if (sbstat)
240                 test_lwext4_mp_stats();
241
242         test_lwext4_dir_ls("/mp/");
243         fflush(stdout);
244         if (!test_lwext4_dir_test(dir_cnt))
245                 return EXIT_FAILURE;
246
247         fflush(stdout);
248         if (!test_lwext4_file_test(rw_count, rw_szie))
249                 return EXIT_FAILURE;
250
251         fflush(stdout);
252         test_lwext4_dir_ls("/mp/");
253
254         if (sbstat)
255                 test_lwext4_mp_stats();
256
257         if (cleanup_flag)
258                 test_lwext4_cleanup();
259
260         if (bstat)
261                 test_lwext4_block_stats();
262
263         if (!test_lwext4_umount())
264                 return EXIT_FAILURE;
265
266         printf("\ntest finished\n");
267         return EXIT_SUCCESS;
268 }