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.
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <sys/types.h>
50 #include "../blockdev/linux/ext4_filedev.h"
51 #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 int verbose = 0;
75 /**@brief Winpart mode*/
76 static int winpart = 0;
78 /**@brief Blockdev handle*/
79 static struct ext4_blockdev *bd;
81 static int cache_wb = 0;
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", required_argument, 0, 'v'},
303 {"winpart", required_argument, 0, 'w'},
304 {"cache_wb", required_argument, 0, 'c'},
307 while (-1 != (c = getopt_long(argc, argv, "c:i:p:v:w:", long_options,
315 connection_port = atoi(optarg);
318 verbose = atoi(optarg);
321 cache_wb = atoi(optarg);
324 winpart = 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");
414 rc = ext4_mount(dev_name, mount_point);
416 ext4_cache_write_back(mount_point, 1);
422 char mount_point[32];
424 if (sscanf(p, "%s", mount_point) != 1) {
425 printf("Param list error\n");
430 ext4_cache_write_back(mount_point, 0);
432 return ext4_umount(mount_point);
435 int _mount_point_stats(char *p)
437 char mount_point[32];
440 struct ext4_mount_stats stats;
442 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
443 printf("Param list error\n");
447 rc = ext4_mount_point_stats(mount_point, &stats);
453 printf("\tinodes_count = %d\n", stats.inodes_count);
454 printf("\tfree_inodes_count = %d\n", stats.free_inodes_count);
455 printf("\tblocks_count = %llu\n", stats.blocks_count);
456 printf("\tfree_blocks_count = %llu\n", stats.free_blocks_count);
458 printf("\tblock_size = %d\n", stats.block_size);
459 printf("\tblock_group_count = %d\n", stats.block_group_count);
460 printf("\tblocks_per_group = %d\n", stats.blocks_per_group);
461 printf("\tinodes_per_group = %d\n", stats.inodes_per_group);
463 printf("\tvolume_name = %s\n", stats.volume_name);
469 int _cache_write_back(char *p)
471 char mount_point[32];
474 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
475 printf("Param list error\n");
479 return ext4_cache_write_back(mount_point, en);
482 int _fremove(char *p)
486 if (sscanf(p, "%s", path) != 1) {
487 printf("Param list error\n");
491 return ext4_fremove(path);
501 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
502 printf("Param list error\n");
506 if (!(fid < MAX_FILES)) {
507 printf("File id too big\n");
511 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
514 strcpy(file_tab[fid].name, path);
524 if (sscanf(p, "%d", &fid) != 1) {
525 printf("Param list error\n");
529 if (!(fid < MAX_FILES)) {
530 printf("File id too big\n");
534 if (file_tab[fid].name[0] == 0) {
535 printf("File id empty\n");
539 rc = ext4_fclose(&file_tab[fid].fd);
542 file_tab[fid].name[0] = 0;
555 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
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");
571 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
573 memset(read_buffer, 0, MAX_RW_BUFFER);
574 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
580 printf("Read count error\n");
584 if (memcmp(read_buffer, write_buffer, d)) {
585 printf("Read compare error\n");
604 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
605 printf("Param list error\n");
609 if (!(fid < MAX_FILES)) {
610 printf("File id too big\n");
614 if (file_tab[fid].name[0] == 0) {
615 printf("File id empty\n");
620 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
621 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
627 printf("Write count error\n");
643 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
644 printf("Param list error\n");
648 if (!(fid < MAX_FILES)) {
649 printf("File id too big\n");
653 if (file_tab[fid].name[0] == 0) {
654 printf("File id empty\n");
658 return ext4_fseek(&file_tab[fid].fd, off, origin);
666 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
667 printf("Param list error\n");
671 if (!(fid < MAX_FILES)) {
672 printf("File id too big\n");
676 if (file_tab[fid].name[0] == 0) {
677 printf("File id empty\n");
681 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
682 printf("Expected filepos error\n");
694 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
695 printf("Param list error\n");
699 if (!(fid < MAX_FILES)) {
700 printf("File id too big\n");
704 if (file_tab[fid].name[0] == 0) {
705 printf("File id empty\n");
709 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
710 printf("Expected filesize error\n");
721 if (sscanf(p, "%s", path) != 1) {
722 printf("Param list error\n");
726 return ext4_dir_rm(path);
733 if (sscanf(p, "%s", path) != 1) {
734 printf("Param list error\n");
738 return ext4_dir_mk(path);
741 int _dir_open(char *p)
747 if (sscanf(p, "%d %s", &did, path) != 2) {
748 printf("Param list error\n");
752 if (!(did < MAX_DIRS)) {
753 printf("Dir id too big\n");
757 rc = ext4_dir_open(&dir_tab[did].fd, path);
760 strcpy(dir_tab[did].name, path);
765 int _dir_close(char *p)
770 if (sscanf(p, "%d", &did) != 1) {
771 printf("Param list error\n");
775 if (!(did < MAX_DIRS)) {
776 printf("Dir id too big\n");
780 if (dir_tab[did].name[0] == 0) {
781 printf("Dir id empty\n");
785 rc = ext4_dir_close(&dir_tab[did].fd);
788 dir_tab[did].name[0] = 0;
793 int _dir_entry_get(char *p)
799 if (sscanf(p, "%d %d", &did, &exp) != 2) {
800 printf("Param list error\n");
804 if (!(did < MAX_DIRS)) {
805 printf("Dir id too big\n");
809 if (dir_tab[did].name[0] == 0) {
810 printf("Dir id empty\n");
815 const ext4_direntry *d;
817 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
820 memcpy(name, d->name, d->name_length);
821 name[d->name_length] = 0;
823 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
828 printf("Minumum dir entry error\n");
832 if ((idx - 2) != exp) {
833 printf("Expected dir entry error\n");
840 int _multi_fcreate(char *p)
850 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
851 printf("Param list error\n");
855 for (i = 0; i < cnt; ++i) {
856 sprintf(path1, "%s%s%d", path, prefix, i);
857 rc = ext4_fopen(&fd, path1, "wb+");
866 int _multi_fwrite(char *p)
877 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
878 printf("Param list error\n");
882 for (i = 0; i < cnt; ++i) {
883 sprintf(path1, "%s%s%d", path, prefix, i);
884 rc = ext4_fopen(&fd, path1, "rb+");
891 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
892 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
898 printf("Write count error\n");
909 int _multi_fread(char *p)
920 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
921 printf("Param list error\n");
925 for (i = 0; i < cnt; ++i) {
926 sprintf(path1, "%s%s%d", path, prefix, i);
927 rc = ext4_fopen(&fd, path1, "rb+");
934 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
936 memset(read_buffer, 0, MAX_RW_BUFFER);
937 rc = ext4_fread(&fd, read_buffer, d, &rb);
943 printf("Read count error\n");
947 if (memcmp(read_buffer, write_buffer, d)) {
948 printf("Read compare error\n");
959 int _multi_fremove(char *p)
966 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
967 printf("Param list error\n");
971 for (i = 0; i < cnt; ++i) {
972 sprintf(path1, "%s%s%d", path, prefix, i);
973 rc = ext4_fremove(path1);
981 int _multi_dcreate(char *p)
988 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
989 printf("Param list error\n");
993 for (i = 0; i < cnt; ++i) {
994 sprintf(path1, "%s%s%d", path, prefix, i);
995 rc = ext4_dir_mk(path1);
1003 int _multi_dremove(char *p)
1010 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1011 printf("Param list error\n");
1015 for (i = 0; i < cnt; ++i) {
1016 sprintf(path1, "%s%s%d", path, prefix, i);
1017 rc = ext4_dir_rm(path1);
1025 struct ext4_mount_stats saved_stats;
1027 int _stats_save(char *p)
1031 if (sscanf(p, "%s", path) != 1) {
1032 printf("Param list error\n");
1036 return ext4_mount_point_stats(path, &saved_stats);
1039 int _stats_check(char *p)
1044 struct ext4_mount_stats actual_stats;
1046 if (sscanf(p, "%s", path) != 1) {
1047 printf("Param list error\n");
1051 rc = ext4_mount_point_stats(path, &actual_stats);
1056 if (memcmp(&saved_stats, &actual_stats,
1057 sizeof(struct ext4_mount_stats))) {
1059 printf("\tMount point stats error:\n");
1060 printf("\tsaved_stats:\n");
1061 printf("\tinodes_count = %d\n",
1062 saved_stats.inodes_count);
1063 printf("\tfree_inodes_count = %d\n",
1064 saved_stats.free_inodes_count);
1065 printf("\tblocks_count = %llu\n",
1066 saved_stats.blocks_count);
1067 printf("\tfree_blocks_count = %llu\n",
1068 saved_stats.free_blocks_count);
1069 printf("\tblock_size = %d\n", saved_stats.block_size);
1070 printf("\tblock_group_count = %d\n",
1071 saved_stats.block_group_count);
1072 printf("\tblocks_per_group = %d\n",
1073 saved_stats.blocks_per_group);
1074 printf("\tinodes_per_group = %d\n",
1075 saved_stats.inodes_per_group);
1076 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1077 printf("\tactual_stats:\n");
1078 printf("\tinodes_count = %d\n",
1079 actual_stats.inodes_count);
1080 printf("\tfree_inodes_count = %d\n",
1081 actual_stats.free_inodes_count);
1082 printf("\tblocks_count = %llu\n",
1083 actual_stats.blocks_count);
1084 printf("\tfree_blocks_count = %llu\n",
1085 actual_stats.free_blocks_count);
1086 printf("\tblock_size = %d\n", actual_stats.block_size);
1087 printf("\tblock_group_count = %d\n",
1088 actual_stats.block_group_count);
1089 printf("\tblocks_per_group = %d\n",
1090 actual_stats.blocks_per_group);
1091 printf("\tinodes_per_group = %d\n",
1092 actual_stats.inodes_per_group);
1093 printf("\tvolume_name = %s\n",
1094 actual_stats.volume_name);
1102 static char *entry_to_str(uint8_t type)
1105 case EXT4_DIRENTRY_UNKNOWN:
1107 case EXT4_DIRENTRY_REG_FILE:
1109 case EXT4_DIRENTRY_DIR:
1111 case EXT4_DIRENTRY_CHRDEV:
1113 case EXT4_DIRENTRY_BLKDEV:
1115 case EXT4_DIRENTRY_FIFO:
1117 case EXT4_DIRENTRY_SOCK:
1119 case EXT4_DIRENTRY_SYMLINK:
1127 static int winsock_init(void)
1131 static WSADATA wsaData;
1132 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1140 static void winsock_fini(void)