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'},
307 while (-1 != (c = getopt_long(argc, argv, "i:p:vcw", long_options,
315 connection_port = atoi(optarg);
334 int main(int argc, char *argv[])
341 if (!parse_opt(argc, argv))
344 listenfd = server_open();
346 printf("lwext4_server: listening on port: %d\n", connection_port);
348 memset(write_buffer, RW_BUFFER_PATERN, MAX_RW_BUFFER);
350 connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
352 n = recv(connfd, op_code, sizeof(op_code), 0);
355 printf("recv() error: %s fd = %d\n", strerror(errno),
362 int r = exec_op_code(op_code);
364 n = send(connfd, (void *)&r, sizeof(r), 0);
366 printf("send() error: %s fd = %d\n", strerror(errno),
378 int _device_register(char *p)
384 if (sscanf(p, "%d %d %s", &dev, &cache_mode, dev_name) != 3) {
385 printf("Param list error\n");
391 ext4_io_raw_filename(ext4_fname);
392 bd = ext4_io_raw_dev_get();
397 ext4_filedev_filename(ext4_fname);
398 bd = ext4_filedev_get();
400 return ext4_device_register(bd, 0, dev_name);
406 char mount_point[32];
409 if (sscanf(p, "%s %s", dev_name, mount_point) != 2) {
410 printf("Param list error\n");
415 ext4_dmask_set(DEBUG_ALL);
417 rc = ext4_mount(dev_name, mount_point);
421 rc = ext4_recover(mount_point);
422 if (rc != EOK && rc != ENOTSUP)
425 rc = ext4_journal_start(mount_point);
430 ext4_cache_write_back(mount_point, 1);
436 char mount_point[32];
439 if (sscanf(p, "%s", mount_point) != 1) {
440 printf("Param list error\n");
445 ext4_cache_write_back(mount_point, 0);
447 rc = ext4_journal_stop(mount_point);
451 rc = ext4_umount(mount_point);
458 int _mount_point_stats(char *p)
460 char mount_point[32];
463 struct ext4_mount_stats stats;
465 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
466 printf("Param list error\n");
470 rc = ext4_mount_point_stats(mount_point, &stats);
476 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
477 printf("\tfree_inodes_count = %" PRIu32"\n",
478 stats.free_inodes_count);
479 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
480 printf("\tfree_blocks_count = %" PRIu64"\n",
481 stats.free_blocks_count);
482 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
483 printf("\tblock_group_count = %" PRIu32"\n",
484 stats.block_group_count);
485 printf("\tblocks_per_group = %" PRIu32"\n",
486 stats.blocks_per_group);
487 printf("\tinodes_per_group = %" PRIu32"\n",
488 stats.inodes_per_group);
489 printf("\tvolume_name = %s\n", stats.volume_name);
495 int _cache_write_back(char *p)
497 char mount_point[32];
500 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
501 printf("Param list error\n");
505 return ext4_cache_write_back(mount_point, en);
508 int _fremove(char *p)
512 if (sscanf(p, "%s", path) != 1) {
513 printf("Param list error\n");
517 return ext4_fremove(path);
527 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
528 printf("Param list error\n");
532 if (!(fid < MAX_FILES)) {
533 printf("File id too big\n");
537 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
540 strcpy(file_tab[fid].name, path);
550 if (sscanf(p, "%d", &fid) != 1) {
551 printf("Param list error\n");
555 if (!(fid < MAX_FILES)) {
556 printf("File id too big\n");
560 if (file_tab[fid].name[0] == 0) {
561 printf("File id empty\n");
565 rc = ext4_fclose(&file_tab[fid].fd);
568 file_tab[fid].name[0] = 0;
581 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
582 printf("Param list error\n");
586 if (!(fid < MAX_FILES)) {
587 printf("File id too big\n");
591 if (file_tab[fid].name[0] == 0) {
592 printf("File id empty\n");
597 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
599 memset(read_buffer, 0, MAX_RW_BUFFER);
600 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
606 printf("Read count error\n");
610 if (memcmp(read_buffer, write_buffer, d)) {
611 printf("Read compare error\n");
630 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
631 printf("Param list error\n");
635 if (!(fid < MAX_FILES)) {
636 printf("File id too big\n");
640 if (file_tab[fid].name[0] == 0) {
641 printf("File id empty\n");
646 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
647 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
653 printf("Write count error\n");
669 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
670 printf("Param list error\n");
674 if (!(fid < MAX_FILES)) {
675 printf("File id too big\n");
679 if (file_tab[fid].name[0] == 0) {
680 printf("File id empty\n");
684 return ext4_fseek(&file_tab[fid].fd, off, origin);
692 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
693 printf("Param list error\n");
697 if (!(fid < MAX_FILES)) {
698 printf("File id too big\n");
702 if (file_tab[fid].name[0] == 0) {
703 printf("File id empty\n");
707 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
708 printf("Expected filepos error\n");
720 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
721 printf("Param list error\n");
725 if (!(fid < MAX_FILES)) {
726 printf("File id too big\n");
730 if (file_tab[fid].name[0] == 0) {
731 printf("File id empty\n");
735 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
736 printf("Expected filesize error\n");
747 if (sscanf(p, "%s", path) != 1) {
748 printf("Param list error\n");
752 return ext4_dir_rm(path);
759 if (sscanf(p, "%s", path) != 1) {
760 printf("Param list error\n");
764 return ext4_dir_mk(path);
767 int _dir_open(char *p)
773 if (sscanf(p, "%d %s", &did, path) != 2) {
774 printf("Param list error\n");
778 if (!(did < MAX_DIRS)) {
779 printf("Dir id too big\n");
783 rc = ext4_dir_open(&dir_tab[did].fd, path);
786 strcpy(dir_tab[did].name, path);
791 int _dir_close(char *p)
796 if (sscanf(p, "%d", &did) != 1) {
797 printf("Param list error\n");
801 if (!(did < MAX_DIRS)) {
802 printf("Dir id too big\n");
806 if (dir_tab[did].name[0] == 0) {
807 printf("Dir id empty\n");
811 rc = ext4_dir_close(&dir_tab[did].fd);
814 dir_tab[did].name[0] = 0;
819 int _dir_entry_get(char *p)
825 if (sscanf(p, "%d %d", &did, &exp) != 2) {
826 printf("Param list error\n");
830 if (!(did < MAX_DIRS)) {
831 printf("Dir id too big\n");
835 if (dir_tab[did].name[0] == 0) {
836 printf("Dir id empty\n");
841 const ext4_direntry *d;
843 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
846 memcpy(name, d->name, d->name_length);
847 name[d->name_length] = 0;
849 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
854 printf("Minumum dir entry error\n");
858 if ((idx - 2) != exp) {
859 printf("Expected dir entry error\n");
866 int _multi_fcreate(char *p)
876 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
877 printf("Param list error\n");
881 for (i = 0; i < cnt; ++i) {
882 sprintf(path1, "%s%s%d", path, prefix, i);
883 rc = ext4_fopen(&fd, path1, "wb+");
892 int _multi_fwrite(char *p)
903 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
904 printf("Param list error\n");
908 for (i = 0; i < cnt; ++i) {
909 sprintf(path1, "%s%s%d", path, prefix, i);
910 rc = ext4_fopen(&fd, path1, "rb+");
917 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
918 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
924 printf("Write count error\n");
935 int _multi_fread(char *p)
946 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
947 printf("Param list error\n");
951 for (i = 0; i < cnt; ++i) {
952 sprintf(path1, "%s%s%d", path, prefix, i);
953 rc = ext4_fopen(&fd, path1, "rb+");
960 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
962 memset(read_buffer, 0, MAX_RW_BUFFER);
963 rc = ext4_fread(&fd, read_buffer, d, &rb);
969 printf("Read count error\n");
973 if (memcmp(read_buffer, write_buffer, d)) {
974 printf("Read compare error\n");
985 int _multi_fremove(char *p)
992 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
993 printf("Param list error\n");
997 for (i = 0; i < cnt; ++i) {
998 sprintf(path1, "%s%s%d", path, prefix, i);
999 rc = ext4_fremove(path1);
1007 int _multi_dcreate(char *p)
1014 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1015 printf("Param list error\n");
1019 for (i = 0; i < cnt; ++i) {
1020 sprintf(path1, "%s%s%d", path, prefix, i);
1021 rc = ext4_dir_mk(path1);
1029 int _multi_dremove(char *p)
1036 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1037 printf("Param list error\n");
1041 for (i = 0; i < cnt; ++i) {
1042 sprintf(path1, "%s%s%d", path, prefix, i);
1043 rc = ext4_dir_rm(path1);
1051 struct ext4_mount_stats saved_stats;
1053 int _stats_save(char *p)
1057 if (sscanf(p, "%s", path) != 1) {
1058 printf("Param list error\n");
1062 return ext4_mount_point_stats(path, &saved_stats);
1065 int _stats_check(char *p)
1070 struct ext4_mount_stats actual_stats;
1072 if (sscanf(p, "%s", path) != 1) {
1073 printf("Param list error\n");
1077 rc = ext4_mount_point_stats(path, &actual_stats);
1082 if (memcmp(&saved_stats, &actual_stats,
1083 sizeof(struct ext4_mount_stats))) {
1085 printf("\tMount point stats error:\n");
1086 printf("\tsaved_stats:\n");
1087 printf("\tinodes_count = %" PRIu32"\n",
1088 saved_stats.inodes_count);
1089 printf("\tfree_inodes_count = %" PRIu32"\n",
1090 saved_stats.free_inodes_count);
1091 printf("\tblocks_count = %" PRIu64"\n",
1092 saved_stats.blocks_count);
1093 printf("\tfree_blocks_count = %" PRIu64"\n",
1094 saved_stats.free_blocks_count);
1095 printf("\tblock_size = %" PRIu32"\n",
1096 saved_stats.block_size);
1097 printf("\tblock_group_count = %" PRIu32"\n",
1098 saved_stats.block_group_count);
1099 printf("\tblocks_per_group = %" PRIu32"\n",
1100 saved_stats.blocks_per_group);
1101 printf("\tinodes_per_group = %" PRIu32"\n",
1102 saved_stats.inodes_per_group);
1103 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1104 printf("\tactual_stats:\n");
1105 printf("\tinodes_count = %" PRIu32"\n",
1106 actual_stats.inodes_count);
1107 printf("\tfree_inodes_count = %" PRIu32"\n",
1108 actual_stats.free_inodes_count);
1109 printf("\tblocks_count = %" PRIu64"\n",
1110 actual_stats.blocks_count);
1111 printf("\tfree_blocks_count = %" PRIu64"\n",
1112 actual_stats.free_blocks_count);
1113 printf("\tblock_size = %d\n", actual_stats.block_size);
1114 printf("\tblock_group_count = %" PRIu32"\n",
1115 actual_stats.block_group_count);
1116 printf("\tblocks_per_group = %" PRIu32"\n",
1117 actual_stats.blocks_per_group);
1118 printf("\tinodes_per_group = %" PRIu32"\n",
1119 actual_stats.inodes_per_group);
1120 printf("\tvolume_name = %s\n",
1121 actual_stats.volume_name);
1129 static char *entry_to_str(uint8_t type)
1132 case EXT4_DE_UNKNOWN:
1134 case EXT4_DE_REG_FILE:
1138 case EXT4_DE_CHRDEV:
1140 case EXT4_DE_BLKDEV:
1146 case EXT4_DE_SYMLINK:
1154 static int winsock_init(void)
1158 static WSADATA wsaData;
1159 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1167 static void winsock_fini(void)