2 * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/types.h>
52 #include "../blockdev/linux/ext4_filedev.h"
53 #include "../blockdev/windows/io_raw.h"
56 static int winsock_init(void);
57 static void winsock_fini(void);
58 static char *entry_to_str(uint8_t type);
63 #define MAX_RW_BUFFER (1024 * 1024)
64 #define RW_BUFFER_PATERN ('x')
66 /**@brief Default connection port*/
67 static int connection_port = 1234;
69 /**@brief Default filesystem filename.*/
70 static char *ext4_fname = "ext2";
72 /**@brief Verbose mode*/
73 static bool verbose = false;
75 /**@brief Winpart mode*/
76 static bool winpart = false;
78 /**@brief Blockdev handle*/
79 static struct ext4_blockdev *bd;
81 static bool cache_wb = false;
83 static char read_buffer[MAX_RW_BUFFER];
84 static char write_buffer[MAX_RW_BUFFER];
86 static const char *usage = " \n\
87 Welcome in lwext4_server. \n\
88 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
90 --image (-i) - ext2/3/4 image file \n\
91 --port (-p) - server port \n\
92 --verbose (-v) - verbose mode \n\
93 --winpart (-w) - windows_partition mode \n\
94 --cache_wb (-c) - cache writeback_mode \n\
97 /**@brief Open file instance descriptor.*/
103 /**@brief Open directory instance descriptor.*/
109 /**@brief Library call opcode.*/
110 struct lwext4_op_codes {
114 /**@brief Library call wraper.*/
116 int (*lwext4_call)(char *p);
120 static struct lwext4_files file_tab[MAX_FILES];
123 static struct lwext4_dirs dir_tab[MAX_DIRS];
126 static struct lwext4_op_codes op_codes[] = {
156 int _device_register(char *p);
158 int _umount(char *p);
159 int _mount_point_stats(char *p);
160 int _cache_write_back(char *p);
161 int _fremove(char *p);
163 int _fclose(char *p);
165 int _fwrite(char *p);
169 int _dir_rm(char *p);
170 int _dir_mk(char *p);
171 int _dir_open(char *p);
172 int _dir_close(char *p);
173 int _dir_close(char *p);
174 int _dir_entry_get(char *p);
176 int _multi_fcreate(char *p);
177 int _multi_fwrite(char *p);
178 int _multi_fread(char *p);
179 int _multi_fremove(char *p);
180 int _multi_dcreate(char *p);
181 int _multi_dremove(char *p);
182 int _stats_save(char *p);
183 int _stats_check(char *p);
186 static struct lwext4_call op_call[] = {
187 _device_register, /*PARAMS(3): 0 cache_mode dev_name */
188 _mount, /*PARAMS(2): dev_name mount_point */
189 _umount, /*PARAMS(1): mount_point */
190 _mount_point_stats, /*PARAMS(2): mount_point, 0 */
191 _cache_write_back, /*PARAMS(2): mount_point, en */
192 _fremove, /*PARAMS(1): path */
193 _fopen, /*PARAMS(2): fid path flags */
194 _fclose, /*PARAMS(1): fid */
195 _fread, /*PARAMS(4): fid 0 len 0 */
196 _fwrite, /*PARAMS(4): fid 0 len 0 */
197 _fseek, /*PARAMS(2): fid off origin */
198 _ftell, /*PARAMS(2): fid exp */
199 _fsize, /*PARAMS(2): fid exp */
200 _dir_rm, /*PARAMS(1): path */
201 _dir_mk, /*PARAMS(1): path */
202 _dir_open, /*PARAMS(2): did, path */
203 _dir_close, /*PARAMS(1): did */
204 _dir_entry_get, /*PARAMS(2): did, exp */
206 _multi_fcreate, /*PARAMS(3): path prefix cnt */
207 _multi_fwrite, /*PARAMS(4): path prefix cnt size */
208 _multi_fread, /*PARAMS(4): path prefix cnt size */
209 _multi_fremove, /*PARAMS(2): path prefix cnt */
210 _multi_dcreate, /*PARAMS(3): path prefix cnt */
211 _multi_dremove, /*PARAMS(2): path prefix */
212 _stats_save, /*PARAMS(1): path */
213 _stats_check, /*PARAMS(1): path */
216 static clock_t get_ms(void)
219 gettimeofday(&t, NULL);
220 return (t.tv_sec * 1000) + (t.tv_usec / 1000);
224 static int exec_op_code(char *opcode)
229 for (i = 0; i < sizeof(op_codes) / sizeof(op_codes[0]); ++i) {
231 if (strncmp(op_codes[i].func, opcode, strlen(op_codes[i].func)))
234 if (opcode[strlen(op_codes[i].func)] != ' ')
237 printf("%s\n", opcode);
238 opcode += strlen(op_codes[i].func);
241 clock_t t = get_ms();
242 r = op_call[i].lwext4_call(opcode);
244 printf("rc: %d, time: %ums\n", r, (unsigned int)(get_ms() - t));
252 static int server_open(void)
255 struct sockaddr_in serv_addr;
257 memset(&serv_addr, 0, sizeof(serv_addr));
259 if (winsock_init() < 0) {
260 printf("winsock_init() error\n");
264 fd = socket(AF_INET, SOCK_STREAM, 0);
266 printf("socket() error: %s\n", strerror(errno));
271 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes,
273 printf("setsockopt() error: %s\n", strerror(errno));
277 serv_addr.sin_family = AF_INET;
278 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
279 serv_addr.sin_port = htons(connection_port);
281 if (bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
282 printf("bind() error: %s\n", strerror(errno));
287 printf("listen() error: %s\n", strerror(errno));
294 static bool parse_opt(int argc, char **argv)
296 int option_index = 0;
299 static struct option long_options[] = {
300 {"image", required_argument, 0, 'i'},
301 {"port", required_argument, 0, 'p'},
302 {"verbose", no_argument, 0, 'v'},
303 {"winpart", no_argument, 0, 'w'},
304 {"cache_wb", no_argument, 0, 'c'},
305 {"version", no_argument, 0, 'x'},
308 while (-1 != (c = getopt_long(argc, argv, "i:p:vcwx", long_options,
316 connection_port = atoi(optarg);
339 int main(int argc, char *argv[])
346 if (!parse_opt(argc, argv))
349 listenfd = server_open();
351 printf("lwext4_server: listening on port: %d\n", connection_port);
353 memset(write_buffer, RW_BUFFER_PATERN, MAX_RW_BUFFER);
355 connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
357 n = recv(connfd, op_code, sizeof(op_code), 0);
360 printf("recv() error: %s fd = %d\n", strerror(errno),
367 int r = exec_op_code(op_code);
369 n = send(connfd, (void *)&r, sizeof(r), 0);
371 printf("send() error: %s fd = %d\n", strerror(errno),
383 int _device_register(char *p)
389 if (sscanf(p, "%d %d %s", &dev, &cache_mode, dev_name) != 3) {
390 printf("Param list error\n");
396 ext4_io_raw_filename(ext4_fname);
397 bd = ext4_io_raw_dev_get();
402 ext4_filedev_filename(ext4_fname);
403 bd = ext4_filedev_get();
405 return ext4_device_register(bd, 0, dev_name);
411 char mount_point[32];
414 if (sscanf(p, "%s %s", dev_name, mount_point) != 2) {
415 printf("Param list error\n");
420 ext4_dmask_set(DEBUG_ALL);
422 rc = ext4_mount(dev_name, mount_point, false);
426 rc = ext4_recover(mount_point);
427 if (rc != EOK && rc != ENOTSUP)
430 rc = ext4_journal_start(mount_point);
435 ext4_cache_write_back(mount_point, 1);
441 char mount_point[32];
444 if (sscanf(p, "%s", mount_point) != 1) {
445 printf("Param list error\n");
450 ext4_cache_write_back(mount_point, 0);
452 rc = ext4_journal_stop(mount_point);
456 rc = ext4_umount(mount_point);
463 int _mount_point_stats(char *p)
465 char mount_point[32];
468 struct ext4_mount_stats stats;
470 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
471 printf("Param list error\n");
475 rc = ext4_mount_point_stats(mount_point, &stats);
481 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
482 printf("\tfree_inodes_count = %" PRIu32"\n",
483 stats.free_inodes_count);
484 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
485 printf("\tfree_blocks_count = %" PRIu64"\n",
486 stats.free_blocks_count);
487 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
488 printf("\tblock_group_count = %" PRIu32"\n",
489 stats.block_group_count);
490 printf("\tblocks_per_group = %" PRIu32"\n",
491 stats.blocks_per_group);
492 printf("\tinodes_per_group = %" PRIu32"\n",
493 stats.inodes_per_group);
494 printf("\tvolume_name = %s\n", stats.volume_name);
500 int _cache_write_back(char *p)
502 char mount_point[32];
505 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
506 printf("Param list error\n");
510 return ext4_cache_write_back(mount_point, en);
513 int _fremove(char *p)
517 if (sscanf(p, "%s", path) != 1) {
518 printf("Param list error\n");
522 return ext4_fremove(path);
532 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
533 printf("Param list error\n");
537 if (!(fid < MAX_FILES)) {
538 printf("File id too big\n");
542 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
545 strcpy(file_tab[fid].name, path);
555 if (sscanf(p, "%d", &fid) != 1) {
556 printf("Param list error\n");
560 if (!(fid < MAX_FILES)) {
561 printf("File id too big\n");
565 if (file_tab[fid].name[0] == 0) {
566 printf("File id empty\n");
570 rc = ext4_fclose(&file_tab[fid].fd);
573 file_tab[fid].name[0] = 0;
586 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
587 printf("Param list error\n");
591 if (!(fid < MAX_FILES)) {
592 printf("File id too big\n");
596 if (file_tab[fid].name[0] == 0) {
597 printf("File id empty\n");
602 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
604 memset(read_buffer, 0, MAX_RW_BUFFER);
605 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
611 printf("Read count error\n");
615 if (memcmp(read_buffer, write_buffer, d)) {
616 printf("Read compare error\n");
635 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
636 printf("Param list error\n");
640 if (!(fid < MAX_FILES)) {
641 printf("File id too big\n");
645 if (file_tab[fid].name[0] == 0) {
646 printf("File id empty\n");
651 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
652 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
658 printf("Write count error\n");
674 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
675 printf("Param list error\n");
679 if (!(fid < MAX_FILES)) {
680 printf("File id too big\n");
684 if (file_tab[fid].name[0] == 0) {
685 printf("File id empty\n");
689 return ext4_fseek(&file_tab[fid].fd, off, origin);
697 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
698 printf("Param list error\n");
702 if (!(fid < MAX_FILES)) {
703 printf("File id too big\n");
707 if (file_tab[fid].name[0] == 0) {
708 printf("File id empty\n");
712 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
713 printf("Expected filepos error\n");
725 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
726 printf("Param list error\n");
730 if (!(fid < MAX_FILES)) {
731 printf("File id too big\n");
735 if (file_tab[fid].name[0] == 0) {
736 printf("File id empty\n");
740 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
741 printf("Expected filesize error\n");
752 if (sscanf(p, "%s", path) != 1) {
753 printf("Param list error\n");
757 return ext4_dir_rm(path);
764 if (sscanf(p, "%s", path) != 1) {
765 printf("Param list error\n");
769 return ext4_dir_mk(path);
772 int _dir_open(char *p)
778 if (sscanf(p, "%d %s", &did, path) != 2) {
779 printf("Param list error\n");
783 if (!(did < MAX_DIRS)) {
784 printf("Dir id too big\n");
788 rc = ext4_dir_open(&dir_tab[did].fd, path);
791 strcpy(dir_tab[did].name, path);
796 int _dir_close(char *p)
801 if (sscanf(p, "%d", &did) != 1) {
802 printf("Param list error\n");
806 if (!(did < MAX_DIRS)) {
807 printf("Dir id too big\n");
811 if (dir_tab[did].name[0] == 0) {
812 printf("Dir id empty\n");
816 rc = ext4_dir_close(&dir_tab[did].fd);
819 dir_tab[did].name[0] = 0;
824 int _dir_entry_get(char *p)
830 if (sscanf(p, "%d %d", &did, &exp) != 2) {
831 printf("Param list error\n");
835 if (!(did < MAX_DIRS)) {
836 printf("Dir id too big\n");
840 if (dir_tab[did].name[0] == 0) {
841 printf("Dir id empty\n");
846 const ext4_direntry *d;
848 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
851 memcpy(name, d->name, d->name_length);
852 name[d->name_length] = 0;
854 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
859 printf("Minumum dir entry error\n");
863 if ((idx - 2) != exp) {
864 printf("Expected dir entry error\n");
871 int _multi_fcreate(char *p)
881 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
882 printf("Param list error\n");
886 for (i = 0; i < cnt; ++i) {
887 sprintf(path1, "%s%s%d", path, prefix, i);
888 rc = ext4_fopen(&fd, path1, "wb+");
897 int _multi_fwrite(char *p)
908 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
909 printf("Param list error\n");
913 for (i = 0; i < cnt; ++i) {
914 sprintf(path1, "%s%s%d", path, prefix, i);
915 rc = ext4_fopen(&fd, path1, "rb+");
922 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
923 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
929 printf("Write count error\n");
940 int _multi_fread(char *p)
951 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
952 printf("Param list error\n");
956 for (i = 0; i < cnt; ++i) {
957 sprintf(path1, "%s%s%d", path, prefix, i);
958 rc = ext4_fopen(&fd, path1, "rb+");
965 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
967 memset(read_buffer, 0, MAX_RW_BUFFER);
968 rc = ext4_fread(&fd, read_buffer, d, &rb);
974 printf("Read count error\n");
978 if (memcmp(read_buffer, write_buffer, d)) {
979 printf("Read compare error\n");
990 int _multi_fremove(char *p)
997 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
998 printf("Param list error\n");
1002 for (i = 0; i < cnt; ++i) {
1003 sprintf(path1, "%s%s%d", path, prefix, i);
1004 rc = ext4_fremove(path1);
1012 int _multi_dcreate(char *p)
1019 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1020 printf("Param list error\n");
1024 for (i = 0; i < cnt; ++i) {
1025 sprintf(path1, "%s%s%d", path, prefix, i);
1026 rc = ext4_dir_mk(path1);
1034 int _multi_dremove(char *p)
1041 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1042 printf("Param list error\n");
1046 for (i = 0; i < cnt; ++i) {
1047 sprintf(path1, "%s%s%d", path, prefix, i);
1048 rc = ext4_dir_rm(path1);
1056 struct ext4_mount_stats saved_stats;
1058 int _stats_save(char *p)
1062 if (sscanf(p, "%s", path) != 1) {
1063 printf("Param list error\n");
1067 return ext4_mount_point_stats(path, &saved_stats);
1070 int _stats_check(char *p)
1075 struct ext4_mount_stats actual_stats;
1077 if (sscanf(p, "%s", path) != 1) {
1078 printf("Param list error\n");
1082 rc = ext4_mount_point_stats(path, &actual_stats);
1087 if (memcmp(&saved_stats, &actual_stats,
1088 sizeof(struct ext4_mount_stats))) {
1090 printf("\tMount point stats error:\n");
1091 printf("\tsaved_stats:\n");
1092 printf("\tinodes_count = %" PRIu32"\n",
1093 saved_stats.inodes_count);
1094 printf("\tfree_inodes_count = %" PRIu32"\n",
1095 saved_stats.free_inodes_count);
1096 printf("\tblocks_count = %" PRIu64"\n",
1097 saved_stats.blocks_count);
1098 printf("\tfree_blocks_count = %" PRIu64"\n",
1099 saved_stats.free_blocks_count);
1100 printf("\tblock_size = %" PRIu32"\n",
1101 saved_stats.block_size);
1102 printf("\tblock_group_count = %" PRIu32"\n",
1103 saved_stats.block_group_count);
1104 printf("\tblocks_per_group = %" PRIu32"\n",
1105 saved_stats.blocks_per_group);
1106 printf("\tinodes_per_group = %" PRIu32"\n",
1107 saved_stats.inodes_per_group);
1108 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1109 printf("\tactual_stats:\n");
1110 printf("\tinodes_count = %" PRIu32"\n",
1111 actual_stats.inodes_count);
1112 printf("\tfree_inodes_count = %" PRIu32"\n",
1113 actual_stats.free_inodes_count);
1114 printf("\tblocks_count = %" PRIu64"\n",
1115 actual_stats.blocks_count);
1116 printf("\tfree_blocks_count = %" PRIu64"\n",
1117 actual_stats.free_blocks_count);
1118 printf("\tblock_size = %d\n", actual_stats.block_size);
1119 printf("\tblock_group_count = %" PRIu32"\n",
1120 actual_stats.block_group_count);
1121 printf("\tblocks_per_group = %" PRIu32"\n",
1122 actual_stats.blocks_per_group);
1123 printf("\tinodes_per_group = %" PRIu32"\n",
1124 actual_stats.inodes_per_group);
1125 printf("\tvolume_name = %s\n",
1126 actual_stats.volume_name);
1134 static char *entry_to_str(uint8_t type)
1137 case EXT4_DE_UNKNOWN:
1139 case EXT4_DE_REG_FILE:
1143 case EXT4_DE_CHRDEV:
1145 case EXT4_DE_BLKDEV:
1151 case EXT4_DE_SYMLINK:
1159 static int winsock_init(void)
1163 static WSADATA wsaData;
1164 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1172 static void winsock_fini(void)