diff --git a/cli/main.c b/cli/main.c index 9581964b..49988e0d 100644 --- a/cli/main.c +++ b/cli/main.c @@ -58,6 +58,8 @@ enum longopt { #endif #if defined(TOYWASM_ENABLE_WASI_LITTLEFS) opt_wasi_littlefs_dir, + opt_wasi_littlefs_block_size, + opt_wasi_littlefs_disk_version, #endif }; @@ -223,6 +225,18 @@ static const struct option longopts[] = { NULL, opt_wasi_littlefs_dir, }, + { + "wasi-littlefs-block-size", + required_argument, + NULL, + opt_wasi_littlefs_block_size, + }, + { + "wasi-littlefs-disk-version", + required_argument, + NULL, + opt_wasi_littlefs_disk_version, + }, #endif { NULL, @@ -245,6 +259,8 @@ static const char *opt_metavars[] = { #endif #if defined(TOYWASM_ENABLE_WASI_LITTLEFS) [opt_wasi_littlefs_dir] = "LITTLEFS_IMAGE_PATH::LFS_DIR[::GUEST_DIR]", + [opt_wasi_littlefs_block_size] = "BLOCK_SIZE", + [opt_wasi_littlefs_disk_version] = "DISK_VERSION", #endif [opt_timeout] = "TIMEOUT_MS", #if defined(TOYWASM_ENABLE_TRACING) @@ -532,6 +548,22 @@ main(int argc, char *const *argv) goto fail; } break; + case opt_wasi_littlefs_block_size: + ret = str_to_u32( + optarg, 0, + &opts->wasi_littlefs_mount_cfg.block_size); + if (ret != 0) { + goto fail; + } + break; + case opt_wasi_littlefs_disk_version: + ret = str_to_u32( + optarg, 0, + &opts->wasi_littlefs_mount_cfg.disk_version); + if (ret != 0) { + goto fail; + } + break; #endif default: print_usage(); diff --git a/cli/repl.c b/cli/repl.c index 648116a7..6147c70b 100644 --- a/cli/repl.c +++ b/cli/repl.c @@ -381,7 +381,8 @@ toywasm_repl_set_wasi_prestat_littlefs(struct repl_state *state, return ret; } struct wasi_vfs *vfs; - ret = wasi_instance_prestat_add_littlefs(state->wasi, path, &vfs); + ret = wasi_instance_prestat_add_littlefs( + state->wasi, path, &state->opts.wasi_littlefs_mount_cfg, &vfs); if (ret != 0) { return ret; } @@ -1491,6 +1492,10 @@ repl_options_init(struct repl_options *opts) #if defined(TOYWASM_ENABLE_DYLD) dyld_options_set_defaults(&opts->dyld_options); #endif +#if defined(TOYWASM_ENABLE_WASI_LITTLEFS) + memset(&opts->wasi_littlefs_mount_cfg, 0, + sizeof(opts->wasi_littlefs_mount_cfg)); +#endif } void diff --git a/cli/repl.h b/cli/repl.h index 6c228719..e5adeca2 100644 --- a/cli/repl.h +++ b/cli/repl.h @@ -4,6 +4,9 @@ #if defined(TOYWASM_ENABLE_DYLD) #include "dyld.h" #endif +#if defined(TOYWASM_ENABLE_WASI_LITTLEFS) +#include "wasi_littlefs.h" +#endif #include "options.h" #include "type.h" @@ -18,6 +21,9 @@ struct repl_options { #endif struct load_options load_options; struct exec_options exec_options; +#if defined(TOYWASM_ENABLE_WASI_LITTLEFS) + struct wasi_littlefs_mount_cfg wasi_littlefs_mount_cfg; +#endif }; struct repl_module_state { diff --git a/libwasi_littlefs/CMakeLists.txt b/libwasi_littlefs/CMakeLists.txt index bd4e7920..ae090248 100644 --- a/libwasi_littlefs/CMakeLists.txt +++ b/libwasi_littlefs/CMakeLists.txt @@ -54,7 +54,9 @@ target_include_directories(toywasm-lib-wasi-littlefs $ $ PRIVATE ${TOYWASM_LITTLEFS_SOURCE_DIR}) -target_compile_definitions(toywasm-lib-wasi-littlefs PRIVATE ${TOYWASM_LITTLEFS_PREFIX_UPPER}_THREADSAFE) +target_compile_definitions(toywasm-lib-wasi-littlefs PRIVATE + ${TOYWASM_LITTLEFS_PREFIX_UPPER}_THREADSAFE + ${TOYWASM_LITTLEFS_PREFIX_UPPER}_MULTIVERSION) if (NOT CMAKE_BUILD_TYPE MATCHES Debug) target_compile_definitions(toywasm-lib-wasi-littlefs PRIVATE ${TOYWASM_LITTLEFS_PREFIX_UPPER}_NO_DEBUG ${TOYWASM_LITTLEFS_PREFIX_UPPER}_NO_ASSERT) endif() diff --git a/libwasi_littlefs/wasi_littlefs.c b/libwasi_littlefs/wasi_littlefs.c index a4a0abd1..40a4e9b8 100644 --- a/libwasi_littlefs/wasi_littlefs.c +++ b/libwasi_littlefs/wasi_littlefs.c @@ -21,7 +21,9 @@ int wasi_instance_prestat_add_littlefs(struct wasi_instance *wasi, - const char *path, struct wasi_vfs **vfsp) + const char *path, + const struct wasi_littlefs_mount_cfg *cfg, + struct wasi_vfs **vfsp) { struct wasi_vfs *vfs = NULL; char *image_path = NULL; @@ -40,7 +42,7 @@ wasi_instance_prestat_add_littlefs(struct wasi_instance *wasi, goto fail; } mapdir_string = coloncolon + 2; - ret = wasi_littlefs_mount_file(image_path, &vfs); + ret = wasi_littlefs_mount_file(image_path, cfg, &vfs); free(image_path); if (ret != 0) { goto fail; diff --git a/libwasi_littlefs/wasi_littlefs.h b/libwasi_littlefs/wasi_littlefs.h index 61aac299..2a48cf4a 100644 --- a/libwasi_littlefs/wasi_littlefs.h +++ b/libwasi_littlefs/wasi_littlefs.h @@ -1,3 +1,6 @@ +#if !defined(_TOYWASM_LIBWASI_LITTLEFS_WASI_LITTLEFS_H) +#define _TOYWASM_LIBWASI_LITTLEFS_WASI_LITTLEFS_H + #include "platform.h" __BEGIN_EXTERN_C @@ -5,8 +8,15 @@ __BEGIN_EXTERN_C struct wasi_instance; struct wasi_vfs; -int wasi_instance_prestat_add_littlefs(struct wasi_instance *wasi, - const char *path, - struct wasi_vfs **vfsp); +struct wasi_littlefs_mount_cfg { + uint32_t disk_version; + uint32_t block_size; +}; + +int wasi_instance_prestat_add_littlefs( + struct wasi_instance *wasi, const char *path, + const struct wasi_littlefs_mount_cfg *cfg, struct wasi_vfs **vfsp); __END_EXTERN_C + +#endif /* !defined(_TOYWASM_LIBWASI_LITTLEFS_WASI_LITTLEFS_H) */ diff --git a/libwasi_littlefs/wasi_littlefs_mount.c b/libwasi_littlefs/wasi_littlefs_mount.c index a35dcf55..64faee6c 100644 --- a/libwasi_littlefs/wasi_littlefs_mount.c +++ b/libwasi_littlefs/wasi_littlefs_mount.c @@ -14,6 +14,7 @@ #include "nbio.h" #include "util.h" +#include "wasi_littlefs.h" #include "wasi_littlefs_impl.h" #include "wasi_littlefs_mount.h" #include "wasi_vfs_impl_littlefs.h" @@ -97,7 +98,9 @@ wasi_lfs_fs_unlock(const struct lfs_config *cfg) RELEASES(MUTEX(cfg)) } int -wasi_littlefs_mount_file(const char *path, struct wasi_vfs **vfsp) +wasi_littlefs_mount_file(const char *path, + const struct wasi_littlefs_mount_cfg *cfg, + struct wasi_vfs **vfsp) { struct wasi_vfs_lfs *vfs_lfs = NULL; int ret; @@ -123,7 +126,10 @@ wasi_littlefs_mount_file(const char *path, struct wasi_vfs **vfsp) * size by scanning the filesystem image. * for now, we simply hardcode a value. */ - lfs_size_t block_size = 4096; + lfs_size_t block_size = cfg->block_size; + if (block_size == 0) { + block_size = 4096; + } /* * Note: read/prog sizes themselves do not affect the filesystem @@ -169,6 +175,7 @@ wasi_littlefs_mount_file(const char *path, struct wasi_vfs **vfsp) lfs_config->prog_size = prog_size; lfs_config->block_size = block_size; lfs_config->block_count = block_count; + lfs_config->disk_version = cfg->disk_version; /* * disable block-level wear-leveling because there is little point diff --git a/libwasi_littlefs/wasi_littlefs_mount.h b/libwasi_littlefs/wasi_littlefs_mount.h index c2ee57b9..9f17c458 100644 --- a/libwasi_littlefs/wasi_littlefs_mount.h +++ b/libwasi_littlefs/wasi_littlefs_mount.h @@ -1,3 +1,8 @@ +#include + struct wasi_vfs; +struct wasi_littlefs_mount_cfg; -int wasi_littlefs_mount_file(const char *path, struct wasi_vfs **vfsp); +int wasi_littlefs_mount_file(const char *path, + const struct wasi_littlefs_mount_cfg *cfg, + struct wasi_vfs **vfsp);