aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCody Wong <codycswong@google.com>2024-04-19 18:42:22 +0800
committerTravis Geiselbrecht <travisg@gmail.com>2024-04-25 00:17:02 -0700
commitb29df9194be08a8d08e77250fdf6b9fcb0a4ead6 (patch)
tree18811334068718c8aedb9a0603ae52787d95b587
parent947cf27830d2ba683ed397becfbfcdff6cfcd32b (diff)
downloadlk-b29df9194be08a8d08e77250fdf6b9fcb0a4ead6.tar.gz
[fs][v9fs] Add VirtIO 9p filesystem structure
Add the fundamental filesystem structure to attach a VirtualIO 9p device. With the implementation of VirtIO 9p devices (lk/dev/virtio/9p), we can use those APIs to connect to a shared folder as a LK filesystem. Signed-off-by: Cody Wong <codycswong@google.com>
-rw-r--r--lib/fs/9p/include/lib/fs/9p.h0
-rw-r--r--lib/fs/9p/rules.mk13
-rw-r--r--lib/fs/9p/v9fs.c128
-rw-r--r--lib/fs/9p/v9fs_priv.h42
-rw-r--r--platform/qemu-virt-arm/rules.mk1
5 files changed, 184 insertions, 0 deletions
diff --git a/lib/fs/9p/include/lib/fs/9p.h b/lib/fs/9p/include/lib/fs/9p.h
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/lib/fs/9p/include/lib/fs/9p.h
diff --git a/lib/fs/9p/rules.mk b/lib/fs/9p/rules.mk
new file mode 100644
index 00000000..94a3891d
--- /dev/null
+++ b/lib/fs/9p/rules.mk
@@ -0,0 +1,13 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_DEPS += \
+ lib/bio \
+ lib/fs \
+ dev/virtio/9p
+
+MODULE_SRCS += \
+ $(LOCAL_DIR)/v9fs.c
+
+include make/module.mk
diff --git a/lib/fs/9p/v9fs.c b/lib/fs/9p/v9fs.c
new file mode 100644
index 00000000..1d67258f
--- /dev/null
+++ b/lib/fs/9p/v9fs.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2024, Google Inc. All rights reserved.
+ * Author: codycswong@google.com (Cody Wong)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <dev/virtio/9p.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <lk/trace.h>
+#include <lk/init.h>
+#include <lk/list.h>
+#include <lk/err.h>
+#include <lib/fs.h>
+#include <kernel/mutex.h>
+
+#include "v9fs_priv.h"
+
+#define LOCAL_TRACE 0
+
+status_t v9fs_mount(bdev_t *dev, fscookie **cookie)
+{
+ status_t ret;
+
+ LTRACEF("bdev (%p) cookie (%p)\n", dev, cookie);
+
+ v9fs_t *v9fs = calloc(1, sizeof(v9fs_t));
+
+ if (!v9fs)
+ return ERR_NO_MEMORY;
+
+ // initialize v9fs structure
+ v9fs->dev = virtio_9p_bdev_to_virtio_device(dev);
+ v9fs->bdev = dev;
+ v9fs->unused_fid = 0;
+ list_initialize(&v9fs->files);
+ list_initialize(&v9fs->dirs);
+ mutex_init(&v9fs->lock);
+ v9fs->root.fid = get_unused_fid(v9fs);
+
+ LTRACEF("v9fs->root.fid: %u\n", v9fs->root.fid);
+ // attach to the host
+ virtio_9p_msg_t tatt = {
+ .msg_type = P9_TATTACH,
+ .tag = P9_TAG_DEFAULT,
+ .msg.tattach = {
+ .fid = v9fs->root.fid,
+ .afid = P9_FID_NOFID,
+ .uname = "root",
+ .aname = V9P_MOUNT_ANAME,
+ .n_uname = P9_UNAME_NONUNAME,
+ }
+ };
+ virtio_9p_msg_t ratt = {};
+
+ if ((ret = virtio_9p_rpc(v9fs->dev, &tatt, &ratt)) != NO_ERROR)
+ goto err;
+
+ v9fs->root.qid = ratt.msg.rattach.qid;
+
+ virtio_9p_msg_destroy(&ratt);
+
+ *cookie = (fscookie *)v9fs;
+
+ return NO_ERROR;
+
+err:
+ LTRACEF("mount 9p dev (%s) failed: %d\n", dev->name, ret);
+
+ free(v9fs);
+ v9fs = NULL;
+ return ret;
+}
+
+status_t v9fs_unmount(fscookie *cookie)
+{
+ v9fs_t *v9fs = (v9fs_t *)cookie;
+
+ LTRACEF("v9fs (%p)\n", v9fs);
+
+ if (v9fs)
+ free(v9fs);
+
+ return 0;
+}
+
+static const struct fs_api v9fs_api = {
+ .format = NULL,
+ .fs_stat = NULL,
+
+ .mount = v9fs_mount,
+ .unmount = v9fs_unmount,
+ .open = NULL,
+ .create = NULL,
+ .remove = NULL,
+ .truncate = NULL,
+ .stat = NULL,
+ .read = NULL,
+ .write = NULL,
+ .close = NULL,
+
+ .mkdir = NULL,
+ .opendir = NULL,
+ .readdir = NULL,
+ .closedir = NULL,
+
+ .file_ioctl = NULL,
+};
+
+STATIC_FS_IMPL(9p, &v9fs_api);
diff --git a/lib/fs/9p/v9fs_priv.h b/lib/fs/9p/v9fs_priv.h
new file mode 100644
index 00000000..9120c605
--- /dev/null
+++ b/lib/fs/9p/v9fs_priv.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2024, Google Inc. All rights reserved.
+ * Author: codycswong@google.com (Cody Wong)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <dev/virtio/9p.h>
+#include <lib/fs.h>
+#include <lk/list.h>
+#include <kernel/mutex.h>
+
+typedef struct v9fs {
+ struct virtio_device *dev;
+ bdev_t *bdev;
+
+ uint32_t unused_fid;
+ v9fs_fid_t root;
+ mutex_t lock;
+
+ struct list_node files;
+ struct list_node dirs;
+} v9fs_t;
+
+status_t v9fs_mount(bdev_t *dev, fscookie **cookie);
+status_t v9fs_unmount(fscookie *cookie);
diff --git a/platform/qemu-virt-arm/rules.mk b/platform/qemu-virt-arm/rules.mk
index eb00bda1..34019338 100644
--- a/platform/qemu-virt-arm/rules.mk
+++ b/platform/qemu-virt-arm/rules.mk
@@ -36,6 +36,7 @@ MODULE_DEPS += \
dev/virtio/9p \
lib/cbuf \
lib/fdtwalk \
+ lib/fs/9p \
GLOBAL_DEFINES += \
MEMBASE=$(MEMBASE) \