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 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);
418 rc = ext4_recover(mount_point);
419 if (rc != EOK && rc != ENOTSUP)
422 rc = ext4_journal_start(mount_point);
427 ext4_cache_write_back(mount_point, 1);
433 char mount_point[32];
436 if (sscanf(p, "%s", mount_point) != 1) {
437 printf("Param list error\n");
442 ext4_cache_write_back(mount_point, 0);
444 rc = ext4_journal_stop(mount_point);
448 rc = ext4_umount(mount_point);
455 int _mount_point_stats(char *p)
457 char mount_point[32];
460 struct ext4_mount_stats stats;
462 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
463 printf("Param list error\n");
467 rc = ext4_mount_point_stats(mount_point, &stats);
473 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
474 printf("\tfree_inodes_count = %" PRIu32"\n",
475 stats.free_inodes_count);
476 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
477 printf("\tfree_blocks_count = %" PRIu64"\n",
478 stats.free_blocks_count);
479 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
480 printf("\tblock_group_count = %" PRIu32"\n",
481 stats.block_group_count);
482 printf("\tblocks_per_group = %" PRIu32"\n",
483 stats.blocks_per_group);
484 printf("\tinodes_per_group = %" PRIu32"\n",
485 stats.inodes_per_group);
486 printf("\tvolume_name = %s\n", stats.volume_name);
492 int _cache_write_back(char *p)
494 char mount_point[32];
497 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
498 printf("Param list error\n");
502 return ext4_cache_write_back(mount_point, en);
505 int _fremove(char *p)
509 if (sscanf(p, "%s", path) != 1) {
510 printf("Param list error\n");
514 return ext4_fremove(path);
524 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
525 printf("Param list error\n");
529 if (!(fid < MAX_FILES)) {
530 printf("File id too big\n");
534 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
537 strcpy(file_tab[fid].name, path);
547 if (sscanf(p, "%d", &fid) != 1) {
548 printf("Param list error\n");
552 if (!(fid < MAX_FILES)) {
553 printf("File id too big\n");
557 if (file_tab[fid].name[0] == 0) {
558 printf("File id empty\n");
562 rc = ext4_fclose(&file_tab[fid].fd);
565 file_tab[fid].name[0] = 0;
578 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
579 printf("Param list error\n");
583 if (!(fid < MAX_FILES)) {
584 printf("File id too big\n");
588 if (file_tab[fid].name[0] == 0) {
589 printf("File id empty\n");
594 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
596 memset(read_buffer, 0, MAX_RW_BUFFER);
597 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
603 printf("Read count error\n");
607 if (memcmp(read_buffer, write_buffer, d)) {
608 printf("Read compare error\n");
627 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
628 printf("Param list error\n");
632 if (!(fid < MAX_FILES)) {
633 printf("File id too big\n");
637 if (file_tab[fid].name[0] == 0) {
638 printf("File id empty\n");
643 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
644 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
650 printf("Write count error\n");
666 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
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 return ext4_fseek(&file_tab[fid].fd, off, origin);
689 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
690 printf("Param list error\n");
694 if (!(fid < MAX_FILES)) {
695 printf("File id too big\n");
699 if (file_tab[fid].name[0] == 0) {
700 printf("File id empty\n");
704 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
705 printf("Expected filepos error\n");
717 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
718 printf("Param list error\n");
722 if (!(fid < MAX_FILES)) {
723 printf("File id too big\n");
727 if (file_tab[fid].name[0] == 0) {
728 printf("File id empty\n");
732 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
733 printf("Expected filesize error\n");
744 if (sscanf(p, "%s", path) != 1) {
745 printf("Param list error\n");
749 return ext4_dir_rm(path);
756 if (sscanf(p, "%s", path) != 1) {
757 printf("Param list error\n");
761 return ext4_dir_mk(path);
764 int _dir_open(char *p)
770 if (sscanf(p, "%d %s", &did, path) != 2) {
771 printf("Param list error\n");
775 if (!(did < MAX_DIRS)) {
776 printf("Dir id too big\n");
780 rc = ext4_dir_open(&dir_tab[did].fd, path);
783 strcpy(dir_tab[did].name, path);
788 int _dir_close(char *p)
793 if (sscanf(p, "%d", &did) != 1) {
794 printf("Param list error\n");
798 if (!(did < MAX_DIRS)) {
799 printf("Dir id too big\n");
803 if (dir_tab[did].name[0] == 0) {
804 printf("Dir id empty\n");
808 rc = ext4_dir_close(&dir_tab[did].fd);
811 dir_tab[did].name[0] = 0;
816 int _dir_entry_get(char *p)
822 if (sscanf(p, "%d %d", &did, &exp) != 2) {
823 printf("Param list error\n");
827 if (!(did < MAX_DIRS)) {
828 printf("Dir id too big\n");
832 if (dir_tab[did].name[0] == 0) {
833 printf("Dir id empty\n");
838 const ext4_direntry *d;
840 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
843 memcpy(name, d->name, d->name_length);
844 name[d->name_length] = 0;
846 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
851 printf("Minumum dir entry error\n");
855 if ((idx - 2) != exp) {
856 printf("Expected dir entry error\n");
863 int _multi_fcreate(char *p)
873 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
874 printf("Param list error\n");
878 for (i = 0; i < cnt; ++i) {
879 sprintf(path1, "%s%s%d", path, prefix, i);
880 rc = ext4_fopen(&fd, path1, "wb+");
889 int _multi_fwrite(char *p)
900 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
901 printf("Param list error\n");
905 for (i = 0; i < cnt; ++i) {
906 sprintf(path1, "%s%s%d", path, prefix, i);
907 rc = ext4_fopen(&fd, path1, "rb+");
914 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
915 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
921 printf("Write count error\n");
932 int _multi_fread(char *p)
943 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
944 printf("Param list error\n");
948 for (i = 0; i < cnt; ++i) {
949 sprintf(path1, "%s%s%d", path, prefix, i);
950 rc = ext4_fopen(&fd, path1, "rb+");
957 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
959 memset(read_buffer, 0, MAX_RW_BUFFER);
960 rc = ext4_fread(&fd, read_buffer, d, &rb);
966 printf("Read count error\n");
970 if (memcmp(read_buffer, write_buffer, d)) {
971 printf("Read compare error\n");
982 int _multi_fremove(char *p)
989 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
990 printf("Param list error\n");
994 for (i = 0; i < cnt; ++i) {
995 sprintf(path1, "%s%s%d", path, prefix, i);
996 rc = ext4_fremove(path1);
1004 int _multi_dcreate(char *p)
1011 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1012 printf("Param list error\n");
1016 for (i = 0; i < cnt; ++i) {
1017 sprintf(path1, "%s%s%d", path, prefix, i);
1018 rc = ext4_dir_mk(path1);
1026 int _multi_dremove(char *p)
1033 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1034 printf("Param list error\n");
1038 for (i = 0; i < cnt; ++i) {
1039 sprintf(path1, "%s%s%d", path, prefix, i);
1040 rc = ext4_dir_rm(path1);
1048 struct ext4_mount_stats saved_stats;
1050 int _stats_save(char *p)
1054 if (sscanf(p, "%s", path) != 1) {
1055 printf("Param list error\n");
1059 return ext4_mount_point_stats(path, &saved_stats);
1062 int _stats_check(char *p)
1067 struct ext4_mount_stats actual_stats;
1069 if (sscanf(p, "%s", path) != 1) {
1070 printf("Param list error\n");
1074 rc = ext4_mount_point_stats(path, &actual_stats);
1079 if (memcmp(&saved_stats, &actual_stats,
1080 sizeof(struct ext4_mount_stats))) {
1082 printf("\tMount point stats error:\n");
1083 printf("\tsaved_stats:\n");
1084 printf("\tinodes_count = %" PRIu32"\n",
1085 saved_stats.inodes_count);
1086 printf("\tfree_inodes_count = %" PRIu32"\n",
1087 saved_stats.free_inodes_count);
1088 printf("\tblocks_count = %" PRIu64"\n",
1089 saved_stats.blocks_count);
1090 printf("\tfree_blocks_count = %" PRIu64"\n",
1091 saved_stats.free_blocks_count);
1092 printf("\tblock_size = %" PRIu32"\n",
1093 saved_stats.block_size);
1094 printf("\tblock_group_count = %" PRIu32"\n",
1095 saved_stats.block_group_count);
1096 printf("\tblocks_per_group = %" PRIu32"\n",
1097 saved_stats.blocks_per_group);
1098 printf("\tinodes_per_group = %" PRIu32"\n",
1099 saved_stats.inodes_per_group);
1100 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1101 printf("\tactual_stats:\n");
1102 printf("\tinodes_count = %" PRIu32"\n",
1103 actual_stats.inodes_count);
1104 printf("\tfree_inodes_count = %" PRIu32"\n",
1105 actual_stats.free_inodes_count);
1106 printf("\tblocks_count = %" PRIu64"\n",
1107 actual_stats.blocks_count);
1108 printf("\tfree_blocks_count = %" PRIu64"\n",
1109 actual_stats.free_blocks_count);
1110 printf("\tblock_size = %d\n", actual_stats.block_size);
1111 printf("\tblock_group_count = %" PRIu32"\n",
1112 actual_stats.block_group_count);
1113 printf("\tblocks_per_group = %" PRIu32"\n",
1114 actual_stats.blocks_per_group);
1115 printf("\tinodes_per_group = %" PRIu32"\n",
1116 actual_stats.inodes_per_group);
1117 printf("\tvolume_name = %s\n",
1118 actual_stats.volume_name);
1126 static char *entry_to_str(uint8_t type)
1129 case EXT4_DE_UNKNOWN:
1131 case EXT4_DE_REG_FILE:
1135 case EXT4_DE_CHRDEV:
1137 case EXT4_DE_BLKDEV:
1143 case EXT4_DE_SYMLINK:
1151 static int winsock_init(void)
1155 static WSADATA wsaData;
1156 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1164 static void winsock_fini(void)