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>
51 #include "../blockdev/linux/ext4_filedev.h"
52 #include "../blockdev/windows/io_raw.h"
57 static int winsock_init(void);
58 static void winsock_fini(void);
59 static char *entry_to_str(uint8_t type);
64 #define MAX_RW_BUFFER (1024 * 1024)
65 #define RW_BUFFER_PATERN ('x')
67 /**@brief Default connection port*/
68 static int connection_port = 1234;
70 /**@brief Default filesystem filename.*/
71 static char *ext4_fname = "ext2";
73 /**@brief Verbose mode*/
74 static int verbose = 0;
76 /**@brief Winpart mode*/
77 static int winpart = 0;
79 /**@brief Blockdev handle*/
80 static struct ext4_blockdev *bd;
82 static int cache_wb = 0;
84 static char read_buffer[MAX_RW_BUFFER];
85 static char write_buffer[MAX_RW_BUFFER];
87 static const char *usage = " \n\
88 Welcome in lwext4_server. \n\
89 Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\
91 --image (-i) - ext2/3/4 image file \n\
92 --port (-p) - server port \n\
93 --verbose (-v) - verbose mode \n\
94 --winpart (-w) - windows_partition mode \n\
95 --cache_wb (-c) - cache writeback_mode \n\
98 /**@brief Open file instance descriptor.*/
104 /**@brief Open directory instance descriptor.*/
110 /**@brief Library call opcode.*/
111 struct lwext4_op_codes {
115 /**@brief Library call wraper.*/
117 int (*lwext4_call)(char *p);
121 static struct lwext4_files file_tab[MAX_FILES];
124 static struct lwext4_dirs dir_tab[MAX_DIRS];
127 static struct lwext4_op_codes op_codes[] = {
157 int _device_register(char *p);
159 int _umount(char *p);
160 int _mount_point_stats(char *p);
161 int _cache_write_back(char *p);
162 int _fremove(char *p);
164 int _fclose(char *p);
166 int _fwrite(char *p);
170 int _dir_rm(char *p);
171 int _dir_mk(char *p);
172 int _dir_open(char *p);
173 int _dir_close(char *p);
174 int _dir_close(char *p);
175 int _dir_entry_get(char *p);
177 int _multi_fcreate(char *p);
178 int _multi_fwrite(char *p);
179 int _multi_fread(char *p);
180 int _multi_fremove(char *p);
181 int _multi_dcreate(char *p);
182 int _multi_dremove(char *p);
183 int _stats_save(char *p);
184 int _stats_check(char *p);
187 static struct lwext4_call op_call[] = {
188 _device_register, /*PARAMS(3): 0 cache_mode dev_name */
189 _mount, /*PARAMS(2): dev_name mount_point */
190 _umount, /*PARAMS(1): mount_point */
191 _mount_point_stats, /*PARAMS(2): mount_point, 0 */
192 _cache_write_back, /*PARAMS(2): mount_point, en */
193 _fremove, /*PARAMS(1): path */
194 _fopen, /*PARAMS(2): fid path flags */
195 _fclose, /*PARAMS(1): fid */
196 _fread, /*PARAMS(4): fid 0 len 0 */
197 _fwrite, /*PARAMS(4): fid 0 len 0 */
198 _fseek, /*PARAMS(2): fid off origin */
199 _ftell, /*PARAMS(2): fid exp */
200 _fsize, /*PARAMS(2): fid exp */
201 _dir_rm, /*PARAMS(1): path */
202 _dir_mk, /*PARAMS(1): path */
203 _dir_open, /*PARAMS(2): did, path */
204 _dir_close, /*PARAMS(1): did */
205 _dir_entry_get, /*PARAMS(2): did, exp */
207 _multi_fcreate, /*PARAMS(3): path prefix cnt */
208 _multi_fwrite, /*PARAMS(4): path prefix cnt size */
209 _multi_fread, /*PARAMS(4): path prefix cnt size */
210 _multi_fremove, /*PARAMS(2): path prefix cnt */
211 _multi_dcreate, /*PARAMS(3): path prefix cnt */
212 _multi_dremove, /*PARAMS(2): path prefix */
213 _stats_save, /*PARAMS(1): path */
214 _stats_check, /*PARAMS(1): path */
217 static clock_t get_ms(void)
220 gettimeofday(&t, NULL);
221 return (t.tv_sec * 1000) + (t.tv_usec / 1000);
225 static int exec_op_code(char *opcode)
230 for (i = 0; i < sizeof(op_codes) / sizeof(op_codes[0]); ++i) {
232 if (strncmp(op_codes[i].func, opcode, strlen(op_codes[i].func)))
235 if (opcode[strlen(op_codes[i].func)] != ' ')
238 printf("%s\n", opcode);
239 opcode += strlen(op_codes[i].func);
242 clock_t t = get_ms();
243 r = op_call[i].lwext4_call(opcode);
245 printf("rc: %d, time: %ums\n", r, (unsigned int)(get_ms() - t));
253 static int server_open(void)
256 struct sockaddr_in serv_addr;
258 memset(&serv_addr, 0, sizeof(serv_addr));
260 if (winsock_init() < 0) {
261 printf("winsock_init() error\n");
265 fd = socket(AF_INET, SOCK_STREAM, 0);
267 printf("socket() error: %s\n", strerror(errno));
272 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes,
274 printf("setsockopt() error: %s\n", strerror(errno));
278 serv_addr.sin_family = AF_INET;
279 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
280 serv_addr.sin_port = htons(connection_port);
282 if (bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
283 printf("bind() error: %s\n", strerror(errno));
288 printf("listen() error: %s\n", strerror(errno));
295 static bool parse_opt(int argc, char **argv)
297 int option_index = 0;
300 static struct option long_options[] = {
301 {"image", required_argument, 0, 'i'},
302 {"port", required_argument, 0, 'p'},
303 {"verbose", required_argument, 0, 'v'},
304 {"winpart", required_argument, 0, 'w'},
305 {"cache_wb", required_argument, 0, 'c'},
308 while (-1 != (c = getopt_long(argc, argv, "c:i:p:v:w:", long_options,
316 connection_port = atoi(optarg);
319 verbose = atoi(optarg);
322 cache_wb = atoi(optarg);
325 winpart = atoi(optarg);
335 int main(int argc, char *argv[])
342 if (!parse_opt(argc, argv))
345 listenfd = server_open();
347 printf("lwext4_server: listening on port: %d\n", connection_port);
349 memset(write_buffer, RW_BUFFER_PATERN, MAX_RW_BUFFER);
351 connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
353 n = recv(connfd, op_code, sizeof(op_code), 0);
356 printf("recv() error: %s fd = %d\n", strerror(errno),
363 int r = exec_op_code(op_code);
365 n = send(connfd, (void *)&r, sizeof(r), 0);
367 printf("send() error: %s fd = %d\n", strerror(errno),
379 int _device_register(char *p)
385 if (sscanf(p, "%d %d %s", &dev, &cache_mode, dev_name) != 3) {
386 printf("Param list error\n");
392 ext4_io_raw_filename(ext4_fname);
393 bd = ext4_io_raw_dev_get();
398 ext4_filedev_filename(ext4_fname);
399 bd = ext4_filedev_get();
401 return ext4_device_register(bd, 0, dev_name);
407 char mount_point[32];
410 if (sscanf(p, "%s %s", dev_name, mount_point) != 2) {
411 printf("Param list error\n");
415 rc = ext4_mount(dev_name, mount_point);
417 ext4_cache_write_back(mount_point, 1);
423 char mount_point[32];
425 if (sscanf(p, "%s", mount_point) != 1) {
426 printf("Param list error\n");
431 ext4_cache_write_back(mount_point, 0);
433 return ext4_umount(mount_point);
436 int _mount_point_stats(char *p)
438 char mount_point[32];
441 struct ext4_mount_stats stats;
443 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
444 printf("Param list error\n");
448 rc = ext4_mount_point_stats(mount_point, &stats);
454 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
455 printf("\tfree_inodes_count = %" PRIu32"\n",
456 stats.free_inodes_count);
457 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
458 printf("\tfree_blocks_count = %" PRIu64"\n",
459 stats.free_blocks_count);
460 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
461 printf("\tblock_group_count = %" PRIu32"\n",
462 stats.block_group_count);
463 printf("\tblocks_per_group = %" PRIu32"\n",
464 stats.blocks_per_group);
465 printf("\tinodes_per_group = %" PRIu32"\n",
466 stats.inodes_per_group);
467 printf("\tvolume_name = %s\n", stats.volume_name);
473 int _cache_write_back(char *p)
475 char mount_point[32];
478 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
479 printf("Param list error\n");
483 return ext4_cache_write_back(mount_point, en);
486 int _fremove(char *p)
490 if (sscanf(p, "%s", path) != 1) {
491 printf("Param list error\n");
495 return ext4_fremove(path);
505 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
506 printf("Param list error\n");
510 if (!(fid < MAX_FILES)) {
511 printf("File id too big\n");
515 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
518 strcpy(file_tab[fid].name, path);
528 if (sscanf(p, "%d", &fid) != 1) {
529 printf("Param list error\n");
533 if (!(fid < MAX_FILES)) {
534 printf("File id too big\n");
538 if (file_tab[fid].name[0] == 0) {
539 printf("File id empty\n");
543 rc = ext4_fclose(&file_tab[fid].fd);
546 file_tab[fid].name[0] = 0;
559 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
560 printf("Param list error\n");
564 if (!(fid < MAX_FILES)) {
565 printf("File id too big\n");
569 if (file_tab[fid].name[0] == 0) {
570 printf("File id empty\n");
575 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
577 memset(read_buffer, 0, MAX_RW_BUFFER);
578 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
584 printf("Read count error\n");
588 if (memcmp(read_buffer, write_buffer, d)) {
589 printf("Read compare error\n");
608 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
609 printf("Param list error\n");
613 if (!(fid < MAX_FILES)) {
614 printf("File id too big\n");
618 if (file_tab[fid].name[0] == 0) {
619 printf("File id empty\n");
624 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
625 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
631 printf("Write count error\n");
647 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
648 printf("Param list error\n");
652 if (!(fid < MAX_FILES)) {
653 printf("File id too big\n");
657 if (file_tab[fid].name[0] == 0) {
658 printf("File id empty\n");
662 return ext4_fseek(&file_tab[fid].fd, off, origin);
670 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
671 printf("Param list error\n");
675 if (!(fid < MAX_FILES)) {
676 printf("File id too big\n");
680 if (file_tab[fid].name[0] == 0) {
681 printf("File id empty\n");
685 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
686 printf("Expected filepos error\n");
698 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
699 printf("Param list error\n");
703 if (!(fid < MAX_FILES)) {
704 printf("File id too big\n");
708 if (file_tab[fid].name[0] == 0) {
709 printf("File id empty\n");
713 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
714 printf("Expected filesize error\n");
725 if (sscanf(p, "%s", path) != 1) {
726 printf("Param list error\n");
730 return ext4_dir_rm(path);
737 if (sscanf(p, "%s", path) != 1) {
738 printf("Param list error\n");
742 return ext4_dir_mk(path);
745 int _dir_open(char *p)
751 if (sscanf(p, "%d %s", &did, path) != 2) {
752 printf("Param list error\n");
756 if (!(did < MAX_DIRS)) {
757 printf("Dir id too big\n");
761 rc = ext4_dir_open(&dir_tab[did].fd, path);
764 strcpy(dir_tab[did].name, path);
769 int _dir_close(char *p)
774 if (sscanf(p, "%d", &did) != 1) {
775 printf("Param list error\n");
779 if (!(did < MAX_DIRS)) {
780 printf("Dir id too big\n");
784 if (dir_tab[did].name[0] == 0) {
785 printf("Dir id empty\n");
789 rc = ext4_dir_close(&dir_tab[did].fd);
792 dir_tab[did].name[0] = 0;
797 int _dir_entry_get(char *p)
803 if (sscanf(p, "%d %d", &did, &exp) != 2) {
804 printf("Param list error\n");
808 if (!(did < MAX_DIRS)) {
809 printf("Dir id too big\n");
813 if (dir_tab[did].name[0] == 0) {
814 printf("Dir id empty\n");
819 const ext4_direntry *d;
821 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
824 memcpy(name, d->name, d->name_length);
825 name[d->name_length] = 0;
827 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
832 printf("Minumum dir entry error\n");
836 if ((idx - 2) != exp) {
837 printf("Expected dir entry error\n");
844 int _multi_fcreate(char *p)
854 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
855 printf("Param list error\n");
859 for (i = 0; i < cnt; ++i) {
860 sprintf(path1, "%s%s%d", path, prefix, i);
861 rc = ext4_fopen(&fd, path1, "wb+");
870 int _multi_fwrite(char *p)
881 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
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, "rb+");
895 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
896 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
902 printf("Write count error\n");
913 int _multi_fread(char *p)
924 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
925 printf("Param list error\n");
929 for (i = 0; i < cnt; ++i) {
930 sprintf(path1, "%s%s%d", path, prefix, i);
931 rc = ext4_fopen(&fd, path1, "rb+");
938 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
940 memset(read_buffer, 0, MAX_RW_BUFFER);
941 rc = ext4_fread(&fd, read_buffer, d, &rb);
947 printf("Read count error\n");
951 if (memcmp(read_buffer, write_buffer, d)) {
952 printf("Read compare error\n");
963 int _multi_fremove(char *p)
970 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
971 printf("Param list error\n");
975 for (i = 0; i < cnt; ++i) {
976 sprintf(path1, "%s%s%d", path, prefix, i);
977 rc = ext4_fremove(path1);
985 int _multi_dcreate(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_dir_mk(path1);
1007 int _multi_dremove(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_rm(path1);
1029 struct ext4_mount_stats saved_stats;
1031 int _stats_save(char *p)
1035 if (sscanf(p, "%s", path) != 1) {
1036 printf("Param list error\n");
1040 return ext4_mount_point_stats(path, &saved_stats);
1043 int _stats_check(char *p)
1048 struct ext4_mount_stats actual_stats;
1050 if (sscanf(p, "%s", path) != 1) {
1051 printf("Param list error\n");
1055 rc = ext4_mount_point_stats(path, &actual_stats);
1060 if (memcmp(&saved_stats, &actual_stats,
1061 sizeof(struct ext4_mount_stats))) {
1063 printf("\tMount point stats error:\n");
1064 printf("\tsaved_stats:\n");
1065 printf("\tinodes_count = %" PRIu32"\n",
1066 saved_stats.inodes_count);
1067 printf("\tfree_inodes_count = %" PRIu32"\n",
1068 saved_stats.free_inodes_count);
1069 printf("\tblocks_count = %" PRIu64"\n",
1070 saved_stats.blocks_count);
1071 printf("\tfree_blocks_count = %" PRIu64"\n",
1072 saved_stats.free_blocks_count);
1073 printf("\tblock_size = %" PRIu32"\n",
1074 saved_stats.block_size);
1075 printf("\tblock_group_count = %" PRIu32"\n",
1076 saved_stats.block_group_count);
1077 printf("\tblocks_per_group = %" PRIu32"\n",
1078 saved_stats.blocks_per_group);
1079 printf("\tinodes_per_group = %" PRIu32"\n",
1080 saved_stats.inodes_per_group);
1081 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1082 printf("\tactual_stats:\n");
1083 printf("\tinodes_count = %" PRIu32"\n",
1084 actual_stats.inodes_count);
1085 printf("\tfree_inodes_count = %" PRIu32"\n",
1086 actual_stats.free_inodes_count);
1087 printf("\tblocks_count = %" PRIu64"\n",
1088 actual_stats.blocks_count);
1089 printf("\tfree_blocks_count = %" PRIu64"\n",
1090 actual_stats.free_blocks_count);
1091 printf("\tblock_size = %d\n", actual_stats.block_size);
1092 printf("\tblock_group_count = %" PRIu32"\n",
1093 actual_stats.block_group_count);
1094 printf("\tblocks_per_group = %" PRIu32"\n",
1095 actual_stats.blocks_per_group);
1096 printf("\tinodes_per_group = %" PRIu32"\n",
1097 actual_stats.inodes_per_group);
1098 printf("\tvolume_name = %s\n",
1099 actual_stats.volume_name);
1107 static char *entry_to_str(uint8_t type)
1110 case EXT4_DE_UNKNOWN:
1112 case EXT4_DE_REG_FILE:
1116 case EXT4_DE_CHRDEV:
1118 case EXT4_DE_BLKDEV:
1124 case EXT4_DE_SYMLINK:
1132 static int winsock_init(void)
1136 static WSADATA wsaData;
1137 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1145 static void winsock_fini(void)