lib: fs: implement mounting filesystems; reading, writing from files

This commit is contained in:
2026-03-10 19:15:26 +00:00
parent aef0163017
commit 6d88cf4bf3
14 changed files with 393 additions and 41 deletions

View File

@@ -2,30 +2,54 @@
#define FS_CONTEXT_H_
#include <rosetta/fs.h>
#include <xpc/msg.h>
struct fs_file;
struct fs_dentry;
struct fs_context;
struct fs_allocator;
struct fs_superblock;
extern struct fs_context *fs_context_create(
struct fs_allocator *alloc,
struct fs_superblock *sb);
enum fs_mount_flags {
FS_MOUNT_READONLY = 0x01u,
};
typedef enum fs_status (*fs_mount_function_t)(
struct fs_context *,
void *arg,
enum fs_mount_flags,
struct fs_superblock **);
extern struct fs_context *fs_context_create(struct fs_allocator *alloc);
extern void fs_context_destroy(struct fs_context *ctx);
extern enum fs_status fs_context_mount_filesystem(
struct fs_context *ctx,
fs_mount_function_t func,
void *arg,
enum fs_mount_flags flags);
extern enum fs_status fs_context_unmount_filesystem(struct fs_context *ctx);
extern struct fs_file *fs_context_open_file(
struct fs_context *ctx,
unsigned long id);
extern struct fs_file *fs_context_get_file(
struct fs_context *ctx,
unsigned long id);
extern void fs_context_close_file(struct fs_context *ctx, struct fs_file *f);
extern struct fs_dentry *fs_context_resolve_path(
extern enum fs_status fs_context_resolve_path(
struct fs_context *ctx,
const char *path);
const char *path,
struct fs_dentry **out);
extern kern_status_t fs_context_dispatch_msg(
struct fs_context *ctx,
kern_handle_t channel,
struct msg_endpoint *sender,
struct msg_header *hdr);
xpc_msg_t *msg);
extern void *fs_context_alloc(struct fs_context *ctx, size_t count);
extern void *fs_context_calloc(struct fs_context *ctx, size_t count, size_t sz);
extern void *fs_context_realloc(struct fs_context *ctx, void *p, size_t count);
extern void fs_context_free(struct fs_context *ctx, void *p);
#endif

View File

@@ -13,6 +13,7 @@ struct fs_dentry {
struct fs_superblock *d_sb;
const struct fs_dentry_ops *d_ops;
void *d_fsdata;
char *d_name;
};
#endif

View File

@@ -5,11 +5,31 @@
#include <stddef.h>
struct fs_file;
struct xpc_buffer;
struct fs_file_ops {
ssize_t (*f_read)(struct fs_file *, void *, size_t);
ssize_t (*f_write)(struct fs_file *, const void *, size_t);
off_t (*f_seek)(struct fs_file *, off_t, int);
enum fs_status (*f_read)(
struct fs_file *,
struct xpc_buffer *,
size_t,
off_t *);
enum fs_status (*f_write)(
struct fs_file *,
const struct xpc_buffer *,
size_t,
off_t *);
enum fs_status (*f_seek)(struct fs_file *, off_t, int);
};
extern struct fs_inode *fs_file_get_inode(const struct fs_file *f);
extern size_t fs_file_get_cursor(const struct fs_file *f);
extern enum fs_status fs_file_read(
struct fs_file *f,
struct xpc_buffer *buf,
size_t count);
extern enum fs_status fs_file_write(
struct fs_file *f,
struct xpc_buffer *buf,
size_t count);
#endif

View File

@@ -1,17 +1,37 @@
#ifndef FS_INODE_H_
#define FS_INODE_H_
#include <fs/status.h>
#include <stddef.h>
struct fs_inode;
struct fs_dentry;
struct fs_superblock;
struct fs_file_ops;
enum fs_inode_mode {
FS_INODE_REG = 0x01u,
FS_INODE_DIR = 0x02u,
};
struct fs_inode_ops {
int (*i_lookup)(struct fs_inode *, struct fs_dentry *);
enum fs_status (*i_lookup)(
struct fs_inode *,
const char *,
struct fs_dentry **);
};
struct fs_inode {
enum fs_inode_mode i_mode;
struct fs_superblock *i_sb;
const struct fs_inode_ops *i_ops;
const struct fs_file_ops *i_fops;
size_t i_size;
};
extern enum fs_status fs_inode_lookup(
struct fs_inode *inode,
const char *name,
struct fs_dentry **out);
#endif

View File

@@ -0,0 +1,18 @@
#ifndef FS_STATUS_H_
#define FS_STATUS_H_
enum fs_status {
FS_SUCCESS = 0,
FS_ERR_NO_ENTRY,
FS_ERR_NO_MEMORY,
FS_ERR_INVALID_ARGUMENT,
FS_ERR_NOT_IMPLEMENTED,
FS_ERR_IS_DIRECTORY,
FS_ERR_NOT_DIRECTORY,
FS_ERR_BAD_STATE,
FS_ERR_INTERNAL_FAILURE,
};
extern int fs_status_to_errno(enum fs_status status);
#endif