fs: add ramfs

This commit is contained in:
Michael Shaw 2026-05-12 07:31:17 -04:00
commit 750ffa2e16
13 changed files with 355 additions and 55 deletions

8
include/fs/ramfs.h Normal file
View file

@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _RAMFS_H_
#define _RAMFS_H_
#include <fs/vfs.h>
#endif /* _RAMFS_H_ */

View file

@ -3,6 +3,7 @@
#define _VFS_H_
#include <kernel/spinlock.h>
#include <drivers/block.h>
#include <lib/rwlock.h>
#include <lib/list.h>
@ -19,19 +20,30 @@
#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 fs_ops {
int (*alloc_inode)(fs_t *fs, inode_t *out);
int (*free_inode)(fs_t *fs, inode_t *inode);
struct fs_type {
char name[256];
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
void (*fs_umount)(fs_t *fs);
bool (*fs_detect)(struct block_device *bdev);
};
struct fs_superblock {
u64 type;
size_t block_size;
size_t n_blocks;
struct fs_ops {
int (*alloc_inode)(fs_t *fs, inode_t **out);
int (*free_inode)(fs_t *fs, inode_t *inode);
int (*open_inode)(fs_t *fs, u64 id, inode_t **out);
};
struct inode_ops {
@ -39,36 +51,50 @@ struct inode_ops {
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 (*mkdir)(inode_t *inode, const char *name, dirent_t *out);
int (*creat)(inode_t *inode, const char *name, dirent_t *out);
int (*link)(inode_t *inode, const char *name, dirent_t *out);
int (*mkdir)(inode_t *inode, const char *name, dirent_t **out);
int (*creat)(inode_t *inode, const char *name, dirent_t **out);
int (*link)(inode_t *inode, const char *name, dirent_t **out);
int (*unlink)(inode_t *inode, const char *name);
int (*lookup)(inode_t *parent, const char *name, dirent_t *out);
int (*readdir)(inode_t *parent);
};
struct _fs {
struct fs_superblock superblock;
struct _inode *root_inode;
u64 fs_type;
u64 type;
size_t block_size;
size_t n_blocks;
inode_t *root_inode;
spinlock_t lock;
struct fs_ops fs_ops;
};
struct _dirent {
inode_t *inode;
u64 ino_num;
size_t rec_len;
size_t name_len;
size_t flags;
u64 flags;
u64 type;
char *name;
};
struct _inode {
fs_t *fs;
u64 mode;
u64 ino_num;
u64 file_size;
list_t dentries;
void *data;
u64 refcount;
rwlock_t rwlock;
struct fs *fs;
rwlock_t rwlock;
struct _inode *parent_inode;
struct inode_ops i_ops;
};
@ -76,5 +102,8 @@ struct _inode {
void vfs_init();
inode_t *inode_create();
void inode_free(inode_t *inode);
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
dirent_t *dirent_create(const char *name);
void dirent_free(dirent_t *dirent);
#endif /* _VFS_H_ */

View file

@ -9,6 +9,8 @@
#include <kernel/types.h>
#include <lib/stdio.h>
#include <lib/math.h>
#include <lib/errno.h>
#define PACKED __attribute__((packed))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@ -26,6 +28,9 @@
_panic(); \
}
#define DRIVER_ENTRY(fn) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall"))) = fn
#define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn
void yield();
void _panic();

View file

@ -27,6 +27,7 @@ void slab_free(mem_cache_t *cache, void *ptr);
void *kalloc(size_t size, u64 flags);
void *kzalloc(size_t size, u64 flags);
void *krealloc(void *old, size_t size, u64 flags);
void kfree(void *ptr);
void kalloc_init();

View file

@ -8,7 +8,6 @@
#include <stddef.h>
#include <stdbool.h>
void *memcpy(void *dest, const void *src, size_t num);
int memcmp(const void *aa, const void *bb, size_t num);
int strcmp(const char *a, const char *b);
@ -17,6 +16,7 @@ void *memset(void *str, int c, size_t n);
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
char *strtok(char *str, const char *delim, char **llast);
char *strdup(const char *s);
bool strempty(const char *s);
char **strsplit(const char *str, char delim, int *num);
size_t strlen(const char *c);