vfs: add vfs_open
vfs_open parses a path and returns the correct inode. The out inode is a double pointer argument passed to vfs_open, and the return value is an error code, or 0 Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
913b91189b
commit
a8540494a9
11 changed files with 345 additions and 137 deletions
|
|
@ -1,4 +1,3 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += vfs.o
|
||||
obj-y += inode.o
|
||||
obj-y += ramfs.o
|
||||
|
|
|
|||
149
fs/vfs/inode.c
149
fs/vfs/inode.c
|
|
@ -9,23 +9,111 @@
|
|||
static mem_cache_t *inode_slab = NULL;
|
||||
static mem_cache_t *dirent_slab = NULL;
|
||||
|
||||
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
||||
int inode_populate_dirents(inode_t *inode)
|
||||
{
|
||||
list_item_t *p = inode->dentries.head;
|
||||
dirent_t *ret = NULL;
|
||||
if (inode->dirents.head != NULL)
|
||||
return -EINVAL;
|
||||
write_lock(&inode->rwlock);
|
||||
|
||||
while (p) {
|
||||
dirent_t *dirent = (void *)p->ptr;
|
||||
if (!strncmp(name, dirent->name, dirent->name_len)) {
|
||||
ret = dirent;
|
||||
break;
|
||||
}
|
||||
p = p->next;
|
||||
dirent_t *dents;
|
||||
ssize_t n = inode->i_ops.get_dents(inode, &dents);
|
||||
|
||||
if (n < 0)
|
||||
return n;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
dirent_t *dent = dents + i;
|
||||
dirent_t *insert = kalloc(sizeof(dirent_t), ALLOC_KERN);
|
||||
|
||||
memcpy(insert, dent, sizeof(dirent_t));
|
||||
list_insert(&inode->dirents, insert);
|
||||
}
|
||||
|
||||
kfree(dents);
|
||||
|
||||
write_unlock(&inode->rwlock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
||||
{
|
||||
if (inode->mode != INODE_DIRECTORY)
|
||||
return -ENOTDIR;
|
||||
|
||||
ssize_t res;
|
||||
|
||||
if (inode->dirents.head == NULL) {
|
||||
res = inode_populate_dirents(inode);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (!strcmp(dent->name, name)) {
|
||||
/* found */
|
||||
*out = dent;
|
||||
return 0;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
inode_t *inode_create()
|
||||
{
|
||||
inode_t *ret = slab_alloc(inode_slab);
|
||||
|
||||
if (ret)
|
||||
memset(ret, 0, sizeof(*ret));
|
||||
|
||||
rwlock_init(&ret->rwlock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void inode_free(inode_t *inode)
|
||||
{
|
||||
if (inode->dirents.head) {
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (dent)
|
||||
dirent_free(dent);
|
||||
item = item->next;
|
||||
}
|
||||
}
|
||||
slab_free(inode_slab, inode);
|
||||
}
|
||||
|
||||
void inode_inc_ref(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode->refcount += 1;
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
void inode_dec_ref(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode->refcount -= 1;
|
||||
|
||||
if (inode->refcount == 0) {
|
||||
if (inode->parent_inode) {
|
||||
write_lock(&inode->parent_inode->rwlock);
|
||||
inode_dec_ref(inode->parent_inode);
|
||||
write_unlock(&inode->parent_inode->rwlock);
|
||||
}
|
||||
inode->fs->fs_ops.free_inode(inode->fs, inode);
|
||||
|
||||
return;
|
||||
}
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
dirent_t *dirent_create(const char *name)
|
||||
{
|
||||
dirent_t *ret = slab_alloc(dirent_slab);
|
||||
|
|
@ -52,47 +140,6 @@ void dirent_free(dirent_t *dirent)
|
|||
slab_free(dirent_slab, dirent);
|
||||
}
|
||||
|
||||
inode_t *inode_create()
|
||||
{
|
||||
inode_t *ret = slab_alloc(inode_slab);
|
||||
|
||||
if (ret)
|
||||
memset(ret, 0, sizeof(*ret));
|
||||
|
||||
rwlock_init(&ret->rwlock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void inode_free(inode_t *inode)
|
||||
{
|
||||
slab_free(inode_slab, inode);
|
||||
}
|
||||
|
||||
void inode_inc_ref(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode->refcount += 1;
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
void inode_dec_ref(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode->refcount -= 1;
|
||||
write_unlock(&inode->rwlock);
|
||||
|
||||
if (inode->refcount == 0) {
|
||||
if (inode->parent_inode) {
|
||||
write_lock(&inode->parent_inode->rwlock);
|
||||
inode_dec_ref(inode->parent_inode);
|
||||
write_unlock(&inode->parent_inode->rwlock);
|
||||
}
|
||||
inode->fs->fs_ops.free_inode(inode->fs, inode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void inode_init()
|
||||
{
|
||||
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
||||
|
|
|
|||
149
fs/vfs/vfs.c
149
fs/vfs/vfs.c
|
|
@ -2,24 +2,165 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
#include <lib/string.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/errno.h>
|
||||
|
||||
static fs_t *root_fs = NULL;
|
||||
|
||||
static list_t fs_type_list = {0};
|
||||
|
||||
static char *path_trim(const char *name)
|
||||
{
|
||||
size_t len = strlen(name);
|
||||
char *ret = kzalloc(len, ALLOC_KERN);
|
||||
const char *zeroptr = NULL;
|
||||
|
||||
/* trim trailing slashes */
|
||||
for (size_t i = len - 1; i >= 1; i++) {
|
||||
if (name[i] == '/')
|
||||
zeroptr = &name[i];
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
/* trim double slashes */
|
||||
char c;
|
||||
char last = 0;
|
||||
size_t i = 0;
|
||||
while (name < zeroptr && (c = *name++)) {
|
||||
if (last == c && c == '/')
|
||||
continue;
|
||||
|
||||
last = c;
|
||||
ret[i++] = last;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int next_level(const char *path, inode_t **cur)
|
||||
{
|
||||
if (!cur || !*cur)
|
||||
return -EINVAL;
|
||||
|
||||
int res;
|
||||
char build[256] = {0};
|
||||
|
||||
for (int i = 0; i < sizeof(build) - 1; i++) {
|
||||
char c = path[i];
|
||||
|
||||
if (c == '/' || c == '\0') {
|
||||
dirent_t *dirent;
|
||||
res = inode_find_dirent(*cur, build, &dirent);
|
||||
if (res < 0)
|
||||
return -ENOENT;
|
||||
spinlock_acquire(&dirent->lock);
|
||||
if (dirent->inode) {
|
||||
*cur = dirent->inode;
|
||||
} else {
|
||||
fs_t *fs = (*cur)->fs;
|
||||
res = fs->fs_ops.open_inode(fs, dirent->ino_num, cur);
|
||||
if (res < 0) {
|
||||
spinlock_release(&dirent->lock);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
spinlock_release(&dirent->lock);
|
||||
inode_inc_ref(*cur);
|
||||
return i;
|
||||
} else {
|
||||
build[i] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int vfs_open(const char *abs_path, inode_t **out)
|
||||
{
|
||||
if (!abs_path || !out)
|
||||
return -EINVAL;
|
||||
|
||||
inode_t *c_ino = root_fs->root_inode;
|
||||
char *path_optr = path_trim(abs_path);
|
||||
char *cur_path = path_optr;
|
||||
list_item_t *item;
|
||||
|
||||
list_t visited;
|
||||
list_create(&visited);
|
||||
|
||||
/* root case */
|
||||
if (*cur_path == '\0') {
|
||||
*out = c_ino;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
int res = next_level(cur_path, &c_ino);
|
||||
|
||||
cur_path += res;
|
||||
|
||||
if (!c_ino)
|
||||
goto out;
|
||||
|
||||
list_insert(&visited, c_ino);
|
||||
|
||||
if (res < 0)
|
||||
goto out;
|
||||
|
||||
/* found */
|
||||
if (res == 0) {
|
||||
*out = c_ino;
|
||||
return 0;
|
||||
}
|
||||
|
||||
cur_path += res;
|
||||
}
|
||||
|
||||
out:
|
||||
item = visited.head;
|
||||
while (item) {
|
||||
inode_t *inode = item->ptr;
|
||||
inode_dec_ref(inode);
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
kfree(path_optr);
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
void vfs_close(inode_t *inode)
|
||||
{
|
||||
inode_dec_ref(inode);
|
||||
}
|
||||
|
||||
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
return ino->i_ops.write(ino, buf, size, offset);
|
||||
ssize_t ret;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
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)
|
||||
{
|
||||
return ino->i_ops.read(ino, buf, size, offset);
|
||||
ssize_t ret;
|
||||
|
||||
read_lock(&ino->rwlock);
|
||||
ret = ino->i_ops.read(ino, buf, size, offset);
|
||||
read_unlock(&ino->rwlock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
||||
|
|
|
|||
Loading…
Reference in a new issue