Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a few options to specify littlefs configurations #272

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Options:
--wasi-dir HOST_DIR[::GUEST_DIR]
--wasi-env NAME=VAR
--wasi-littlefs-dir LITTLEFS_IMAGE_PATH::LFS_DIR[::GUEST_DIR]
--wasi-littlefs-block-size BLOCK_SIZE
--wasi-littlefs-disk-version DISK_VERSION
Examples:
Run a wasi module
toywasm --wasi module
Expand Down
32 changes: 32 additions & 0 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion cli/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions cli/repl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion libwasi_littlefs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ target_include_directories(toywasm-lib-wasi-littlefs
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
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()
Expand Down
6 changes: 4 additions & 2 deletions libwasi_littlefs/wasi_littlefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions libwasi_littlefs/wasi_littlefs.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#if !defined(_TOYWASM_LIBWASI_LITTLEFS_WASI_LITTLEFS_H)
#define _TOYWASM_LIBWASI_LITTLEFS_WASI_LITTLEFS_H

#include "platform.h"

__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) */
11 changes: 9 additions & 2 deletions libwasi_littlefs/wasi_littlefs_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion libwasi_littlefs/wasi_littlefs_mount.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <stdint.h>

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);
Loading