321 lines
5.8 KiB
C
321 lines
5.8 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/common.h>
|
|
#include <kernel/slab.h>
|
|
#include <fs/vfs.h>
|
|
#include <lib/list.h>
|
|
#include <lib/string.h>
|
|
#include <lib/errno.h>
|
|
|
|
static list_t fs_type_list = {0};
|
|
|
|
static char *path_trim(const char *name)
|
|
{
|
|
while (*name == '/')
|
|
name++;
|
|
|
|
size_t len = strlen(name);
|
|
|
|
if (len == 0)
|
|
return kzalloc(1, ALLOC_KERN);
|
|
|
|
const char *end = name + len;
|
|
while (end > name && *(end - 1) == '/')
|
|
end--;
|
|
|
|
char *ret = kzalloc((end - name) + 1, ALLOC_KERN);
|
|
if (!ret)
|
|
return NULL;
|
|
|
|
char last = 0;
|
|
size_t j = 0;
|
|
for (const char *c = name; c < end; c++) {
|
|
if (last == '/' && *c == '/')
|
|
continue;
|
|
ret[j++] = *c;
|
|
last = *c;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|
{
|
|
if (!dent || !out_dirent)
|
|
return -EINVAL;
|
|
|
|
int res;
|
|
char *build = kzalloc(FILE_NAME_MAX_LEN + 1, ALLOC_KERN);
|
|
if (!build)
|
|
return -ENOMEM;
|
|
|
|
for (int i = 0; i < FILE_NAME_MAX_LEN; i++) {
|
|
char c = path[i];
|
|
|
|
if (c == '/' || c == '\0') {
|
|
dirent_t *dirent;
|
|
res = dirent_lookup(dent, build, &dirent);
|
|
kfree(build);
|
|
if (res < 0)
|
|
return -ENOENT;
|
|
|
|
spinlock_acquire(&dirent->lock);
|
|
|
|
if (!dirent->inode) {
|
|
fs_t *fs = dent->fs;
|
|
u64 inum = dirent->ino_num;
|
|
res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode);
|
|
if (res < 0) {
|
|
spinlock_release(&dirent->lock);
|
|
dirent_dec_ref(dirent);
|
|
return res;
|
|
}
|
|
}
|
|
|
|
spinlock_release(&dirent->lock);
|
|
|
|
*out_dirent = dirent;
|
|
return i;
|
|
} else {
|
|
build[i] = c;
|
|
}
|
|
}
|
|
|
|
kfree(build);
|
|
return -ENOENT;
|
|
}
|
|
|
|
int vfs_openat(struct mount *mnt, dirent_t *in, const char *name, dirent_t **out)
|
|
{
|
|
if (!in || !name || !out)
|
|
return -EINVAL;
|
|
|
|
char *path_optr = path_trim(name);
|
|
if (!path_optr)
|
|
return -ENOMEM;
|
|
|
|
struct mount *cur_mount = mnt;
|
|
dirent_t *current_dir = in;
|
|
dirent_inc_ref(current_dir);
|
|
|
|
if (*path_optr == '\0') {
|
|
*out = current_dir;
|
|
kfree(path_optr);
|
|
return 0;
|
|
}
|
|
|
|
char *cur_path = path_optr;
|
|
int ret = -ENOENT;
|
|
|
|
while (1) {
|
|
dirent_t *next_dir;
|
|
int res = next_level(cur_path, current_dir, &next_dir);
|
|
|
|
if (res < 0) {
|
|
dirent_dec_ref(current_dir);
|
|
ret = res;
|
|
goto 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 == '/')
|
|
cur_path++;
|
|
|
|
if (*cur_path == '\0') {
|
|
*out = current_dir;
|
|
ret = 0;
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
out:
|
|
kfree(path_optr);
|
|
return ret;
|
|
}
|
|
|
|
int vfs_open(const char *abs_path, dirent_t **out)
|
|
{
|
|
if (!abs_path || !out)
|
|
return -EINVAL;
|
|
|
|
struct mount *root = vfs_get_root_mount();
|
|
if (!root || !root->fs || !root->fs->root_dirent)
|
|
return -ENOENT;
|
|
|
|
return vfs_openat(root, root->fs->root_dirent, abs_path, out);
|
|
}
|
|
|
|
void vfs_close(dirent_t *dent)
|
|
{
|
|
dirent_dec_ref(dent);
|
|
}
|
|
|
|
int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out)
|
|
{
|
|
if (!dir || !ctx || !out)
|
|
return -EINVAL;
|
|
|
|
inode_t *ino = dir->inode;
|
|
if ((ino->mode & S_IFMT) != S_IFDIR)
|
|
return -ENOTDIR;
|
|
|
|
int res = dirent_populate_children(dir);
|
|
if (res < 0)
|
|
return res;
|
|
|
|
read_lock(&ino->rwlock);
|
|
|
|
if (!ctx->init) {
|
|
ctx->cookie = ino->dirents.head;
|
|
ctx->init = true;
|
|
}
|
|
|
|
list_item_t *item = ctx->cookie;
|
|
if (!item) {
|
|
read_unlock(&ino->rwlock);
|
|
return -ENOENT;
|
|
}
|
|
|
|
dirent_inc_ref(item->ptr);
|
|
*out = item->ptr;
|
|
ctx->cookie = item->next;
|
|
|
|
read_unlock(&ino->rwlock);
|
|
return 0;
|
|
}
|
|
|
|
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
|
{
|
|
if (!ino)
|
|
return -EINVAL;
|
|
|
|
write_lock(&ino->rwlock);
|
|
ssize_t ret = ino->i_ops.write(ino, buf, size, offset);
|
|
write_unlock(&ino->rwlock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
|
|
{
|
|
if (!ino)
|
|
return -EINVAL;
|
|
|
|
read_lock(&ino->rwlock);
|
|
ssize_t ret = ino->i_ops.read(ino, buf, size, offset);
|
|
read_unlock(&ino->rwlock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out)
|
|
{
|
|
if (!parent || !name || !out)
|
|
return -EINVAL;
|
|
|
|
inode_t *ino = parent->inode;
|
|
size_t len = strlen(name);
|
|
if (len > FILE_NAME_MAX_LEN)
|
|
return -ENAMETOOLONG;
|
|
|
|
if ((ino->mode & S_IFMT) != S_IFDIR)
|
|
return -ENOTDIR;
|
|
|
|
write_lock(&ino->rwlock);
|
|
|
|
if ((ino->mode & S_IFMT) != S_IFDIR) {
|
|
write_unlock(&ino->rwlock);
|
|
return -ENOTDIR;
|
|
}
|
|
|
|
dirent_t *test = NULL;
|
|
int res = dirent_lookup_locked(parent, name, &test);
|
|
if (res == 0) {
|
|
dirent_dec_ref(test);
|
|
write_unlock(&ino->rwlock);
|
|
return -EEXIST;
|
|
} else if (res != -ENOENT) {
|
|
write_unlock(&ino->rwlock);
|
|
return res;
|
|
}
|
|
|
|
fs_t *fs = parent->fs;
|
|
inode_t *new_ino;
|
|
dirent_t *new_dirent;
|
|
|
|
res = fs->fs_ops.alloc_inode(fs, mode, &new_ino);
|
|
if (res < 0) {
|
|
write_unlock(&ino->rwlock);
|
|
return res;
|
|
}
|
|
|
|
res = ino->i_ops.link(ino, new_ino, name, &new_dirent);
|
|
|
|
if (res < 0) {
|
|
fs->fs_ops.free_inode(fs, new_ino);
|
|
write_unlock(&ino->rwlock);
|
|
return res;
|
|
}
|
|
|
|
new_dirent->fs = fs;
|
|
new_dirent->parent = parent;
|
|
dirent_inc_ref(parent);
|
|
*out = new_dirent;
|
|
write_unlock(&ino->rwlock);
|
|
return 0;
|
|
}
|
|
|
|
int vfs_unlink(dirent_t *parent, const char *name)
|
|
{
|
|
if (!parent || !name)
|
|
return -EINVAL;
|
|
|
|
inode_t *ino = parent->inode;
|
|
|
|
write_lock(&ino->rwlock);
|
|
|
|
if ((ino->mode & S_IFMT) != S_IFDIR) {
|
|
write_unlock(&ino->rwlock);
|
|
return -ENOTDIR;
|
|
}
|
|
|
|
int res = ino->i_ops.unlink(ino, name);
|
|
|
|
write_unlock(&ino->rwlock);
|
|
return res;
|
|
}
|
|
|
|
void vfs_register(struct fs_driver *fs)
|
|
{
|
|
struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
|
if (!ins)
|
|
return;
|
|
|
|
memcpy(ins, fs, sizeof(*fs));
|
|
if (!list_insert(&fs_type_list, ins))
|
|
kfree(ins);
|
|
}
|
|
|
|
void vfs_init(void)
|
|
{
|
|
inode_init();
|
|
list_create(&fs_type_list);
|
|
|
|
void vfs_uapi_init();
|
|
vfs_uapi_init();
|
|
}
|