87 lines
1.4 KiB
C
87 lines
1.4 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include "lib/rbtree.h"
|
|
#include <kernel/common.h>
|
|
#include <kernel/slab.h>
|
|
|
|
#include <lib/list.h>
|
|
#include <lib/string.h>
|
|
#include <fs/vfs.h>
|
|
|
|
#include <lib/errno.h>
|
|
|
|
static fs_t *root_fs = NULL;
|
|
|
|
static list_t fs_type_list = {0};
|
|
|
|
int vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
|
|
{
|
|
if (!mount_point) {
|
|
if (root_fs) {
|
|
return -EBUSY;
|
|
} else {
|
|
root_fs = fs_to_mount;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int iter_inode_dirent(inode_t *ino, const char *name, inode_t **out)
|
|
{
|
|
fs_t *fs = ino->fs;
|
|
dirent_t *d = inode_find_dirent(ino, name);
|
|
if (d) {
|
|
if (d->inode)
|
|
*out = d->inode;
|
|
else
|
|
return fs->fs_ops.open_inode(fs, d->ino_num, out);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int vfs_get_inode(const char *path, inode_t *ret)
|
|
{
|
|
inode_t *ino = root_fs->root_inode;
|
|
|
|
char name[512] = {0};
|
|
|
|
char c;
|
|
int i = 0;
|
|
while (i < 256 && ino) {
|
|
c = *path++;
|
|
if (c == '/' || c == '\0') {
|
|
name[i] = 0;
|
|
i = 0;
|
|
|
|
int res = iter_inode_dirent(ino, name, &ino);
|
|
if (res)
|
|
return res;
|
|
|
|
if (c == '\0')
|
|
break;
|
|
} else {
|
|
name[i] = c;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return -ENOENT;
|
|
}
|
|
|
|
void vfs_register(struct fs_type *fs)
|
|
{
|
|
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
|
memcpy(ins, fs, sizeof(*fs));
|
|
list_insert(&fs_type_list, ins);
|
|
}
|
|
|
|
void vfs_init()
|
|
{
|
|
void inode_init();
|
|
inode_init();
|
|
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
|
|
memset(root_fs, 0, sizeof(*root_fs));
|
|
|
|
list_create(&fs_type_list);
|
|
}
|