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/file_dev.h"
53 #include "../blockdev/windows/file_windows.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)(const 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 static int device_register(const char *p);
157 static int mount(const char *p);
158 static int umount(const char *p);
159 static int mount_point_stats(const char *p);
160 static int cache_write_back(const char *p);
161 static int fremove(const char *p);
162 static int file_open(const char *p);
163 static int file_close(const char *p);
164 static int file_read(const char *p);
165 static int file_write(const char *p);
166 static int file_seek(const char *p);
167 static int file_tell(const char *p);
168 static int file_size(const char *p);
169 static int dir_rm(const char *p);
170 static int dir_mk(const char *p);
171 static int dir_open(const char *p);
172 static int dir_close(const char *p);
173 static int dir_close(const char *p);
174 static int dir_entry_get(const char *p);
176 static int multi_fcreate(const char *p);
177 static int multi_fwrite(const char *p);
178 static int multi_fread(const char *p);
179 static int multi_fremove(const char *p);
180 static int multi_dcreate(const char *p);
181 static int multi_dremove(const char *p);
182 static int stats_save(const char *p);
183 static int stats_check(const 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 file_open, /*PARAMS(2): fid path flags */
194 file_close, /*PARAMS(1): fid */
195 file_read, /*PARAMS(4): fid 0 len 0 */
196 file_write, /*PARAMS(4): fid 0 len 0 */
197 file_seek, /*PARAMS(2): fid off origin */
198 file_tell, /*PARAMS(2): fid exp */
199 file_size, /*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(const 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 static int device_register(const char *p)
389 if (sscanf(p, "%d %d %s", &dev, &cache_mode, dev_name) != 3) {
390 printf("Param list error\n");
396 file_windows_name_set(ext4_fname);
397 bd = file_windows_dev_get();
402 file_dev_name_set(ext4_fname);
406 ext4_device_unregister_all();
408 return ext4_device_register(bd, dev_name);
411 static int mount(const char *p)
414 char mount_point[32];
417 if (sscanf(p, "%s %s", dev_name, mount_point) != 2) {
418 printf("Param list error\n");
423 ext4_dmask_set(DEBUG_ALL);
425 rc = ext4_mount(dev_name, mount_point, false);
429 rc = ext4_recover(mount_point);
430 if (rc != EOK && rc != ENOTSUP)
433 rc = ext4_journal_start(mount_point);
438 ext4_cache_write_back(mount_point, 1);
442 static int umount(const char *p)
444 char mount_point[32];
447 if (sscanf(p, "%s", mount_point) != 1) {
448 printf("Param list error\n");
453 ext4_cache_write_back(mount_point, 0);
455 rc = ext4_journal_stop(mount_point);
459 rc = ext4_umount(mount_point);
466 static int mount_point_stats(const char *p)
468 char mount_point[32];
471 struct ext4_mount_stats stats;
473 if (sscanf(p, "%s %d", mount_point, &d) != 2) {
474 printf("Param list error\n");
478 rc = ext4_mount_point_stats(mount_point, &stats);
484 printf("\tinodes_count = %" PRIu32"\n", stats.inodes_count);
485 printf("\tfree_inodes_count = %" PRIu32"\n",
486 stats.free_inodes_count);
487 printf("\tblocks_count = %" PRIu64"\n", stats.blocks_count);
488 printf("\tfree_blocks_count = %" PRIu64"\n",
489 stats.free_blocks_count);
490 printf("\tblock_size = %" PRIu32"\n", stats.block_size);
491 printf("\tblock_group_count = %" PRIu32"\n",
492 stats.block_group_count);
493 printf("\tblocks_per_group = %" PRIu32"\n",
494 stats.blocks_per_group);
495 printf("\tinodes_per_group = %" PRIu32"\n",
496 stats.inodes_per_group);
497 printf("\tvolume_name = %s\n", stats.volume_name);
503 static int cache_write_back(const char *p)
505 char mount_point[32];
508 if (sscanf(p, "%s %d", mount_point, &en) != 2) {
509 printf("Param list error\n");
513 return ext4_cache_write_back(mount_point, en);
516 static int fremove(const char *p)
520 if (sscanf(p, "%s", path) != 1) {
521 printf("Param list error\n");
525 return ext4_fremove(path);
528 static int file_open(const char *p)
535 if (sscanf(p, "%d %s %s", &fid, path, flags) != 3) {
536 printf("Param list error\n");
540 if (!(fid < MAX_FILES)) {
541 printf("File id too big\n");
545 rc = ext4_fopen(&file_tab[fid].fd, path, flags);
548 strcpy(file_tab[fid].name, path);
553 static int file_close(const char *p)
558 if (sscanf(p, "%d", &fid) != 1) {
559 printf("Param list error\n");
563 if (!(fid < MAX_FILES)) {
564 printf("File id too big\n");
568 if (file_tab[fid].name[0] == 0) {
569 printf("File id empty\n");
573 rc = ext4_fclose(&file_tab[fid].fd);
576 file_tab[fid].name[0] = 0;
581 static int file_read(const char *p)
589 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
590 printf("Param list error\n");
594 if (!(fid < MAX_FILES)) {
595 printf("File id too big\n");
599 if (file_tab[fid].name[0] == 0) {
600 printf("File id empty\n");
605 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
607 memset(read_buffer, 0, MAX_RW_BUFFER);
608 rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb);
614 printf("Read count error\n");
618 if (memcmp(read_buffer, write_buffer, d)) {
619 printf("Read compare error\n");
629 static int file_write(const const char *p)
638 if (sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4) {
639 printf("Param list error\n");
643 if (!(fid < MAX_FILES)) {
644 printf("File id too big\n");
648 if (file_tab[fid].name[0] == 0) {
649 printf("File id empty\n");
654 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
655 rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb);
661 printf("Write count error\n");
671 static int file_seek(const char *p)
677 if (sscanf(p, "%d %d %d", &fid, &off, &origin) != 3) {
678 printf("Param list error\n");
682 if (!(fid < MAX_FILES)) {
683 printf("File id too big\n");
687 if (file_tab[fid].name[0] == 0) {
688 printf("File id empty\n");
692 return ext4_fseek(&file_tab[fid].fd, off, origin);
695 static int file_tell(const char *p)
700 if (sscanf(p, "%d %u", &fid, &exp_pos) != 2) {
701 printf("Param list error\n");
705 if (!(fid < MAX_FILES)) {
706 printf("File id too big\n");
710 if (file_tab[fid].name[0] == 0) {
711 printf("File id empty\n");
715 if (exp_pos != ext4_ftell(&file_tab[fid].fd)) {
716 printf("Expected filepos error\n");
723 static int file_size(const char *p)
728 if (sscanf(p, "%d %u", &fid, &exp_size) != 2) {
729 printf("Param list error\n");
733 if (!(fid < MAX_FILES)) {
734 printf("File id too big\n");
738 if (file_tab[fid].name[0] == 0) {
739 printf("File id empty\n");
743 if (exp_size != ext4_fsize(&file_tab[fid].fd)) {
744 printf("Expected filesize error\n");
751 static int dir_rm(const char *p)
755 if (sscanf(p, "%s", path) != 1) {
756 printf("Param list error\n");
760 return ext4_dir_rm(path);
763 static int dir_mk(const char *p)
767 if (sscanf(p, "%s", path) != 1) {
768 printf("Param list error\n");
772 return ext4_dir_mk(path);
775 static int dir_open(const char *p)
781 if (sscanf(p, "%d %s", &did, path) != 2) {
782 printf("Param list error\n");
786 if (!(did < MAX_DIRS)) {
787 printf("Dir id too big\n");
791 rc = ext4_dir_open(&dir_tab[did].fd, path);
794 strcpy(dir_tab[did].name, path);
799 static int dir_close(const char *p)
804 if (sscanf(p, "%d", &did) != 1) {
805 printf("Param list error\n");
809 if (!(did < MAX_DIRS)) {
810 printf("Dir id too big\n");
814 if (dir_tab[did].name[0] == 0) {
815 printf("Dir id empty\n");
819 rc = ext4_dir_close(&dir_tab[did].fd);
822 dir_tab[did].name[0] = 0;
827 static int dir_entry_get(const char *p)
833 if (sscanf(p, "%d %d", &did, &exp) != 2) {
834 printf("Param list error\n");
838 if (!(did < MAX_DIRS)) {
839 printf("Dir id too big\n");
843 if (dir_tab[did].name[0] == 0) {
844 printf("Dir id empty\n");
849 const ext4_direntry *d;
851 while ((d = ext4_dir_entry_next(&dir_tab[did].fd)) != NULL) {
854 memcpy(name, d->name, d->name_length);
855 name[d->name_length] = 0;
857 printf("\t%s %s\n", entry_to_str(d->inode_type), name);
862 printf("Minumum dir entry error\n");
866 if ((idx - 2) != exp) {
867 printf("Expected dir entry error\n");
874 static int multi_fcreate(const char *p)
884 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
885 printf("Param list error\n");
889 for (i = 0; i < cnt; ++i) {
890 sprintf(path1, "%s%s%d", path, prefix, i);
891 rc = ext4_fopen(&fd, path1, "wb+");
900 static int multi_fwrite(const char *p)
911 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
912 printf("Param list error\n");
916 for (i = 0; i < cnt; ++i) {
917 sprintf(path1, "%s%s%d", path, prefix, i);
918 rc = ext4_fopen(&fd, path1, "rb+");
925 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
926 rc = ext4_fwrite(&fd, write_buffer, d, &wb);
932 printf("Write count error\n");
943 static int multi_fread(const char *p)
954 if (sscanf(p, "%s %s %d %d", path, prefix, &cnt, &ll) != 4) {
955 printf("Param list error\n");
959 for (i = 0; i < cnt; ++i) {
960 sprintf(path1, "%s%s%d", path, prefix, i);
961 rc = ext4_fopen(&fd, path1, "rb+");
968 d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len;
970 memset(read_buffer, 0, MAX_RW_BUFFER);
971 rc = ext4_fread(&fd, read_buffer, d, &rb);
977 printf("Read count error\n");
981 if (memcmp(read_buffer, write_buffer, d)) {
982 printf("Read compare error\n");
993 static int multi_fremove(const char *p)
1000 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1001 printf("Param list error\n");
1005 for (i = 0; i < cnt; ++i) {
1006 sprintf(path1, "%s%s%d", path, prefix, i);
1007 rc = ext4_fremove(path1);
1015 static int multi_dcreate(const char *p)
1022 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1023 printf("Param list error\n");
1027 for (i = 0; i < cnt; ++i) {
1028 sprintf(path1, "%s%s%d", path, prefix, i);
1029 rc = ext4_dir_mk(path1);
1037 static int multi_dremove(const char *p)
1044 if (sscanf(p, "%s %s %d", path, prefix, &cnt) != 3) {
1045 printf("Param list error\n");
1049 for (i = 0; i < cnt; ++i) {
1050 sprintf(path1, "%s%s%d", path, prefix, i);
1051 rc = ext4_dir_rm(path1);
1059 struct ext4_mount_stats saved_stats;
1061 static int stats_save(const char *p)
1065 if (sscanf(p, "%s", path) != 1) {
1066 printf("Param list error\n");
1070 return ext4_mount_point_stats(path, &saved_stats);
1073 static int stats_check(const char *p)
1078 struct ext4_mount_stats actual_stats;
1080 if (sscanf(p, "%s", path) != 1) {
1081 printf("Param list error\n");
1085 rc = ext4_mount_point_stats(path, &actual_stats);
1090 if (memcmp(&saved_stats, &actual_stats,
1091 sizeof(struct ext4_mount_stats))) {
1093 printf("\tMount point stats error:\n");
1094 printf("\tsaved_stats:\n");
1095 printf("\tinodes_count = %" PRIu32"\n",
1096 saved_stats.inodes_count);
1097 printf("\tfree_inodes_count = %" PRIu32"\n",
1098 saved_stats.free_inodes_count);
1099 printf("\tblocks_count = %" PRIu64"\n",
1100 saved_stats.blocks_count);
1101 printf("\tfree_blocks_count = %" PRIu64"\n",
1102 saved_stats.free_blocks_count);
1103 printf("\tblock_size = %" PRIu32"\n",
1104 saved_stats.block_size);
1105 printf("\tblock_group_count = %" PRIu32"\n",
1106 saved_stats.block_group_count);
1107 printf("\tblocks_per_group = %" PRIu32"\n",
1108 saved_stats.blocks_per_group);
1109 printf("\tinodes_per_group = %" PRIu32"\n",
1110 saved_stats.inodes_per_group);
1111 printf("\tvolume_name = %s\n", saved_stats.volume_name);
1112 printf("\tactual_stats:\n");
1113 printf("\tinodes_count = %" PRIu32"\n",
1114 actual_stats.inodes_count);
1115 printf("\tfree_inodes_count = %" PRIu32"\n",
1116 actual_stats.free_inodes_count);
1117 printf("\tblocks_count = %" PRIu64"\n",
1118 actual_stats.blocks_count);
1119 printf("\tfree_blocks_count = %" PRIu64"\n",
1120 actual_stats.free_blocks_count);
1121 printf("\tblock_size = %d\n", actual_stats.block_size);
1122 printf("\tblock_group_count = %" PRIu32"\n",
1123 actual_stats.block_group_count);
1124 printf("\tblocks_per_group = %" PRIu32"\n",
1125 actual_stats.blocks_per_group);
1126 printf("\tinodes_per_group = %" PRIu32"\n",
1127 actual_stats.inodes_per_group);
1128 printf("\tvolume_name = %s\n",
1129 actual_stats.volume_name);
1137 static char *entry_to_str(uint8_t type)
1140 case EXT4_DE_UNKNOWN:
1142 case EXT4_DE_REG_FILE:
1146 case EXT4_DE_CHRDEV:
1148 case EXT4_DE_BLKDEV:
1154 case EXT4_DE_SYMLINK:
1162 static int winsock_init(void)
1166 static WSADATA wsaData;
1167 rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
1175 static void winsock_fini(void)