From 52a1caf80302fcaa86466785cd036bdfab2d2298 Mon Sep 17 00:00:00 2001 From: gkostka Date: Sun, 6 Apr 2014 15:58:29 +0000 Subject: [PATCH] Client server basic test suite + minor code improvment. --- CMakeLists.txt | 75 +- Makefile | 8 +- blockdev/filedev/ext4_filedev.c | 10 - blockdev/filedev/ext4_filedev.h | 5 +- blockdev/{io_raw => filedev_win}/io_raw.c | 10 - blockdev/{io_raw => filedev_win}/io_raw.h | 4 +- demos/generic/generic.cmake | 9 + demos/generic/main.c | 17 +- demos/stm32f429_disco/main.c | 5 +- demos/stm32f429_disco/stm32f429_demo.cmake | 38 + fs_test.mk | 421 ++++++++ fs_test/fs_test.cmake | 14 + fs_test/lwext4_client.c | 194 ++++ fs_test/lwext4_server.c | 1071 ++++++++++++++++++++ lwext4/ext4.c | 46 +- lwext4/ext4_ialloc.c | 2 +- 16 files changed, 1814 insertions(+), 115 deletions(-) rename blockdev/{io_raw => filedev_win}/io_raw.c (91%) rename blockdev/{io_raw => filedev_win}/io_raw.h (93%) create mode 100644 demos/generic/generic.cmake create mode 100644 demos/stm32f429_disco/stm32f429_demo.cmake create mode 100644 fs_test.mk create mode 100644 fs_test/fs_test.cmake create mode 100644 fs_test/lwext4_client.c create mode 100644 fs_test/lwext4_server.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ab30180..2f630fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,71 +7,24 @@ include_directories(. lwext4) aux_source_directory(lwext4 LWEXT4_SRC) add_library(lwext4 ${LWEXT4_SRC}) - -#EXECUTABLE - +#Examples if(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m3) -#Library size print -add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) - + #Library size print + add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL cortex-m4) -#Library size print -add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) - -#Discoery disco demo -enable_language(ASM) -set (STM32F429_DEMO_ASM - demos/stm32f429_disco/startup.S -) - -include_directories(demos/stm32f429_disco) -include_directories(demos/stm32f429_disco/cmsis) -include_directories(demos/stm32f429_disco/stm/lcd_utils) -include_directories(demos/stm32f429_disco/stm/stm32f4_spl/inc) -include_directories(demos/stm32f429_disco/stm/stm32f429) -include_directories(demos/stm32f429_disco/stm/usb_dev/Core/inc) -include_directories(demos/stm32f429_disco/stm/usb_host/Core/inc) -include_directories(demos/stm32f429_disco/stm/usb_host/Class/MSC/inc) -include_directories(demos/stm32f429_disco/stm/usb_otg/inc) -include_directories(demos/stm32f429_disco/stm/usb_user) - -aux_source_directory(demos/stm32f429_disco STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/cmsis STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/lcd_utils STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/stm32f4_spl/src STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/stm32f429 STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/usb_host/Core/src STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/usb_host/Class/MSC/src STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/usb_otg/src STM32F429_DEMO) -aux_source_directory(demos/stm32f429_disco/stm/usb_user STM32F429_DEMO) - -add_executable(stm324f29_demo ${STM32F429_DEMO} ${STM32F429_DEMO_ASM}) - -set_target_properties(stm324f29_demo PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter") -set_target_properties(stm324f29_demo PROPERTIES COMPILE_FLAGS "-Wno-format") -set_target_properties(stm324f29_demo PROPERTIES COMPILE_DEFINITIONS "STM32F429_439xx") - -set_target_properties(stm324f29_demo PROPERTIES LINK_FLAGS "-T${CMAKE_SOURCE_DIR}/demos/stm32f429_disco/stm32f429.ld") -target_link_libraries(stm324f29_demo lwext4) - -add_custom_target(stm32f429_size ALL DEPENDS stm324f29_demo COMMAND ${SIZE} -B stm324f29_demo) - + #Library size print + add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) + #Discoery disco demo + include(demos/stm32f429_disco/stm32f429_demo.cmake) elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL bf518) -#Library size print -add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) - + #Library size print + add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND ${SIZE} -B liblwext4.a) else() -#Generic example target -include_directories(blockdev/filedev) -include_directories(blockdev/io_raw) - -aux_source_directory(blockdev/filedev GENERIC_SRC) -aux_source_directory(blockdev/io_raw GENERIC_SRC) -aux_source_directory(demos/generic GENERIC_SRC) - -add_executable(fileimage_demo ${GENERIC_SRC}) -target_link_libraries(fileimage_demo lwext4) -add_custom_target(size ALL DEPENDS lwext4 COMMAND size -B liblwext4.a) + #Library size print + add_custom_target(lib_size ALL DEPENDS lwext4 COMMAND size -B liblwext4.a) + #Generic example target + include(demos/generic/generic.cmake) + include(fs_test/fs_test.cmake) endif() #DISTRIBUTION diff --git a/Makefile b/Makefile index 5a57c19..f6a0ac1 100644 --- a/Makefile +++ b/Makefile @@ -32,11 +32,17 @@ cortex-m4: all: generic bf518 cortex-m3 cortex-m4 generic - clean: rm -R -f build_bf518 rm -R -f build_cortex-m3 rm -R -f build_cortex-m4 rm -R -f build_generic + +include fs_test.mk + + + + + \ No newline at end of file diff --git a/blockdev/filedev/ext4_filedev.c b/blockdev/filedev/ext4_filedev.c index 09c19ef..ea3ba7c 100644 --- a/blockdev/filedev/ext4_filedev.c +++ b/blockdev/filedev/ext4_filedev.c @@ -68,9 +68,6 @@ EXT4_BLOCKDEV_STATIC_INSTANCE( filedev_close ); -/******************************************************************************/ -EXT4_BCACHE_STATIC_INSTANCE(__cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024); - /******************************************************************************/ static int filedev_open(struct ext4_blockdev *bdev) { @@ -137,13 +134,6 @@ static int filedev_close(struct ext4_blockdev *bdev) return EOK; } - -/******************************************************************************/ - -struct ext4_bcache* ext4_filecache_get(void) -{ - return &__cache; -} /******************************************************************************/ struct ext4_blockdev* ext4_filedev_get(void) { diff --git a/blockdev/filedev/ext4_filedev.h b/blockdev/filedev/ext4_filedev.h index b37ee9b..cbd8aa5 100644 --- a/blockdev/filedev/ext4_filedev.h +++ b/blockdev/filedev/ext4_filedev.h @@ -34,13 +34,10 @@ #include #include -/**@brief Filecache get.*/ -struct ext4_bcache* ext4_filecache_get(void); - /**@brief File blockdev get.*/ struct ext4_blockdev* ext4_filedev_get(void); -/**@brief Filename set.*/ +/**@brief Set filename to open.*/ void ext4_filedev_filename(const char *n); #endif /* EXT4_FILEDEV_H_ */ diff --git a/blockdev/io_raw/io_raw.c b/blockdev/filedev_win/io_raw.c similarity index 91% rename from blockdev/io_raw/io_raw.c rename to blockdev/filedev_win/io_raw.c index 66b35b4..6af3861 100644 --- a/blockdev/io_raw/io_raw.c +++ b/blockdev/filedev_win/io_raw.c @@ -70,9 +70,6 @@ EXT4_BLOCKDEV_STATIC_INSTANCE( io_raw_close ); -/******************************************************************************/ -EXT4_BCACHE_STATIC_INSTANCE(__cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024); - /******************************************************************************/ static int io_raw_open(struct ext4_blockdev *bdev) { @@ -179,13 +176,6 @@ static int io_raw_close(struct ext4_blockdev *bdev) return EOK; } - -/******************************************************************************/ - -struct ext4_bcache* ext4_io_raw_cache_get(void) -{ - return &__cache; -} /******************************************************************************/ struct ext4_blockdev* ext4_io_raw_dev_get(void) { diff --git a/blockdev/io_raw/io_raw.h b/blockdev/filedev_win/io_raw.h similarity index 93% rename from blockdev/io_raw/io_raw.h rename to blockdev/filedev_win/io_raw.h index cf097aa..742f49e 100644 --- a/blockdev/io_raw/io_raw.h +++ b/blockdev/filedev_win/io_raw.h @@ -35,12 +35,10 @@ #include -/**@brief IO raw get.*/ -struct ext4_bcache* ext4_io_raw_cache_get(void); - /**@brief IO raw blockdev get.*/ struct ext4_blockdev* ext4_io_raw_dev_get(void); +/**@brief Set filrname to open.*/ void ext4_io_raw_filename(const char *n); #endif /* IO_RAW_H_ */ diff --git a/demos/generic/generic.cmake b/demos/generic/generic.cmake new file mode 100644 index 0000000..ae89fd3 --- /dev/null +++ b/demos/generic/generic.cmake @@ -0,0 +1,9 @@ +include_directories(blockdev/filedev) +include_directories(blockdev/filedev_win) + +aux_source_directory(blockdev/filedev GENERIC_SRC) +aux_source_directory(blockdev/filedev_win GENERIC_SRC) +aux_source_directory(demos/generic GENERIC_SRC) + +add_executable(fileimage_demo ${GENERIC_SRC}) +target_link_libraries(fileimage_demo lwext4) \ No newline at end of file diff --git a/demos/generic/main.c b/demos/generic/main.c index d210dca..fe304e6 100644 --- a/demos/generic/main.c +++ b/demos/generic/main.c @@ -79,8 +79,11 @@ static uint8_t *rd_buff; /**@brief Block device handle.*/ static struct ext4_blockdev *bd; +/**@brief Static cache instance*/ +EXT4_BCACHE_STATIC_INSTANCE(_lwext4_cache, CONFIG_BLOCK_DEV_CACHE_SIZE, 1024); + /**@brief Block cache handle.*/ -static struct ext4_bcache *bc; +static struct ext4_bcache *bc = &_lwext4_cache; static const char *usage = " \n\ Welcome in ext4 generic demo. \n\ @@ -97,6 +100,10 @@ Usage: \n\ --wpart - windows partition mode \n\ \n"; + + + + static char* entry_to_str(uint8_t type) { switch(type){ @@ -370,8 +377,7 @@ static bool open_filedev(void) { ext4_filedev_filename(input_name); bd = ext4_filedev_get(); - bc = ext4_filecache_get(); - if(!bd || !bc){ + if(!bd){ printf("Block device ERROR\n"); return false; } @@ -383,14 +389,13 @@ static bool open_winpartition(void) #ifdef WIN32 ext4_io_raw_filename(input_name); bd = ext4_io_raw_dev_get(); - bc = ext4_io_raw_cache_get(); - if(!bd || !bc){ + if(!bd){ printf("Block device ERROR\n"); return false; } return true; #else - printf("open_winpartition: this mode shouls be used only under windows !\n"); + printf("open_winpartition: this mode should be used only under windows !\n"); return false; #endif } diff --git a/demos/stm32f429_disco/main.c b/demos/stm32f429_disco/main.c index f72ef04..02b0dde 100644 --- a/demos/stm32f429_disco/main.c +++ b/demos/stm32f429_disco/main.c @@ -57,7 +57,7 @@ static int rw_szie = READ_WRITE_SZIZE; static int rw_count = 100; /**@brief Directory test count*/ -static int dir_cnt = 10; +static int dir_cnt = 50; /**@brief Static or dynamic cache mode*/ static bool cache_mode = false; @@ -215,6 +215,7 @@ static bool dir_test(int len) } + ext4_cache_write_back("/mp/", 1); printf("Add files to: /mp/dir1\n"); for (i = 0; i < len; ++i) { sprintf(path, "/mp/dir1/f%d", i); @@ -224,7 +225,7 @@ static bool dir_test(int len) return false; } } - + ext4_cache_write_back("/mp/", 0); stop = get_ms(); diff = stop - start; dir_ls("/mp/dir1"); diff --git a/demos/stm32f429_disco/stm32f429_demo.cmake b/demos/stm32f429_disco/stm32f429_demo.cmake new file mode 100644 index 0000000..44474ec --- /dev/null +++ b/demos/stm32f429_disco/stm32f429_demo.cmake @@ -0,0 +1,38 @@ +#Discoery disco demo +enable_language(ASM) +set (STM32F429_DEMO_ASM + demos/stm32f429_disco/startup.S +) + + +include_directories(demos/stm32f429_disco) +include_directories(demos/stm32f429_disco/cmsis) +include_directories(demos/stm32f429_disco/stm/lcd_utils) +include_directories(demos/stm32f429_disco/stm/stm32f4_spl/inc) +include_directories(demos/stm32f429_disco/stm/stm32f429) +include_directories(demos/stm32f429_disco/stm/usb_dev/Core/inc) +include_directories(demos/stm32f429_disco/stm/usb_host/Core/inc) +include_directories(demos/stm32f429_disco/stm/usb_host/Class/MSC/inc) +include_directories(demos/stm32f429_disco/stm/usb_otg/inc) +include_directories(demos/stm32f429_disco/stm/usb_user) + +aux_source_directory(demos/stm32f429_disco STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/cmsis STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/lcd_utils STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/stm32f4_spl/src STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/stm32f429 STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/usb_host/Core/src STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/usb_host/Class/MSC/src STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/usb_otg/src STM32F429_DEMO) +aux_source_directory(demos/stm32f429_disco/stm/usb_user STM32F429_DEMO) + +add_executable(stm324f29_demo ${STM32F429_DEMO} ${STM32F429_DEMO_ASM}) + +set_target_properties(stm324f29_demo PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter") +set_target_properties(stm324f29_demo PROPERTIES COMPILE_FLAGS "-Wno-format") +set_target_properties(stm324f29_demo PROPERTIES COMPILE_DEFINITIONS "STM32F429_439xx") + +set_target_properties(stm324f29_demo PROPERTIES LINK_FLAGS "-T${CMAKE_SOURCE_DIR}/demos/stm32f429_disco/stm32f429.ld") +target_link_libraries(stm324f29_demo lwext4) + +add_custom_target(stm32f429_size ALL DEPENDS stm324f29_demo COMMAND ${SIZE} -B stm324f29_demo) \ No newline at end of file diff --git a/fs_test.mk b/fs_test.mk new file mode 100644 index 0000000..836dff4 --- /dev/null +++ b/fs_test.mk @@ -0,0 +1,421 @@ + + +LWEXT4_CLIENT = @build_generic\\lwext4_client + +TEST_DIR = /test + +t0: + @echo "T0: Device regoster test:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + +t1: + @echo "T1: Single mount-umount test:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "umount /" + +t2: + @echo "T2: Multiple mount-umount test:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "umount /" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "umount /" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "umount /" + +t3: + @echo "T3: Test dir create/remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 0" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t4: + @echo "T4: 10 files create + write + read + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 10" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 10 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 10 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 10" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_fremove $(TEST_DIR) /f 10" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t5: + @echo "T5: 100 files create + write + read + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 100" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 100 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 100 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 100" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_fremove $(TEST_DIR) /f 100" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t6: + @echo "T6: 1000 files create + write + read + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 1000" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 1000 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 1000 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 1000" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_fremove $(TEST_DIR) /f 1000" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t7: + @echo "T7: 10 dirs create + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 10" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 10" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_dremove $(TEST_DIR) /d 10" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t8: + @echo "T8: 100 dirs create + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 100" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 100" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_dremove $(TEST_DIR) /d 100" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t9: + @echo "T9: 1000 dirs create + remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 1000" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 1000" + $(LWEXT4_CLIENT) -c "dir_close 0" + $(LWEXT4_CLIENT) -c "multi_dremove $(TEST_DIR) /d 1000" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t10: + @echo "T10: 10 entries (dir) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 10" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 10" + $(LWEXT4_CLIENT) -c "dir_close 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t11: + @echo "T11: 100 entries (dir) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 100" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 100" + $(LWEXT4_CLIENT) -c "dir_close 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t12: + @echo "T12: 1000 entries (dir) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_dcreate $(TEST_DIR) /d 1000" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 1000" + $(LWEXT4_CLIENT) -c "dir_close 0" + + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t13: + @echo "T13: 10 entries (files) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 10" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 10 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 10 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 10" + $(LWEXT4_CLIENT) -c "dir_close 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t14: + @echo "T14: 100 entries (files) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 100" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 100 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 100 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 100" + $(LWEXT4_CLIENT) -c "dir_close 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t15: + @echo "T15: 1000 entries (files) dir recursive remove:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "multi_fcreate $(TEST_DIR) /f 1000" + $(LWEXT4_CLIENT) -c "multi_fwrite $(TEST_DIR) /f 1000 1024" + $(LWEXT4_CLIENT) -c "multi_fread $(TEST_DIR) /f 1000 1024" + $(LWEXT4_CLIENT) -c "dir_open 0 $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "dir_entry_get 0 1000" + $(LWEXT4_CLIENT) -c "dir_close 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + + +t16: + @echo "T16: 8kB file write/read:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "fopen 0 $(TEST_DIR)/test.txt wb+" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 0" + + $(LWEXT4_CLIENT) -c "fwrite 0 0 8192 0" + + $(LWEXT4_CLIENT) -c "ftell 0 8192" + $(LWEXT4_CLIENT) -c "fsize 0 8192" + + $(LWEXT4_CLIENT) -c "fseek 0 0 0" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 8192" + + $(LWEXT4_CLIENT) -c "fread 0 0 8192 0" + + $(LWEXT4_CLIENT) -c "ftell 0 8192" + $(LWEXT4_CLIENT) -c "fsize 0 8192" + + $(LWEXT4_CLIENT) -c "fclose 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t17: + @echo "T17: 64kB file write/read:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "fopen 0 $(TEST_DIR)/test.txt wb+" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 0" + + $(LWEXT4_CLIENT) -c "fwrite 0 0 65536 0" + + $(LWEXT4_CLIENT) -c "ftell 0 65536" + $(LWEXT4_CLIENT) -c "fsize 0 65536" + + $(LWEXT4_CLIENT) -c "fseek 0 0 0" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 65536" + + $(LWEXT4_CLIENT) -c "fread 0 0 65536 0" + + $(LWEXT4_CLIENT) -c "ftell 0 65536" + $(LWEXT4_CLIENT) -c "fsize 0 65536" + + $(LWEXT4_CLIENT) -c "fclose 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t18: + @echo "T18: 512kB file write/read:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "fopen 0 $(TEST_DIR)/test.txt wb+" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 0" + + $(LWEXT4_CLIENT) -c "fwrite 0 0 524288 0" + + $(LWEXT4_CLIENT) -c "ftell 0 524288" + $(LWEXT4_CLIENT) -c "fsize 0 524288" + + $(LWEXT4_CLIENT) -c "fseek 0 0 0" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 524288" + + $(LWEXT4_CLIENT) -c "fread 0 0 524288 0" + + $(LWEXT4_CLIENT) -c "ftell 0 524288" + $(LWEXT4_CLIENT) -c "fsize 0 524288" + + $(LWEXT4_CLIENT) -c "fclose 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t19: + @echo "T19: 4MB file write/read:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "fopen 0 $(TEST_DIR)/test.txt wb+" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 0" + + $(LWEXT4_CLIENT) -c "fwrite 0 0 4194304 0" + + $(LWEXT4_CLIENT) -c "ftell 0 4194304" + $(LWEXT4_CLIENT) -c "fsize 0 4194304" + + $(LWEXT4_CLIENT) -c "fseek 0 0 0" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 4194304" + + $(LWEXT4_CLIENT) -c "fread 0 0 4194304 0" + + $(LWEXT4_CLIENT) -c "ftell 0 4194304" + $(LWEXT4_CLIENT) -c "fsize 0 4194304" + + $(LWEXT4_CLIENT) -c "fclose 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + +t20: + @echo "T10: 32MB file write/read:" + $(LWEXT4_CLIENT) -c "device_register 0 0 bdev" + $(LWEXT4_CLIENT) -c "mount bdev /" + $(LWEXT4_CLIENT) -c "stats_save /" + $(LWEXT4_CLIENT) -c "dir_mk $(TEST_DIR)" + + $(LWEXT4_CLIENT) -c "fopen 0 $(TEST_DIR)/test.txt wb+" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 0" + + $(LWEXT4_CLIENT) -c "fwrite 0 0 33554432 0" + + $(LWEXT4_CLIENT) -c "ftell 0 33554432" + $(LWEXT4_CLIENT) -c "fsize 0 33554432" + + $(LWEXT4_CLIENT) -c "fseek 0 0 0" + + $(LWEXT4_CLIENT) -c "ftell 0 0" + $(LWEXT4_CLIENT) -c "fsize 0 33554432" + + $(LWEXT4_CLIENT) -c "fread 0 0 33554432 0" + + $(LWEXT4_CLIENT) -c "ftell 0 33554432" + $(LWEXT4_CLIENT) -c "fsize 0 33554432" + + $(LWEXT4_CLIENT) -c "fclose 0" + + $(LWEXT4_CLIENT) -c "dir_rm $(TEST_DIR)" + $(LWEXT4_CLIENT) -c "stats_check /" + $(LWEXT4_CLIENT) -c "umount /" + + + +all_tests: t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 \ No newline at end of file diff --git a/fs_test/fs_test.cmake b/fs_test/fs_test.cmake new file mode 100644 index 0000000..affbe89 --- /dev/null +++ b/fs_test/fs_test.cmake @@ -0,0 +1,14 @@ +include_directories(blockdev/filedev) +include_directories(blockdev/filedev_win) + +aux_source_directory(blockdev/filedev BLOCKDEV_SRC) +aux_source_directory(blockdev/filedev_win BLOCKDEV_SRC) + + +add_executable(lwext4_server fs_test/lwext4_server.c ${BLOCKDEV_SRC}) +target_link_libraries(lwext4_server lwext4) +target_link_libraries(lwext4_server ws2_32) + +add_executable(lwext4_client fs_test/lwext4_client.c ${BLOCKDEV_SRC}) +target_link_libraries(lwext4_client lwext4) +target_link_libraries(lwext4_client ws2_32) \ No newline at end of file diff --git a/fs_test/lwext4_client.c b/fs_test/lwext4_client.c new file mode 100644 index 0000000..ae77961 --- /dev/null +++ b/fs_test/lwext4_client.c @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#include +#include + +static int inet_pton(int af, const char *src, void *dst); + +#else +#include +#include +#include +#include +#endif + +static int winsock_init(void); +static void winsock_fini(void); + +/**@brief Default server addres.*/ +static char *server_addr = "127.0.0.1"; + +/**@brief Default connection port.*/ +static int connection_port = 8888; + +/**@brief Call op*/ +static char *op_code; + +static const char *usage = " \n\ +Welcome in lwext4_client. \n\ +Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\ +Usage: \n\ + --call (-c) - call opt \n\ + --port (-p) - server port \n\ + --addr (-a) - server ip address \n\ +\n"; + + + +static int client_connect(void) +{ + int fd = 0; + struct sockaddr_in serv_addr; + + if(winsock_init() < 0) { + printf("winsock_init error\n"); + exit(-1); + } + + memset(&serv_addr, '0', sizeof(serv_addr)); + fd = socket(AF_INET, SOCK_STREAM, 0); + if(fd < 0) { + printf("socket() error: %s\n", strerror(errno)); + exit(-1); + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(connection_port); + + if(!inet_pton(AF_INET, server_addr, &serv_addr.sin_addr)){ + printf("inet_pton() error\n"); + exit(-1); + } + + if(connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))){ + printf("connect() error: %s\n", strerror(errno)); + exit(-1); + } + + return fd; +} + + + +static bool parse_opt(int argc, char **argv) +{ + int option_index = 0; + int c; + + static struct option long_options[] = + { + {"call", required_argument, 0, 'c'}, + {"port", required_argument, 0, 'p'}, + {"addr", required_argument, 0, 'a'}, + {0, 0, 0, 0} + }; + + while(-1 != (c = getopt_long (argc, argv, "c:p:a:", long_options, &option_index))) { + + switch(c){ + case 'a': + server_addr = optarg; + break; + case 'p': + connection_port = atoi(optarg); + break; + case 'c': + op_code = optarg; + break; + default: + printf("%s", usage); + return false; + + } + } + return true; +} + + +int main(int argc, char *argv[]) +{ + int sockfd; + int n; + int rc; + char recvBuff[1024]; + + if(!parse_opt(argc, argv)) + return -1; + + sockfd = client_connect(); + + + n = send(sockfd, op_code, strlen(op_code), 0); + if(n < 0) { + printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd); + return -1; + } + + n = recv(sockfd, (void *)&rc, sizeof(rc), 0); + if(n < 0) { + printf("\tWrite error: %s fd = %d\n", strerror(errno), sockfd); + return -1; + } + + printf("rc: %d %s\n", rc, strerror(rc)); + if(rc) + printf("\t%s\n",op_code); + + closesocket(sockfd); + return rc; +} + +static int winsock_init(void) +{ +#if WIN32 + int rc; + static WSADATA wsaData; + rc = WSAStartup(MAKEWORD(2,2), &wsaData); + if (rc != 0) { + return -1; + } +#endif + return 0; +} + +static void winsock_fini(void) +{ +#if WIN32 + WSACleanup(); +#endif +} + + +#if WIN32 +static int inet_pton(int af, const char *src, void *dst) +{ + struct sockaddr_storage ss; + int size = sizeof(ss); + char src_copy[INET6_ADDRSTRLEN+1]; + + ZeroMemory(&ss, sizeof(ss)); + /* stupid non-const API */ + strncpy (src_copy, src, INET6_ADDRSTRLEN+1); + src_copy[INET6_ADDRSTRLEN] = 0; + + if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) { + switch(af) { + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; + } + } + return 0; +} +#endif diff --git a/fs_test/lwext4_server.c b/fs_test/lwext4_server.c new file mode 100644 index 0000000..deb9bd6 --- /dev/null +++ b/fs_test/lwext4_server.c @@ -0,0 +1,1071 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#include +#else +#include +#include +#include +#include +#endif + +#include +#include +#include + +static int winsock_init(void); +static void winsock_fini(void); +static char* entry_to_str(uint8_t type); + +#define MAX_FILES 64 +#define MAX_DIRS 64 + +#define MAX_RW_BUFFER (1024 * 1024) +#define RW_BUFFER_PATERN ('x') + + +/**@brief Default connection port*/ +static int connection_port = 8888; + +/**@brief Default filesystem filename.*/ +static char *ext4_fname = "ext2"; + +/**@brief Verbose mode*/ +static int verbose = 0; + +static char read_buffer[MAX_RW_BUFFER]; +static char write_buffer[MAX_RW_BUFFER]; + + +static const char *usage = " \n\ +Welcome in lwext4_server. \n\ +Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) \n\ +Usage: \n\ + --image (-i) - ext2/3/4 image file \n\ + --port (-p) - server port \n\ + --verbose (-v) - verbose mode \n\ +\n"; + + + +/**@brief Open file instance descriptor.*/ +struct lwext4_files { + char name[255]; + ext4_file fd; +}; + +/**@brief Open directory instance descriptor.*/ +struct lwext4_dirs { + char name[255]; + ext4_dir fd; +}; + +/**@brief Library call opcode.*/ +struct lwext4_op_codes { + char *func; +}; + +/**@brief Library call wraper.*/ +struct lwext4_call { + int (*lwext4_call)(char *p); +}; + +/**@brief */ +static struct lwext4_files file_tab[MAX_FILES]; + +/**@brief */ +static struct lwext4_dirs dir_tab[MAX_DIRS]; + +/**@brief */ +static struct lwext4_op_codes op_codes[] = { + "device_register", + "mount", + "umount", + "mount_point_stats", + "cache_write_back", + "fremove", + "fopen", + "fclose", + "fread", + "fwrite", + "fseek", + "ftell", + "fsize", + "dir_rm", + "dir_mk", + "dir_open", + "dir_close", + "dir_entry_get", + + "multi_fcreate", + "multi_fwrite", + "multi_fread", + "multi_fremove", + "multi_dcreate", + "multi_dremove", + "stats_save", + "stats_check", +}; + +int _device_register(char *p); +int _mount(char *p); +int _umount(char *p); +int _mount_point_stats(char *p); +int _cache_write_back(char *p); +int _fremove(char *p); +int _fopen(char *p); +int _fclose(char *p); +int _fread(char *p); +int _fwrite(char *p); +int _fseek(char *p); +int _ftell(char *p); +int _fsize(char *p); +int _dir_rm(char *p); +int _dir_mk(char *p); +int _dir_open(char *p); +int _dir_close(char *p); +int _dir_close(char *p); +int _dir_entry_get(char *p); + +int _multi_fcreate(char *p); +int _multi_fwrite(char *p); +int _multi_fread(char *p); +int _multi_fremove(char *p); +int _multi_dcreate(char *p); +int _multi_dremove(char *p); +int _stats_save(char *p); +int _stats_check(char *p); + +/**@brief */ +static struct lwext4_call op_call[] = { + _device_register, /*PARAMS(3): 0 cache_mode dev_name */ + _mount, /*PARAMS(2): dev_name mount_point */ + _umount, /*PARAMS(1): mount_point */ + _mount_point_stats,/*PARAMS(2): mount_point, 0 */ + _cache_write_back, /*PARAMS(2): mount_point, en */ + _fremove, /*PARAMS(1): path */ + _fopen, /*PARAMS(2): fid path flags */ + _fclose, /*PARAMS(1): fid */ + _fread, /*PARAMS(4): fid 0 len 0 */ + _fwrite, /*PARAMS(4): fid 0 len 0 */ + _fseek, /*PARAMS(2): fid off origin */ + _ftell, /*PARAMS(2): fid exp */ + _fsize, /*PARAMS(2): fid exp */ + _dir_rm, /*PARAMS(1): path */ + _dir_mk, /*PARAMS(1): path */ + _dir_open, /*PARAMS(2): did, path */ + _dir_close, /*PARAMS(1): did */ + _dir_entry_get, /*PARAMS(2): did, exp */ + + _multi_fcreate, /*PARAMS(3): path prefix cnt */ + _multi_fwrite, /*PARAMS(4): path prefix cnt size */ + _multi_fread, /*PARAMS(4): path prefix cnt size */ + _multi_fremove, /*PARAMS(2): path prefix cnt */ + _multi_dcreate, /*PARAMS(3): path prefix cnt */ + _multi_dremove, /*PARAMS(2): path prefix */ + _stats_save, /*PARAMS(1): path */ + _stats_check, /*PARAMS(1): path */ +}; + +static clock_t get_ms(void) +{ + struct timeval t; + gettimeofday(&t, NULL); + return (t.tv_sec * 1000) + (t.tv_usec / 1000); +} + +/**@brief */ +static int exec_op_code(char *opcode) +{ + int i; + int r = -1; + + for (i = 0; i < sizeof(op_codes) / sizeof(op_codes[0]); ++i) { + + if(strncmp(op_codes[i].func, opcode, strlen(op_codes[i].func))) + continue; + + if(opcode[strlen(op_codes[i].func)] != ' ') + continue; + + printf("%s\n", opcode); + opcode += strlen(op_codes[i].func); + /*Call*/ + + clock_t t = get_ms(); + r = op_call[i].lwext4_call(opcode); + + printf("rc: %d, time: %ums\n", r, get_ms() - t); + + break; + } + + return r; +} + + +static int server_open(void) +{ + int fd = 0; + struct sockaddr_in serv_addr; + + memset(&serv_addr, 0, sizeof(serv_addr)); + + if(winsock_init() < 0) { + printf("winsock_init() error\n"); + exit(-1); + } + + fd = socket(AF_INET, SOCK_STREAM, 0); + if(fd < 0) { + printf("socket() error: %s\n", strerror(errno)); + exit(-1); + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + serv_addr.sin_port = htons(connection_port); + + if(bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))){ + printf("bind() error: %s\n", strerror(errno)); + exit(-1); + } + + if(listen(fd, 1)){ + printf("listen() error: %s\n", strerror(errno)); + exit(-1); + } + + return fd; +} + +static bool parse_opt(int argc, char **argv) +{ + int option_index = 0; + int c; + + static struct option long_options[] = { + {"image", required_argument, 0, 'i'}, + {"port", required_argument, 0, 'p'}, + {"verbose", required_argument, 0, 'v'}, + {0, 0, 0, 0} + }; + + while(-1 != (c = getopt_long (argc, argv, "i:p:v:", long_options, &option_index))) { + + switch(c){ + case 'i': + ext4_fname = optarg; + break; + case 'p': + connection_port = atoi(optarg); + break; + case 'v': + verbose = atoi(optarg); + break; + default: + printf("%s", usage); + return false; + + } + } + return true; +} + +int main(int argc, char *argv[]) +{ + int n; + int listenfd; + int connfd; + char op_code[128]; + + if(!parse_opt(argc, argv)) + return -1; + + ext4_filedev_filename(ext4_fname); + listenfd = server_open(); + + printf("lwext4_server: listening on port: %d\n", connection_port); + + memset(write_buffer, RW_BUFFER_PATERN, MAX_RW_BUFFER); + while(1) + { + connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); + + n = recv(connfd, op_code, sizeof(op_code), 0); + + if(n < 0) { + printf("recv() error: %s fd = %d\n", strerror(errno), connfd); + break; + } + + op_code[n] = 0; + + int r = exec_op_code(op_code); + + n = send(connfd, (void *)&r, sizeof(r), 0); + if(n < 0) { + printf("send() error: %s fd = %d\n", strerror(errno), connfd); + break; + } + + close(connfd); + } + + closesocket(listenfd); + winsock_fini(); + return 0; +} + + +int _device_register(char *p) +{ + int bd; + int cache_mode; + char dev_name[32]; + + if(sscanf(p, "%d %d %s", &bd, &cache_mode, dev_name) != 3){ + printf("Param list error\n"); + return -1; + } + + return ext4_device_register(ext4_filedev_get(), 0, dev_name); +} + +int _mount(char *p) +{ + char dev_name[32]; + char mount_point[32]; + int rc; + + if(sscanf(p, "%s %s", dev_name, mount_point) != 2){ + printf("Param list error\n"); + return -1; + } + + return ext4_mount(dev_name, mount_point); +} + +int _umount(char *p) +{ + char mount_point[32]; + + if(sscanf(p, "%s", mount_point) != 1){ + printf("Param list error\n"); + return -1; + } + + return ext4_umount(mount_point); +} + +int _mount_point_stats(char *p) +{ + char mount_point[32]; + int d; + int rc; + struct ext4_mount_stats stats; + + if(sscanf(p, "%s %d", mount_point, &d) != 2){ + printf("Param list error\n"); + return -1; + } + + rc = ext4_mount_point_stats(mount_point, &stats); + + if(rc != EOK) + return; + + if(verbose){ + printf("\tinodes_count = %d\n", stats.inodes_count); + printf("\tfree_inodes_count = %d\n", stats.free_inodes_count); + printf("\tblocks_count = %llu\n", stats.blocks_count); + printf("\tfree_blocks_count = %llu\n", stats.free_blocks_count); + + printf("\tblock_size = %d\n", stats.block_size); + printf("\tblock_group_count = %d\n", stats.block_group_count); + printf("\tblocks_per_group = %d\n", stats.blocks_per_group); + printf("\tinodes_per_group = %d\n", stats.inodes_per_group); + + printf("\tvolume_name = %s\n", stats.volume_name); + } + + return rc; +} + +int _cache_write_back(char *p) +{ + char mount_point[32]; + int en; + + if(sscanf(p, "%s %d", mount_point, &en) != 2){ + printf("Param list error\n"); + return -1; + } + + return ext4_cache_write_back(mount_point, en); +} + + +int _fremove(char *p) +{ + char path[255]; + + if(sscanf(p, "%s", path) != 1){ + printf("Param list error\n"); + return -1; + } + + return ext4_fremove(path); +} + +int _fopen(char *p) +{ + int fid = MAX_FILES; + char path[256]; + char flags[8]; + int rc; + + if(sscanf(p, "%d %s %s", &fid, path, flags) != 3){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + rc = ext4_fopen(&file_tab[fid].fd, path, flags); + + if(rc == EOK) + strcpy(file_tab[fid].name, path); + + return rc; +} + +int _fclose(char *p) +{ + int fid = MAX_FILES; + int rc; + + if(sscanf(p, "%d", &fid) != 1){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + rc = ext4_fclose(&file_tab[fid].fd); + + if(rc == EOK) + file_tab[fid].name[0] = 0; + + return rc; +} + +int _fread(char *p) +{ + int fid = MAX_FILES; + int len; + int d; + int rc; + int rb; + + if(sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + while(len){ + d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len; + + memset(read_buffer, 0, MAX_RW_BUFFER); + rc = ext4_fread(&file_tab[fid].fd, read_buffer, d, &rb); + + if(rc != EOK) + break; + + if(rb != d){ + printf("Read count error\n"); + return -1; + } + + if(memcmp(read_buffer, write_buffer, d)){ + printf("Read compare error\n"); + return -1; + } + + len -= d; + } + + return rc; +} + +int _fwrite(char *p) +{ + int fid = MAX_FILES; + int len; + int d; + int rc; + int wb; + + if(sscanf(p, "%d %d %d %d", &fid, &d, &len, &d) != 4){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + while(len){ + d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len; + rc = ext4_fwrite(&file_tab[fid].fd, write_buffer, d, &wb); + + if(rc != EOK) + break; + + if(wb != d){ + printf("Write count error\n"); + return -1; + } + + len -= d; + } + + return rc; +} + +int _fseek(char *p) +{ + int fid = MAX_FILES; + int off; + int origin; + + if(sscanf(p, "%d %d %d", &fid, &off, &origin) != 3){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + return ext4_fseek(&file_tab[fid].fd, off, origin); +} + +int _ftell(char *p) +{ + int fid = MAX_FILES; + uint32_t exp_pos; + + if(sscanf(p, "%d %u", &fid, &exp_pos) != 2){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + + if(exp_pos != ext4_ftell(&file_tab[fid].fd)){ + printf("Expected filepos error\n"); + return -1; + } + + return EOK; +} + +int _fsize(char *p) +{ + int fid = MAX_FILES; + uint32_t exp_size; + + if(sscanf(p, "%d %u", &fid, &exp_size) != 2){ + printf("Param list error\n"); + return -1; + } + + if(!(fid < MAX_FILES)){ + printf("File id too big\n"); + return -1; + } + + if(file_tab[fid].name[0] == 0){ + printf("File id empty\n"); + return -1; + } + + if(exp_size != ext4_fsize(&file_tab[fid].fd)){ + printf("Expected filesize error\n"); + return -1; + } + + return EOK; +} + +int _dir_rm(char *p) +{ + char path[255]; + + if(sscanf(p, "%s", path) != 1){ + printf("Param list error\n"); + return -1; + } + + return ext4_dir_rm(path); +} + +int _dir_mk(char *p) +{ + char path[255]; + + if(sscanf(p, "%s", path) != 1){ + printf("Param list error\n"); + return -1; + } + + return ext4_dir_mk(path); +} + +int _dir_open(char *p) +{ + int did = MAX_DIRS; + char path[255]; + int rc; + + if(sscanf(p, "%d %s", &did, path) != 2){ + printf("Param list error\n"); + return -1; + } + + if(!(did < MAX_DIRS)){ + printf("Dir id too big\n"); + return -1; + } + + rc = ext4_dir_open(&dir_tab[did].fd, path); + + if(rc == EOK) + strcpy(dir_tab[did].name, path); + + return rc; +} + +int _dir_close(char *p) +{ + int did = MAX_DIRS; + int rc; + + if(sscanf(p, "%d", &did) != 1){ + printf("Param list error\n"); + return -1; + } + + if(!(did < MAX_DIRS)){ + printf("Dir id too big\n"); + return -1; + } + + if(dir_tab[did].name[0] == 0){ + printf("Dir id empty\n"); + return -1; + } + + rc = ext4_dir_close(&dir_tab[did].fd); + + if(rc == EOK) + dir_tab[did].name[0] = 0; + + return rc; +} + +int _dir_entry_get(char *p) +{ + int did = MAX_DIRS; + int exp; + char name[256]; + + if(sscanf(p, "%d %d", &did, &exp) != 2){ + printf("Param list error\n"); + return -1; + } + + if(!(did < MAX_DIRS)){ + printf("Dir id too big\n"); + return -1; + } + + if(dir_tab[did].name[0] == 0){ + printf("Dir id empty\n"); + return -1; + } + + + int idx = 0; + ext4_direntry *d; + + while(d = ext4_dir_entry_get(&dir_tab[did].fd, idx++)){ + + memcpy(name, d->name, d->name_length); + name[d->name_length] = 0; + if(verbose){ + printf("\t%s %s\n", entry_to_str(d->inode_type), name); + } + } + + idx--; + + if(idx < 2){ + printf("Minumum dir entry error\n"); + return -1; + } + + if((idx - 2) != exp){ + printf("Expected dir entry error\n"); + return -1; + } + + return EOK; +} + +int _multi_fcreate(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt; + int rc; + int i; + ext4_file fd; + + if(sscanf(p, "%s %s %d", &path, prefix, &cnt) != 3){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_fopen(&fd, path1, "wb+"); + + if(rc != EOK) + break; + } + + return rc; +} + +int _multi_fwrite(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt; + int len, ll; + int rc; + int i, d, wb; + ext4_file fd; + + if(sscanf(p, "%s %s %d %d", &path, prefix, &cnt, &ll) != 4){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_fopen(&fd, path1, "rb+"); + + if(rc != EOK) + break; + + len = ll; + while(len){ + d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len; + rc = ext4_fwrite(&fd, write_buffer, d, &wb); + + if(rc != EOK) + break; + + if(wb != d){ + printf("Write count error\n"); + return -1; + } + + len -= d; + } + } + + return rc; +} + +int _multi_fread(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt; + int len, ll; + int rc; + int i, d, rb; + ext4_file fd; + + if(sscanf(p, "%s %s %d %d", &path, prefix, &cnt, &ll) != 4){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_fopen(&fd, path1, "rb+"); + + if(rc != EOK) + break; + + len = ll; + while(len){ + d = len > MAX_RW_BUFFER ? MAX_RW_BUFFER : len; + + memset(read_buffer, 0, MAX_RW_BUFFER); + rc = ext4_fread(&fd, read_buffer, d, &rb); + + if(rc != EOK) + break; + + if(rb != d){ + printf("Read count error\n"); + return -1; + } + + if(memcmp(read_buffer, write_buffer, d)){ + printf("Read compare error\n"); + return -1; + } + + len -= d; + } + } + + return rc; +} + +int _multi_fremove(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt, i, rc; + + if(sscanf(p, "%s %s %d", &path, prefix, &cnt) != 3){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_fremove(path1); + if(rc != EOK) + break; + } + + return rc; +} + +int _multi_dcreate(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt, i, rc; + + if(sscanf(p, "%s %s %d", &path, prefix, &cnt) != 3){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_dir_mk(path1); + if(rc != EOK) + break; + } + + return rc; +} + +int _multi_dremove(char *p) +{ + char path[256]; + char path1[256]; + char prefix[32]; + int cnt, i, rc; + + if(sscanf(p, "%s %s %d", &path, prefix, &cnt) != 3){ + printf("Param list error\n"); + return -1; + } + + for (i = 0; i < cnt; ++i) { + sprintf(path1, "%s%s%d", path, prefix, i); + rc = ext4_dir_rm(path1); + if(rc != EOK) + break; + } + + return rc; +} + +struct ext4_mount_stats saved_stats; + +int _stats_save(char *p) +{ + char path[256]; + + if(sscanf(p, "%s", &path) != 1){ + printf("Param list error\n"); + return -1; + } + + return ext4_mount_point_stats(path, &saved_stats); +} + +int _stats_check(char *p) +{ + char path[256]; + int rc; + + struct ext4_mount_stats actual_stats; + + if(sscanf(p, "%s", &path) != 1){ + printf("Param list error\n"); + return -1; + } + + rc = ext4_mount_point_stats(path, &actual_stats); + + if(rc != EOK) + return rc; + + if(memcmp(&saved_stats, &actual_stats, sizeof(struct ext4_mount_stats))){ + if(verbose){ + printf("\tMount point stats error:\n"); + printf("\tsaved_stats:\n"); + printf("\tinodes_count = %d\n", saved_stats.inodes_count); + printf("\tfree_inodes_count = %d\n", saved_stats.free_inodes_count); + printf("\tblocks_count = %llu\n", saved_stats.blocks_count); + printf("\tfree_blocks_count = %llu\n", saved_stats.free_blocks_count); + printf("\tblock_size = %d\n", saved_stats.block_size); + printf("\tblock_group_count = %d\n", saved_stats.block_group_count); + printf("\tblocks_per_group = %d\n", saved_stats.blocks_per_group); + printf("\tinodes_per_group = %d\n", saved_stats.inodes_per_group); + printf("\tvolume_name = %s\n", saved_stats.volume_name); + printf("\tactual_stats:\n"); + printf("\tinodes_count = %d\n", actual_stats.inodes_count); + printf("\tfree_inodes_count = %d\n", actual_stats.free_inodes_count); + printf("\tblocks_count = %llu\n", actual_stats.blocks_count); + printf("\tfree_blocks_count = %llu\n", actual_stats.free_blocks_count); + printf("\tblock_size = %d\n", actual_stats.block_size); + printf("\tblock_group_count = %d\n", actual_stats.block_group_count); + printf("\tblocks_per_group = %d\n", actual_stats.blocks_per_group); + printf("\tinodes_per_group = %d\n", actual_stats.inodes_per_group); + printf("\tvolume_name = %s\n", actual_stats.volume_name); + } + return -1; + } + + + return rc; +} + + +static char* entry_to_str(uint8_t type) +{ + switch(type){ + case EXT4_DIRENTRY_UNKNOWN: + return "[UNK] "; + case EXT4_DIRENTRY_REG_FILE: + return "[FIL] "; + case EXT4_DIRENTRY_DIR: + return "[DIR] "; + case EXT4_DIRENTRY_CHRDEV: + return "[CHA] "; + case EXT4_DIRENTRY_BLKDEV: + return "[BLK] "; + case EXT4_DIRENTRY_FIFO: + return "[FIF] "; + case EXT4_DIRENTRY_SOCK: + return "[SOC] "; + case EXT4_DIRENTRY_SYMLINK: + return "[SYM] "; + default: + break; + } + return "[???]"; +} + +static int winsock_init(void) +{ +#if WIN32 + int rc; + static WSADATA wsaData; + rc = WSAStartup(MAKEWORD(2,2), &wsaData); + if (rc != 0) { + return -1; + } +#endif + return 0; +} + +static void winsock_fini(void) +{ +#if WIN32 + WSACleanup(); +#endif +} + diff --git a/lwext4/ext4.c b/lwext4/ext4.c index 257ce7a..e6eb7a8 100644 --- a/lwext4/ext4.c +++ b/lwext4/ext4.c @@ -61,8 +61,11 @@ /**@brief Mount point descrpitor.*/ struct ext4_mountpoint { + /**@brief Mount done flag.*/ + bool mounted; + /**@brief Mount point name (@ref ext4_mount)*/ - const char *name; + char name[32]; /**@brief Os dependent lock/unlock functions.*/ struct ext4_lock *os_locks; @@ -78,7 +81,7 @@ struct ext4_mountpoint { struct _ext4_devices { /**@brief Block device name (@ref ext4_device_register)*/ - const char *name; + char name[32]; /**@brief Block device handle.*/ struct ext4_blockdev *bd; @@ -102,12 +105,15 @@ int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc, ext4_assert(bd && dev_name); for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) { - if(!_bdevices[i].name){ - _bdevices[i].name = dev_name; + if(!_bdevices[i].bd){ + strcpy(_bdevices[i].name, dev_name); _bdevices[i].bd = bd; _bdevices[i].bc = bc; return EOK; } + + if(!strcmp(_bdevices[i].name, dev_name)) + return EOK; } return ENOSPC; } @@ -330,11 +336,16 @@ int ext4_mount(const char * dev_name, char *mount_point) return ENODEV; for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { - if(!_mp[i].name){ - _mp[i].name = mount_point; + if(!_mp[i].mounted){ + strcpy(_mp[i].name, mount_point); + _mp[i].mounted = 1; mp = &_mp[i]; break; } + + if(!strcmp(_mp[i].name, mount_point)){ + return EOK; + } } if(!mp) @@ -393,9 +404,8 @@ int ext4_umount(char *mount_point) struct ext4_mountpoint *mp = 0; for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { - if(_mp[i].name){ - if(!strcmp(_mp[i].name, mount_point)) - mp = &_mp[i]; + if(!strcmp(_mp[i].name, mount_point)){ + mp = &_mp[i]; break; } } @@ -407,7 +417,7 @@ int ext4_umount(char *mount_point) if(r != EOK) return r; - mp->name = 0; + mp->mounted = 0; if(mp->cache_dynamic){ ext4_bcache_fini_dynamic(mp->fs.bdev->bc); @@ -424,11 +434,11 @@ int ext4_mount_point_stats(const char *mount_point, struct ext4_mountpoint *mp = 0; for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { - if(_mp[i].name){ - if(!strcmp(_mp[i].name, mount_point)) - mp = &_mp[i]; + if(!strcmp(_mp[i].name, mount_point)){ + mp = &_mp[i]; break; } + } if(!mp) return ENOENT; @@ -456,10 +466,12 @@ static struct ext4_mountpoint* ext4_get_mount(const char *path) { int i; for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { - if(_mp[i].name){ - if(!strncmp(_mp[i].name, path, strlen(_mp[i].name))) - return &_mp[i]; - } + + if(!_mp[i].mounted) + continue; + + if(!strncmp(_mp[i].name, path, strlen(_mp[i].name))) + return &_mp[i]; } return 0; } diff --git a/lwext4/ext4_ialloc.c b/lwext4/ext4_ialloc.c index 39bba1c..3c10e5e 100644 --- a/lwext4/ext4_ialloc.c +++ b/lwext4/ext4_ialloc.c @@ -186,7 +186,7 @@ int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir) uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb); /* Check if this block group is good candidate for allocation */ - if (free_inodes > 0) { + if (free_inodes >= avg_free_inodes) { /* Load block with bitmap */ uint32_t bitmap_block_addr = ext4_bg_get_inode_bitmap( bg_ref.block_group, sb);