From 7c8b46eedab638ccdc572f71c8a073250aedf2a0 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 15:13:05 -0400 Subject: [PATCH] vfs: add mount support Signed-off-by: Michael Shaw --- fs/vfs/mount.c | 283 +++++++++++++++++++++++++++++++++++++++++++++++ fs/vfs/vfs.c | 35 +++--- include/fs/vfs.h | 20 +++- 3 files changed, 318 insertions(+), 20 deletions(-) create mode 100644 fs/vfs/mount.c diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c new file mode 100644 index 0000000..d39f6d2 --- /dev/null +++ b/fs/vfs/mount.c @@ -0,0 +1,283 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +#include + +#include +#include + +static struct mount *root_mount = NULL; +static spinlock_t mount_lock = 0; + +fs_t *vfs_get_root(void) +{ + if (!root_mount) + return NULL; + return root_mount->fs; +} + +struct mount *vfs_get_root_mount(void) +{ + return root_mount; +} + +struct mount *mount_find_child(struct mount *parent, dirent_t *dent) +{ + spinlock_acquire(&mount_lock); + + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == dent) { + spinlock_release(&mount_lock); + return m; + } + item = item->next; + } + + spinlock_release(&mount_lock); + return NULL; +} + +static int load_inode(dirent_t *dent) +{ + if (dent->inode) + return 0; + + int res = dent->fs->fs_ops.open_inode(dent->fs, dent->ino_num, dent, &dent->inode); + return res; +} + +static int mount_traverse(const char *path, struct mount **out_mount, dirent_t **out_dirent) +{ + if (!root_mount) + return -ENOSYS; + + while (*path == '/') + path++; + + if (*path == '\0') { + *out_mount = root_mount; + *out_dirent = root_mount->fs->root_dirent; + if (*out_dirent) + dirent_inc_ref(*out_dirent); + return 0; + } + + struct mount *cur = root_mount; + dirent_t *cur_dent = root_mount->fs->root_dirent; + + if (!cur_dent) + return -ENOENT; + + dirent_inc_ref(cur_dent); + + const char *p = path; + + while (*p != '\0') { + const char *comp_start = p; + while (*p != '/' && *p != '\0') + p++; + + size_t comp_len = p - comp_start; + if (comp_len > FILE_NAME_MAX_LEN) { + dirent_dec_ref(cur_dent); + return -ENAMETOOLONG; + } + + char name[FILE_NAME_MAX_LEN + 1]; + memcpy(name, comp_start, comp_len); + name[comp_len] = '\0'; + + while (*p == '/') + p++; + + bool is_last = (*p == '\0'); + + dirent_t *found = NULL; + int res = inode_find_dirent(cur_dent->inode, name, &found); + if (res < 0) { + dirent_dec_ref(cur_dent); + return res; + } + + res = load_inode(found); + if (res < 0) { + dirent_dec_ref(found); + dirent_dec_ref(cur_dent); + return res; + } + + if (is_last) { + dirent_dec_ref(cur_dent); + *out_mount = cur; + *out_dirent = found; + return 0; + } + + struct mount *child = NULL; + list_item_t *item = cur->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == found) { + child = m; + break; + } + item = item->next; + } + + dirent_dec_ref(cur_dent); + + if (child) { + dirent_dec_ref(found); + cur = child; + cur_dent = cur->fs->root_dirent; + if (!cur_dent) + return -ENOENT; + dirent_inc_ref(cur_dent); + } else { + cur_dent = found; + } + } + + dirent_dec_ref(cur_dent); + *out_mount = cur; + *out_dirent = cur->fs->root_dirent; + if (*out_dirent) + dirent_inc_ref(*out_dirent); + return 0; +} + +int vfs_mount(const char *path, fs_t *fs, u64 flags) +{ + if (!fs) + return -EINVAL; + + spinlock_acquire(&mount_lock); + + if (!path) { + if (root_mount) { + spinlock_release(&mount_lock); + return -EBUSY; + } + + struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); + if (!m) { + spinlock_release(&mount_lock); + return -ENOMEM; + } + + m->parent = NULL; + m->fs = fs; + m->mountpoint = NULL; + m->flags = flags; + m->child_link = NULL; + list_create(&m->children); + + root_mount = m; + spinlock_release(&mount_lock); + return 0; + } + + struct mount *parent = NULL; + dirent_t *mp = NULL; + int res = mount_traverse(path, &parent, &mp); + if (res < 0) { + spinlock_release(&mount_lock); + return res; + } + + if ((mp->inode->mode & S_IFMT) != S_IFDIR) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -ENOTDIR; + } + + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == mp) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -EBUSY; + } + item = item->next; + } + + struct mount *m = kzalloc(sizeof(*m), ALLOC_KERN); + if (!m) { + dirent_dec_ref(mp); + spinlock_release(&mount_lock); + return -ENOMEM; + } + + m->parent = parent; + m->fs = fs; + m->mountpoint = mp; + m->flags = flags; + + list_create(&m->children); + + m->child_link = list_insert(&parent->children, m); + if (!m->child_link) { + dirent_dec_ref(mp); + kfree(m); + spinlock_release(&mount_lock); + return -ENOMEM; + } + + spinlock_release(&mount_lock); + return 0; +} + +int vfs_umount(const char *path) +{ + if (!path) + return -EINVAL; + + spinlock_acquire(&mount_lock); + + struct mount *parent = NULL; + dirent_t *mp = NULL; + int res = mount_traverse(path, &parent, &mp); + if (res < 0) { + spinlock_release(&mount_lock); + return res; + } + + struct mount *target = NULL; + list_item_t *item = parent->children.head; + while (item) { + struct mount *m = item->ptr; + if (m->mountpoint == mp) { + target = m; + break; + } + item = item->next; + } + + dirent_dec_ref(mp); + + if (!target) { + spinlock_release(&mount_lock); + return -EINVAL; + } + + if (target == root_mount) { + spinlock_release(&mount_lock); + return -EINVAL; + } + + if (target->children.head) { + spinlock_release(&mount_lock); + return -EBUSY; + } + + list_remove(target->child_link); + dirent_dec_ref(target->mountpoint); + kfree(target); + + spinlock_release(&mount_lock); + return 0; +} diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index a673cc5..8a2faad 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -6,7 +6,6 @@ #include #include -static fs_t *root_fs = NULL; static list_t fs_type_list = {0}; static char *path_trim(const char *name) @@ -85,7 +84,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) return -ENOENT; } -int vfs_openat(dirent_t *in, const char *name, dirent_t **out) +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out) { if (!in || !name || !out) return -EINVAL; @@ -94,6 +93,7 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out) if (!path_optr) return -ENOMEM; + struct mount *cur_mount = mnt; dirent_t *current_dir = in; dirent_inc_ref(current_dir); @@ -118,6 +118,19 @@ int vfs_openat(dirent_t *in, const char *name, dirent_t **out) dirent_dec_ref(current_dir); current_dir = next_dir; + + struct mount *child = mount_find_child(cur_mount, current_dir); + if (child) { + dirent_dec_ref(current_dir); + cur_mount = child; + current_dir = child->fs->root_dirent; + if (!current_dir) { + ret = -ENOENT; + goto out; + } + dirent_inc_ref(current_dir); + } + cur_path += res; while (*cur_path == '/') @@ -140,10 +153,11 @@ int vfs_open(const char *abs_path, dirent_t **out) if (!abs_path || !out) return -EINVAL; - if (!root_fs->root_dirent) + struct mount *root = vfs_get_root_mount(); + if (!root || !root->fs || !root->fs->root_dirent) return -ENOENT; - return vfs_openat(root_fs->root_dirent, abs_path, out); + return vfs_openat(root, root->fs->root_dirent, abs_path, out); } void vfs_close(dirent_t *dent) @@ -284,16 +298,6 @@ int vfs_unlink(dirent_t *parent, const char *name) return res; } -int vfs_mount(const char *mount_point, fs_t *fs_to_mount) -{ - if (!mount_point || !strcmp(mount_point, "/")) { - root_fs = fs_to_mount; - return 0; - } - - return -ENOSYS; -} - void vfs_register(struct fs_driver *fs) { struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN); @@ -308,8 +312,5 @@ void vfs_register(struct fs_driver *fs) void vfs_init(void) { inode_init(); - root_fs = kalloc(sizeof(fs_t), ALLOC_KERN); - memset(root_fs, 0, sizeof(*root_fs)); - list_create(&fs_type_list); } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index d6add85..e8a2062 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -3,6 +3,7 @@ #define _VFS_H_ #include +#include #include #include @@ -81,19 +82,32 @@ struct _fs { struct dir_context { void *cookie; - bool init; + bool init; +}; + +struct mount { + struct mount *parent; + list_t children; + list_item_t *child_link; + fs_t *fs; + dirent_t *mountpoint; + u64 flags; }; void vfs_init(void); void vfs_register(struct fs_driver *fs); int vfs_open(const char *abs_path, dirent_t **out); -int vfs_openat(dirent_t *in, const char *name, dirent_t **out); +int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out); void vfs_close(dirent_t *dent); int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out); int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out); int vfs_unlink(dirent_t *parent, const char *name); -int vfs_mount(const char *mount_point, fs_t *fs_to_mount); +int vfs_mount(const char *path, fs_t *fs, u64 flags); +int vfs_umount(const char *path); +fs_t *vfs_get_root(void); +struct mount *vfs_get_root_mount(void); +struct mount *mount_find_child(struct mount *parent, dirent_t *dent); ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset); ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset);