gitignore: add .cpio

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-05-13 04:47:59 -04:00
commit f0a9ff299b
27 changed files with 253 additions and 245 deletions

View file

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

View file

@ -12,15 +12,18 @@ static mem_cache_t *dirent_slab = NULL;
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
{
list_item_t *p = inode->dentries.head;
dirent_t *ret = NULL;
while (p) {
dirent_t *dirent = (void *)p->ptr;
if (!strncmp(name, dirent->name, dirent->name_len))
return dirent;
if (!strncmp(name, dirent->name, dirent->name_len)) {
ret = dirent;
break;
}
p = p->next;
}
return NULL;
return ret;
}
dirent_t *dirent_create(const char *name)
@ -66,6 +69,30 @@ 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

@ -1,161 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* ramfs: in-ram filesystem
*
* notes:
* inode->ino_num is a the length of the current kalloc buffer
* inode->data is a pointer to the direct data block of the file
* dirent->ino_num is a pointer to the inode
*/
#include "fs/vfs.h"
#include "lib/errno.h"
#include "lib/list.h"
#include <kernel/common.h>
#include <kernel/slab.h>
#include <lib/string.h>
#include <fs/ramfs.h>
static int alloc_inode(fs_t *fs, inode_t **out)
{
const size_t alloc_len = 0x4000;
inode_t *ret = inode_create();
if (!ret)
return -ENOMEM;
memset(ret, 0, sizeof(*ret));
ret->fs = fs;
ret->ino_num = alloc_len;
ret->data = kalloc(alloc_len, ALLOC_KERN);
*out = ret;
return 0;
}
static int free_inode(fs_t *fs, inode_t *inode)
{
inode_free(inode);
return 0;
}
static int open_inode(fs_t *fs, u64 id, inode_t **out)
{
if (!id)
return -ENOENT;
*out = (void *)id;
return 0;
}
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
{
if (offset < inode->file_size) {
size = MIN(size, inode->file_size - offset);
memcpy(buf, inode->data + offset, size);
return size;
}
return 0;
}
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
{
size_t cur_len = inode->ino_num;
if (offset + size >= cur_len) {
size_t new_size = npow2(size + offset);
void *new_buf = krealloc(inode->data, new_size, ALLOC_KERN);
if (!new_buf)
return -ENOMEM;
inode->ino_num = new_size;
inode->data = new_buf;
}
memcpy(inode->data + offset, buf, size);
return size;
}
static int creat(inode_t *inode, const char *name, u64 mode, dirent_t **out)
{
if (inode_find_dirent(inode, name))
return -EEXIST;
dirent_t *dirent = dirent_create(name);
if (!dirent)
return -ENOMEM;
inode_t *new_ino;
int ret = alloc_inode(inode->fs, &new_ino);
if (ret < 0)
goto out_1;
dirent->inode = new_ino;
inode->mode = mode;
list_insert(&inode->dentries, dirent);
*out = dirent;
return 0;
out_1:
dirent_free(dirent);
return ret;
}
static int mkdir(inode_t *inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_DIRECTORY, &d);
if (ret >= 0)
*out = d;
return ret;
}
int link(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_SYMLINK, &d);
if (ret >= 0)
*out = d;
return ret;
}
static int unlink(inode_t *inode, const char *name)
{
if (inode->mode != INODE_DIRECTORY)
return -ENOTDIR;
list_item_t *item = inode->dentries.head;
size_t name_len = strlen(name);
while (item) {
dirent_t *ent = item->ptr;
if (!strncmp(name, ent->name, name_len)) {
list_remove(item);
dirent_free(ent);
return 0;
}
item = item->next;
}
return -ENOENT;
}
static int readdir(inode_t *parent)
{
if (parent->mode != INODE_DIRECTORY)
return -ENOTDIR;
return 0;
}
static dirent_t *ramfs_mount(int flags, struct block_device *bdev)
{
static struct inode_ops ramfs_inode_ops = {
.read = read, .write = write, .mkdir = mkdir, .creat = creat, .link = link, .unlink = unlink, .readdir = readdir};
static struct fs_ops ramfs_ops = {.alloc_inode = alloc_inode, .free_inode = free_inode, .open_inode = open_inode};
}
void ramfs_init()
{
struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = ramfs_mount};
}
DRIVER_ENTRY_PRIORITY(ramfs_init, 2);

View file

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "lib/rbtree.h"
#include <kernel/common.h>
#include <kernel/slab.h>
@ -13,65 +12,30 @@ 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)
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
{
if (!mount_point) {
if (root_fs) {
return -EBUSY;
} else {
root_fs = fs_to_mount;
}
return ino->i_ops.write(ino, buf, size, offset);
}
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
{
return ino->i_ops.read(ino, buf, size, offset);
}
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
{
if (!mount_point || !strcmp(mount_point, "/")) {
root_fs = fs_to_mount;
} else {
}
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);
}