ext4: add ext4_cache_flush for explicit cache flush
[lwext4.git] / fs_test / lwext4_mkfs.c
1 /*
2  * Copyright (c) 2015 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 <ext4_mkfs.h>
42 #include "../blockdev/linux/ext4_filedev.h"
43 #include "../blockdev/windows/io_raw.h"
44
45 /**@brief   Input stream name.*/
46 const char *input_name = NULL;
47
48 /**@brief   Block device handle.*/
49 static struct ext4_blockdev *bd;
50
51 /**@brief   Indicates that input is windows partition.*/
52 static bool winpart = false;
53
54 static int fs_type = F_SET_EXT4;
55
56 static struct ext4_fs fs;
57 static struct ext4_mkfs_info info = {
58         .block_size = 1024,
59 };
60
61 static bool verbose = false;
62
63 static const char *usage = "                                    \n\
64 Welcome in lwext4_mkfs tool .                                   \n\
65 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
66 Usage:                                                          \n\
67 [-i] --input   - input file name (or blockdevice)               \n\
68 [-w] --wpart   - windows partition mode                         \n\
69 [-v] --verbose - verbose mode                                   \n\
70 [-b] --block   - block size: 1024, 2048, 4096 (default 1024)    \n\
71 [-e] --ext     - fs type (ext2: 2, ext3: 3 ext4: 4))            \n\
72 \n";
73
74
75 static bool open_linux(void)
76 {
77         ext4_filedev_filename(input_name);
78         bd = ext4_filedev_get();
79         if (!bd) {
80                 printf("open_filedev: fail\n");
81                 return false;
82         }
83         return true;
84 }
85
86 static bool open_windows(void)
87 {
88 #ifdef WIN32
89         ext4_io_raw_filename(input_name);
90         bd = ext4_io_raw_dev_get();
91         if (!bd) {
92                 printf("open_winpartition: fail\n");
93                 return false;
94         }
95         return true;
96 #else
97         printf("open_winpartition: this mode should be used only under windows "
98                "!\n");
99         return false;
100 #endif
101 }
102
103 static bool open_filedev(void)
104 {
105         return winpart ? open_windows() : open_linux();
106 }
107
108 static bool parse_opt(int argc, char **argv)
109 {
110         int option_index = 0;
111         int c;
112
113         static struct option long_options[] = {
114             {"input", required_argument, 0, 'i'},
115             {"block", required_argument, 0, 'b'},
116             {"ext", required_argument, 0, 'e'},
117             {"wpart", no_argument, 0, 'w'},
118             {"verbose", no_argument, 0, 'v'},
119             {"version", no_argument, 0, 'x'},
120             {0, 0, 0, 0}};
121
122         while (-1 != (c = getopt_long(argc, argv, "i:b:e:wvx",
123                                       long_options, &option_index))) {
124
125                 switch (c) {
126                 case 'i':
127                         input_name = optarg;
128                         break;
129                 case 'b':
130                         info.block_size = atoi(optarg);
131                         break;
132                 case 'e':
133                         fs_type = atoi(optarg);
134                         break;
135                 case 'w':
136                         winpart = true;
137                         break;
138                 case 'v':
139                         verbose = true;
140                         break;
141                 case 'x':
142                         puts(VERSION);
143                         exit(0);
144                         break;
145                 default:
146                         printf("%s", usage);
147                         return false;
148                 }
149         }
150
151         switch (info.block_size) {
152         case 1024:
153         case 2048:
154         case 4096:
155                 break;
156         default:
157                 printf("parse_opt: block_size = %"PRIu32" unsupported\n",
158                                 info.block_size);
159                 return false;
160         }
161
162         switch (fs_type) {
163         case F_SET_EXT2:
164         case F_SET_EXT3:
165         case F_SET_EXT4:
166                 break;
167         default:
168                 printf("parse_opt: fs_type = %"PRIu32" unsupported\n", fs_type);
169                 return false;
170         }
171
172         return true;
173 }
174
175 int main(int argc, char **argv)
176 {
177         int r;
178         if (!parse_opt(argc, argv)){
179                 printf("parse_opt error\n");
180                 return EXIT_FAILURE;
181         }
182
183         if (!open_filedev()) {
184                 printf("open_filedev error\n");
185                 return EXIT_FAILURE;
186         }
187
188         if (verbose)
189                 ext4_dmask_set(DEBUG_ALL);
190
191         printf("ext4_mkfs: ext%d\n", fs_type);
192         r = ext4_mkfs(&fs, bd, &info, fs_type);
193         if (r != EOK) {
194                 printf("ext4_mkfs error: %d\n", r);
195                 return EXIT_FAILURE;
196         }
197
198         memset(&info, 0, sizeof(struct ext4_mkfs_info));
199         r = ext4_mkfs_read_info(bd, &info);
200         if (r != EOK) {
201                 printf("ext4_mkfs_read_info error: %d\n", r);
202                 return EXIT_FAILURE;
203         }
204
205         printf("Created filesystem with parameters:\n");
206         printf("Size: %"PRIu64"\n", info.len);
207         printf("Block size: %"PRIu32"\n", info.block_size);
208         printf("Blocks per group: %"PRIu32"\n", info.blocks_per_group);
209         printf("Inodes per group: %"PRIu32"\n", info.inodes_per_group);
210         printf("Inode size: %"PRIu32"\n", info.inode_size);
211         printf("Inodes: %"PRIu32"\n", info.inodes);
212         printf("Journal blocks: %"PRIu32"\n", info.journal_blocks);
213         printf("Features ro_compat: 0x%x\n", info.feat_ro_compat);
214         printf("Features compat: 0x%x\n", info.feat_compat);
215         printf("Features incompat: 0x%x\n", info.feat_incompat);
216         printf("BG desc reserve: %"PRIu32"\n", info.bg_desc_reserve_blocks);
217         printf("Descriptor size: %"PRIu32"\n",info.dsc_size);
218         printf("Label: %s\n", info.label);
219
220         printf("\nDone ...\n");
221         return EXIT_SUCCESS;
222 }