From 7cda17edfcc20680e0b184207193a10521a01e66 Mon Sep 17 00:00:00 2001 From: Cody Wong Date: Fri, 19 Apr 2024 19:33:13 +0800 Subject: [PATCH] [fs][v9fs] Add an example test for VirtIO 9p filesystem Add a simple test to validate the filesystem APIs that connect the LK filesystem layer and the virtualIO 9p devices. The test does the same as `app/tests/v9p_tests.c` to mount the littlekernel codebase folder as the `/v9p` on the LK filesystem. Then it tries to read the `LICENSE` file under the codebase and show the first 1024 bytes of the file. For example: ``` starting internet servers starting app shell entering main console loop ] v9fs_tests 0x80017060: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 |/*. * Copyright 0x80017070: 28 63 29 20 32 30 30 38 2d 32 30 31 35 20 54 72 |(c) 2008-2015 Tr 0x80017080: 61 76 69 73 20 47 65 69 73 65 6c 62 72 65 63 68 |avis Geiselbrech 0x80017090: 74 0a 20 2a 0a 20 2a 20 50 65 72 6d 69 73 73 69 |t. *. * Permissi 0x800170a0: 6f 6e 20 69 73 20 68 65 72 65 62 79 20 67 72 61 |on is hereby gra ... ``` Signed-off-by: Cody Wong --- app/tests/include/app/tests.h | 1 + app/tests/rules.mk | 1 + app/tests/tests.c | 1 + app/tests/v9fs_tests.c | 72 +++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 app/tests/v9fs_tests.c diff --git a/app/tests/include/app/tests.h b/app/tests/include/app/tests.h index ecfe3b1b..7e3890ee 100644 --- a/app/tests/include/app/tests.h +++ b/app/tests/include/app/tests.h @@ -20,6 +20,7 @@ int clock_tests(int argc, const console_cmd_args *argv); int printf_tests(int argc, const console_cmd_args *argv); int printf_tests_float(int argc, const console_cmd_args *argv); int v9p_tests(int argc, const console_cmd_args *argv); +int v9fs_tests(int argc, const console_cmd_args *argv); #endif diff --git a/app/tests/rules.mk b/app/tests/rules.mk index cb8ffd97..23008a05 100644 --- a/app/tests/rules.mk +++ b/app/tests/rules.mk @@ -12,6 +12,7 @@ MODULE_SRCS := \ $(LOCAL_DIR)/thread_tests.c \ $(LOCAL_DIR)/port_tests.c \ $(LOCAL_DIR)/v9p_tests.c \ + $(LOCAL_DIR)/v9fs_tests.c \ MODULE_FLOAT_SRCS := \ $(LOCAL_DIR)/benchmarks.c \ diff --git a/app/tests/tests.c b/app/tests/tests.c index 900330bd..401be1ca 100644 --- a/app/tests/tests.c +++ b/app/tests/tests.c @@ -23,4 +23,5 @@ STATIC_COMMAND("fibo", "threaded fibonacci", &fibo) STATIC_COMMAND("spinner", "create a spinning thread", &spinner) STATIC_COMMAND("cbuf_tests", "test lib/cbuf", &cbuf_tests) STATIC_COMMAND("v9p_tests", "test dev/virtio/9p", &v9p_tests) +STATIC_COMMAND("v9fs_tests", "test lib/fs/9p", &v9fs_tests) STATIC_COMMAND_END(tests); diff --git a/app/tests/v9fs_tests.c b/app/tests/v9fs_tests.c new file mode 100644 index 00000000..c6d9ac9b --- /dev/null +++ b/app/tests/v9fs_tests.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Cody Wong + * + * Use of this source code is governed by a MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT + */ +#include +#include +#include + +#define _LOGF(fmt, args...) \ + printf("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__, ##args) +#define LOGF(x...) _LOGF(x) + +#if WITH_DEV_VIRTIO_9P +#include + +#define V9FS_MOUNT_POINT "/v9p" +#define V9FS_NAME "9p" +#define V9P_BDEV_NAME "v9p0" + +#define BUF_SIZE 1024 + +int v9fs_tests(int argc, const console_cmd_args *argv) { + status_t status; + ssize_t readbytes; + filehandle *handle; + char buf[BUF_SIZE]; + + status = fs_mount(V9FS_MOUNT_POINT, V9FS_NAME, V9P_BDEV_NAME); + if (status != NO_ERROR) { + LOGF("failed to mount v9p bdev (%s) onto mount point (%s): %d\n", + V9P_BDEV_NAME, V9FS_MOUNT_POINT, status); + return status; + } + + status = fs_open_file(V9FS_MOUNT_POINT "/LICENSE", &handle); + if (status != NO_ERROR) { + LOGF("failed to open the target file: %d\n", status); + return status; + } + + readbytes = fs_read_file(handle, buf, 0, BUF_SIZE); + if (readbytes < 0) { + LOGF("failed to read the target file: %ld\n", readbytes); + return status; + } + + hexdump8(buf, BUF_SIZE); + + status = fs_close_file(handle); + if (status != NO_ERROR) { + LOGF("failed to close the target file: %d\n", status); + return status; + } + + status = fs_unmount(V9FS_MOUNT_POINT); + if (status != NO_ERROR) { + LOGF("failed to unmount v9p on mount point (%s): %d\n", + V9FS_MOUNT_POINT, status); + return status; + } + + return NO_ERROR; +} +#else +int v9fs_tests(int argc, const console_cmd_args *argv) { + LOGF("platform didn't have dev/virtio/9p supported\n"); + return ERR_NOT_SUPPORTED; +} +#endif // WITH_DEV_VIRTIO_9P