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:
Michael Shaw 2026-07-01 23:06:39 +02:00
commit a8540494a9
11 changed files with 345 additions and 137 deletions

View file

@ -228,5 +228,7 @@ void ahci_init()
ahci_init_device(item->ptr);
item = item->next;
}
krdy("AHCI ready\n");
}
DRIVER_ENTRY_PRIORITY(ahci_init, 3);

View file

@ -3,12 +3,12 @@
#include <drivers/block.h>
inline ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n)
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n)
{
return dev->ops.read(dev, buf, offset, n);
}
inline ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n)
ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n)
{
return dev->ops.write(dev, buf, offset, n);
}

View file

@ -1,4 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += vfs.o
obj-y += inode.o
obj-y += ramfs.o

View file

@ -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);

View file

@ -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)

95
include/fs/inode.h Normal file
View file

@ -0,0 +1,95 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _INODE_H_
#define _INODE_H_
#include <kernel/types.h>
#include <lib/list.h>
#include <lib/rwlock.h>
#define DIRENT_UNKNOWN_TYPE 0x0
#define DIRENT_REGULAR_TYPE 0x1
#define DIRENT_DIRECTORY_TYPE 0x2
#define DIRENT_CHRDEV_TYPE 0x3
#define DIRENT_BLKDEV_TYPE 0x4
#define DIRENT_FIFO_TYPE 0x5
#define DIRENT_SOCKET_TYPE 0x6
#define DIRENT_SYMLINK_TYPE 0x7
#define DIRENT_MOUNT_POINT 0x8
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
#define INODE_UNKNOWN 0x0
#define INODE_REGULAR 0x1
#define INODE_DIRECTORY 0x2
#define INODE_CHRDEV 0x3
#define INODE_BLKDEV 0x4
#define INODE_FIFO 0x5
#define INODE_SOCKET 0x6
#define INODE_SYMLINK 0x7
typedef struct _inode inode_t;
typedef struct _dirent dirent_t;
typedef struct _fs fs_t;
struct inode_ops {
/* for all files */
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
/* for directory files */
int (*creat)(inode_t *inode, const char *name, u64 mode, dirent_t **out);
int (*mkdir)(inode_t *inode, const char *name, dirent_t **out);
int (*link)(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out);
int (*unlink)(inode_t *inode, const char *name);
ssize_t (*get_dents)(inode_t *parent, dirent_t **dents);
};
struct _inode {
fs_t *fs;
u64 mode;
u64 ino_num;
u64 file_size;
u64 flags;
void *data;
u64 refcount;
rwlock_t rwlock;
list_t dirents;
struct _inode *parent_inode;
struct inode_ops i_ops;
};
struct _dirent {
inode_t *inode;
union {
u64 ino_num;
fs_t *fs_ptr;
};
size_t rec_len;
size_t name_len;
u64 flags;
u64 type;
spinlock_t lock;
char *name;
};
/* dirent ops */
dirent_t *dirent_create(const char *name);
void dirent_free(dirent_t *dirent);
/* inode ops */
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out);
inode_t *inode_create();
void inode_free(inode_t *inode);
void inode_inc_ref(inode_t *inode);
void inode_dec_ref(inode_t *inode);
#endif /* _INODE_H_ */

View file

@ -3,38 +3,13 @@
#define _VFS_H_
#include <kernel/spinlock.h>
#include <fs/inode.h>
#include <drivers/block.h>
#include <lib/rwlock.h>
#include <lib/list.h>
#define DIRENT_UNKNOWN_TYPE 0x0
#define DIRENT_REGULAR_TYPE 0x1
#define DIRENT_DIRECTORY_TYPE 0x2
#define DIRENT_CHRDEV_TYPE 0x3
#define DIRENT_BLKDEV_TYPE 0x4
#define DIRENT_FIFO_TYPE 0x5
#define DIRENT_SOCKET_TYPE 0x6
#define DIRENT_SYMLINK_TYPE 0x7
#define DIRENT_MOUNT_POINT 0x8
#define DIRENT_TYPE(dirent) ((dirent)->flags & 0x7)
#define INODE_UNKNOWN 0x0
#define INODE_REGULAR 0x1
#define INODE_DIRECTORY 0x2
#define INODE_CHRDEV 0x3
#define INODE_BLKDEV 0x4
#define INODE_FIFO 0x5
#define INODE_SOCKET 0x6
#define INODE_SYMLINK 0x7
#define I_NO_FREE 0x1
typedef struct _inode inode_t;
typedef struct _dirent dirent_t;
typedef struct _fs fs_t;
struct fs_type {
char name[256];
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
@ -47,18 +22,6 @@ struct fs_ops {
int (*open_inode)(fs_t *fs, u64 id, inode_t **out);
};
struct inode_ops {
/* for all files */
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
/* for directory files */
int (*creat)(inode_t *inode, const char *name, u64 mode, dirent_t **out);
int (*mkdir)(inode_t *inode, const char *name, dirent_t **out);
int (*link)(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out);
int (*unlink)(inode_t *inode, const char *name);
int (*readdir)(inode_t *parent);
};
struct _fs {
u64 type;
size_t block_size;
@ -73,41 +36,6 @@ struct _fs {
struct fs_ops fs_ops;
};
struct _dirent {
inode_t *inode;
union {
u64 ino_num;
fs_t *fs_ptr;
};
size_t rec_len;
size_t name_len;
u64 flags;
u64 type;
char *name;
};
struct _inode {
fs_t *fs;
u64 mode;
u64 ino_num;
u64 file_size;
u64 flags;
list_t dentries;
void *data;
u64 refcount;
rwlock_t rwlock;
struct _inode *parent_inode;
struct inode_ops i_ops;
};
struct ram_file {
u64 type;
u64 extra;
@ -119,16 +47,14 @@ struct ram_file {
size_t file_size;
};
/* VFS ops */
void vfs_init();
void vfs_register(struct fs_type *fs);
dirent_t *dirent_create(const char *name);
void dirent_free(dirent_t *dirent);
int vfs_open(const char *abs_path, inode_t **out);
void vfs_close(inode_t *inode);
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
inode_t *inode_create();
void inode_free(inode_t *inode);
void inode_inc_ref(inode_t *inode);
void inode_dec_ref(inode_t *inode);
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);
#endif /* _VFS_H_ */

View file

@ -17,6 +17,7 @@
#define EBUSY 12
#define ENOSYS 13
#define ESYSCALLBLK 14
#define ENAMETOOLONG 15
#ifndef __ASM__

View file

@ -6,6 +6,8 @@
struct tty;
#define STRINGIZE(x) #x
#define COL_RED_BLACK "\033[31m"
#define COL_YELLOW_BLACK "\033[33m"
#define COL_GREEN_BLACK "\033[32m"
@ -27,7 +29,7 @@ struct tty;
#define kinfo(...) (kprintf(LOG_INFO __VA_ARGS__))
#define kwarn(...) (kprintf(LOG_WARN __VA_ARGS__))
#define kerr(...) (kprintf(LOG_ERROR __VA_ARGS__))
#define krdy(...) (kprintf(LOG_READY __VA_ARGS__))
#define krdy(...) (kprintf(LOG_READY __FILE__ ":" STRINGIZE(__LINE__)"(" STRINGIZE(__func__) ") " __VA_ARGS__))
#ifdef KDEBUG
#define kdebug(...) (kprintf(LOG_DEBUG __VA_ARGS__))
#else

View file

@ -17,9 +17,10 @@ static const char *errno_str[] = {
[EAGAIN] = "Try again",
[EBUSY] = "Device or resource busy",
[ENOSYS] = "Function not implemented",
[ENAMETOOLONG] = "Name too long",
};
inline const char *strerror(int errnum)
const char *strerror(int errnum)
{
if (errnum < 0 || (size_t)errnum >= ARRAY_SIZE(errno_str))
return "Unknown error";

View file

@ -15,9 +15,7 @@ void *memcpy(void *dest, const void *src, size_t num)
int strcmp(const char *a, const char *b)
{
while (*a && *b) {
if (*a != *b)
return *a - *b;
while (*a && *a == *b) {
a++;
b++;
}
@ -26,12 +24,14 @@ int strcmp(const char *a, const char *b)
int strncmp(const char *a, const char *b, size_t n)
{
if (n == 0)
return 0;
for (size_t i = 0; i < n; i++) {
if (a[i] != b[i])
if (a[i] != b[i] || !a[i] || !b[i])
return a[i] - b[i];
if (!a[i] || !b[i])
break;
}
return 0;
}
@ -177,24 +177,21 @@ static char *udirname(char *path)
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return ".";
return ".";
if (last_slash_idx == 0)
return "/";
return "/";
path[last_slash_idx] = '\0';
path[last_slash_idx] = '\0';
return path;
}
@ -203,18 +200,15 @@ static char *ubasename(char *path)
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return path;
if (path[last_slash_idx + 1] == '\0' && last_slash_idx == 0)