vfs: begin restructuring of vfs

This commit is contained in:
Michael Shaw 2024-01-24 02:04:42 -05:00
commit 67050f1a93
3 changed files with 88 additions and 52 deletions

109
fs/vfs.c
View file

@ -9,6 +9,7 @@
static struct rbtree *kfd; static struct rbtree *kfd;
static slab_t *fd_slab; static slab_t *fd_slab;
static slab_t *file_slab;
static struct fs *rootfs; static struct fs *rootfs;
static int fd_counter = 0; static int fd_counter = 0;
@ -68,6 +69,38 @@ int read(struct file_descriptor *fdesc, void *buf, size_t count)
return ret; return ret;
} }
ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count)
{
/* TODO: implement */
if ((file->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_CHARDEV)
return -EBADF;
if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EISDIR;
return file->vnode.fs->ops->write(file->vnode.fs, &file->vnode, buf, off, count);
}
ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count)
{
/* TODO: implement */
if ((file->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_CHARDEV)
return -EBADF;
/* TODO: implement */
if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EISDIR;
return file->vnode.fs->ops->read(file->vnode.fs, &file->vnode, buf, off, count);
}
int vfs_statf(struct file *file, struct statbuf *statbuf)
{
statbuf->size = file->vnode.size;
return 0;
}
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf) int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf)
{ {
statbuf->size = fdesc->vnode.size; statbuf->size = fdesc->vnode.size;
@ -75,6 +108,32 @@ int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf)
return 0; return 0;
} }
struct file *vfs_open(const char *pathname, int *err)
{
/* allocate a file */
struct file *file = slab_alloc(file_slab);
if (!file) {
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
return NULL;
}
memset(file, 0, sizeof(struct file));
/* find the file */
int result = rootfs->ops->open(rootfs, &file->vnode, pathname);
if (result < 0) {
*err = result;
slab_free(file_slab, file);
return NULL;
} else if (result == VFSE_IS_BDEV) {
/* TODO: continue search into mounted filesystems */
} else {
}
return file;
}
struct file_descriptor *open(const char *pathname, int flags) struct file_descriptor *open(const char *pathname, int flags)
{ {
/* allocate a file descriptor */ /* allocate a file descriptor */
@ -116,28 +175,10 @@ int close(struct file_descriptor *fdesc)
return 0; return 0;
} }
int seek(struct file_descriptor *fdesc, size_t offset, int whence) int vfs_close(struct file *file)
{ {
switch (whence) { slab_free(file_slab, file);
case SEEK_SET: return 0;
fdesc->pos = offset;
break;
case SEEK_CUR:
fdesc->pos += offset;
break;
case SEEK_END:
fdesc->pos = fdesc->vnode.size + offset;
break;
default:
return -EINVAL;
}
return fdesc->pos;
}
size_t tell(struct file_descriptor *fdesc)
{
return fdesc->pos;
} }
int unlink(const char *pathname) int unlink(const char *pathname)
@ -152,26 +193,23 @@ int mkdir(const char *pathname)
DIR *opendir(const char *name) DIR *opendir(const char *name)
{ {
struct file_descriptor *fdesc = open(name, 0); int err;
if (!fdesc) struct file *file = vfs_open(name, &err);
return NULL; if (!file) {
kprintf(LOG_ERROR "Failed to open directory %s: %s\n", name, strerror(err));
if (fdesc->fd < 0) {
slab_free(fd_slab, fdesc);
return NULL; return NULL;
} }
struct fs *fs = fdesc->fs; struct fs *fs = file->vnode.fs;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) { if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
close(fdesc); vfs_close(file);
return NULL; return NULL;
} }
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN); DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
dir->fdesc = fdesc; dir->file = file;
dir->fs = fs; dir->fs = fs;
dir->vnode = &fdesc->vnode;
dir->pos = 0; dir->pos = 0;
dir->dirents = NULL; dir->dirents = NULL;
@ -203,11 +241,11 @@ struct dirent *readdir(DIR *dir)
struct fs *fs = dir->fs; struct fs *fs = dir->fs;
if ((dir->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) if ((dir->file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR)
return NULL; return NULL;
if (dir->dirents == NULL) if (dir->dirents == NULL)
dir->num_dirents = fs->ops->readdir(fs, dir->vnode->ino_num, &dir->dirents); dir->num_dirents = fs->ops->readdir(fs, dir->file->vnode.ino_num, &dir->dirents);
if (dir->dirents == NULL) if (dir->dirents == NULL)
return NULL; return NULL;
@ -223,7 +261,7 @@ int closedir(DIR *dir)
if (!dir) if (!dir)
return -EBADF; return -EBADF;
int ret = close(dir->fdesc); int ret = vfs_close(dir->file);
kfree(dir->dirents); kfree(dir->dirents);
kfree(dir); kfree(dir);
@ -293,4 +331,5 @@ void vfs_init(const char *rootdev_name)
/* init file descriptor slab */ /* init file descriptor slab */
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0); fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
file_slab = slab_create(sizeof(struct file), 16 * KB, 0);
} }

View file

@ -56,8 +56,7 @@ typedef struct _DIR {
struct fs *fs; struct fs *fs;
struct dirent *dirents; struct dirent *dirents;
size_t num_dirents; size_t num_dirents;
struct vnode *vnode; struct file *file;
struct file_descriptor *fdesc;
uint64_t pos; uint64_t pos;
} DIR; } DIR;
@ -67,15 +66,14 @@ struct file {
ino_t inode_num; /* inode number */ ino_t inode_num; /* inode number */
uint32_t type; /* type of the file */ uint32_t type; /* type of the file */
uint64_t size; /* size of the file */ uint64_t size; /* size of the file */
void *fs_file; /* filesystem specific object */
}; };
struct file_descriptor { struct file_descriptor {
struct file *file;
struct vnode vnode; struct vnode vnode;
uint64_t pos;
int fd; int fd;
uint64_t pos;
uint32_t flags; uint32_t flags;
uint32_t mode; uint32_t mode;
@ -110,11 +108,14 @@ struct statbuf {
void vfs_init(const char *rootfs); void vfs_init(const char *rootfs);
int write(struct file_descriptor *fdesc, void *buf, size_t count); int write(struct file_descriptor *fdesc, void *buf, size_t count);
ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count);
ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count);
int vfs_statf(struct file *file, struct statbuf *statbuf);
struct file *vfs_open(const char *pathname, int *err);
int vfs_close(struct file *file);
int read(struct file_descriptor *fdesc, void *buf, size_t count); int read(struct file_descriptor *fdesc, void *buf, size_t count);
struct file_descriptor *open(const char *pathname, int flags); struct file_descriptor *open(const char *pathname, int flags);
int close(struct file_descriptor *fdesc); int close(struct file_descriptor *fdesc);
int seek(struct file_descriptor *fdesc, size_t offset, int whence);
size_t tell(struct file_descriptor *fdesc);
int unlink(const char *pathname); int unlink(const char *pathname);
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf); int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf);
int mkdir(const char *pathname); int mkdir(const char *pathname);

View file

@ -38,24 +38,20 @@ int elf_check_compat(char *buf)
pid_t elf_load_proc(char *fname) pid_t elf_load_proc(char *fname)
{ {
struct file_descriptor *fdesc = open(fname, 0); int err;
struct file *file = vfs_open(fname, &err);
if (!fdesc) if (!file)
return -ENOENT; return -ENOENT;
if (fdesc->fd < 0) size_t size = file->vnode.size;
return fdesc->fd;
seek(fdesc, 0, SEEK_END);
size_t size = tell(fdesc);
seek(fdesc, 0, SEEK_SET);
char *buf = kmalloc(size, ALLOC_KERN); char *buf = kmalloc(size, ALLOC_KERN);
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
read(fdesc, buf, size); vfs_read(file, buf, 0, size);
close(fdesc); vfs_close(file);
int ret; int ret;