vfs: remove kernel file descriptor numbers

This commit is contained in:
Michael Shaw 2023-11-22 21:09:05 -05:00
commit f8b801236a
6 changed files with 119 additions and 87 deletions

View file

@ -14,15 +14,8 @@ static int fd_counter = 0;
typedef struct fs *(*vfs_init_t)(struct block_device *); typedef struct fs *(*vfs_init_t)(struct block_device *);
int write(int fd, void *buf, size_t count) int write(struct file_descriptor *fdesc, void *buf, size_t count)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR; return -EISDIR;
@ -39,15 +32,8 @@ int write(int fd, void *buf, size_t count)
return ret; return ret;
} }
int read(int fd, void *buf, size_t count) int read(struct file_descriptor *fdesc, void *buf, size_t count)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR; return -EISDIR;
@ -64,27 +50,20 @@ int read(int fd, void *buf, size_t count)
return ret; return ret;
} }
int statfd(int fd, struct statbuf *statbuf) int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
statbuf->size = fdesc->vnode.size; statbuf->size = fdesc->vnode.size;
return 0; return 0;
} }
int open(const char *pathname, int flags) struct file_descriptor *open(const char *pathname, int flags)
{ {
/* allocate a file descriptor */ /* allocate a file descriptor */
struct file_descriptor *fd = slab_alloc(fd_slab); struct file_descriptor *fd = slab_alloc(fd_slab);
if (!fd) { if (!fd) {
kprintf(LOG_ERROR "Failed to allocate file descriptor\n"); kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
return -ENOMEM; return NULL;
} }
memset(fd, 0, sizeof(struct file_descriptor)); memset(fd, 0, sizeof(struct file_descriptor));
@ -93,7 +72,8 @@ int open(const char *pathname, int flags)
int result = rootfs->ops->open(rootfs, &fd->vnode, pathname); int result = rootfs->ops->open(rootfs, &fd->vnode, pathname);
if (result < 0) { if (result < 0) {
return result; fd->fd = result;
return fd;
} else if (result == VFSE_IS_BDEV) { } else if (result == VFSE_IS_BDEV) {
/* TODO: continue search into mounted filesystems */ /* TODO: continue search into mounted filesystems */
} }
@ -106,33 +86,20 @@ int open(const char *pathname, int flags)
struct rbnode *fdnode = rbt_insert(kfd, fd->fd); struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
fdnode->value = (uint64_t)fd; fdnode->value = (uint64_t)fd;
return fd->fd; return fd;
} }
int close(int fd) int close(struct file_descriptor *fdesc)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd); struct rbnode *node = rbt_search(kfd, fdesc->fd);
rbt_delete(kfd, node);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
slab_free(fd_slab, fdesc); slab_free(fd_slab, fdesc);
rbt_delete(kfd, fdnode);
return 0; return 0;
} }
int seek(int fd, size_t offset, int whence) int seek(struct file_descriptor *fdesc, size_t offset, int whence)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
switch (whence) { switch (whence) {
case SEEK_SET: case SEEK_SET:
fdesc->pos = offset; fdesc->pos = offset;
@ -150,15 +117,8 @@ int seek(int fd, size_t offset, int whence)
return fdesc->pos; return fdesc->pos;
} }
size_t tell(int fd) size_t tell(struct file_descriptor *fdesc)
{ {
struct rbnode *fdnode = rbt_search(kfd, fd);
if (!fdnode)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
return fdesc->pos; return fdesc->pos;
} }
@ -174,24 +134,19 @@ int mkdir(const char *pathname)
DIR *opendir(const char *name) DIR *opendir(const char *name)
{ {
int fd = open(name, 0); struct file_descriptor *fdesc = open(name, 0);
if (fd < 0)
return NULL;
struct file_descriptor *fdesc = (struct file_descriptor *)rbt_search(kfd, fd)->value;
if (!fdesc) if (!fdesc)
return NULL; return NULL;
struct fs *fs = fdesc->fs; struct fs *fs = fdesc->fs;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) { if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
close(fd); close(fdesc);
return NULL; return NULL;
} }
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN); DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
dir->fd = fd; dir->fdesc = fdesc;
dir->fs = fs; dir->fs = fs;
dir->vnode = &fdesc->vnode; dir->vnode = &fdesc->vnode;
dir->pos = 0; dir->pos = 0;
@ -200,6 +155,24 @@ DIR *opendir(const char *name)
return dir; return dir;
} }
struct file_descriptor *fd_special()
{
struct file_descriptor *fd = slab_alloc(fd_slab);
if (!fd) {
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
return NULL;
}
memset(fd, 0, sizeof(struct file_descriptor));
return fd;
}
void fd_special_free(struct file_descriptor *fd)
{
slab_free(fd_slab, fd);
}
struct dirent *readdir(DIR *dir) struct dirent *readdir(DIR *dir)
{ {
if (!dir) if (!dir)
@ -227,7 +200,7 @@ int closedir(DIR *dir)
if (!dir) if (!dir)
return -EBADF; return -EBADF;
int ret = close(dir->fd); int ret = close(dir->fdesc);
kfree(dir->dirents); kfree(dir->dirents);
kfree(dir); kfree(dir);

View file

@ -28,6 +28,8 @@
#define O_TRUNC 0x0010 #define O_TRUNC 0x0010
#define O_APPEND 0x0020 #define O_APPEND 0x0020
#define FD_TYPE_CHARDEV 0x0001
typedef uint32_t ino_t; typedef uint32_t ino_t;
typedef uint8_t ftype_t; typedef uint8_t ftype_t;
typedef size_t off_t; typedef size_t off_t;
@ -55,7 +57,7 @@ typedef struct _DIR {
struct dirent *dirents; struct dirent *dirents;
size_t num_dirents; size_t num_dirents;
struct vnode *vnode; struct vnode *vnode;
int fd; struct file_descriptor *fdesc;
uint64_t pos; uint64_t pos;
} DIR; } DIR;
@ -77,6 +79,9 @@ struct file_descriptor {
uint32_t flags; uint32_t flags;
uint32_t mode; uint32_t mode;
char *buf;
size_t buf_size;
struct fs *fs; struct fs *fs;
}; };
@ -102,14 +107,14 @@ struct statbuf {
}; };
void vfs_init(const char *rootfs); void vfs_init(const char *rootfs);
int write(int fd, void *buf, size_t count); int write(struct file_descriptor *fdesc, void *buf, size_t count);
int read(int fd, void *buf, size_t count); int read(struct file_descriptor *fdesc, void *buf, size_t count);
int open(const char *pathname, int flags); struct file_descriptor *open(const char *pathname, int flags);
int close(int fd); int close(struct file_descriptor *fdesc);
int seek(int fd, size_t offset, int whence); int seek(struct file_descriptor *fdesc, size_t offset, int whence);
size_t tell(int fd); size_t tell(struct file_descriptor *fdesc);
int unlink(const char *pathname); int unlink(const char *pathname);
int statfd(int fd, struct statbuf *statbuf); int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf);
int mkdir(const char *pathname); int mkdir(const char *pathname);
DIR *opendir(const char *name); DIR *opendir(const char *name);
@ -118,5 +123,7 @@ int closedir(DIR *dir);
char *basename(char *path); char *basename(char *path);
char *dirname(char *path); char *dirname(char *path);
struct file_descriptor *fd_special();
void fd_special_free(struct file_descriptor *fd);
#endif /* _VFS_H_ */ #endif /* _VFS_H_ */

View file

@ -11,7 +11,7 @@
#define PROC_STOPPED 1 #define PROC_STOPPED 1
#define PROC_BLOCKED 2 #define PROC_BLOCKED 2
typedef uint64_t pid_t; typedef int64_t pid_t;
struct procregs { struct procregs {
uint64_t rax; uint64_t rax;
@ -50,6 +50,8 @@ struct proc {
struct rbtree page_map; struct rbtree page_map;
spinlock_t page_map_lock; spinlock_t page_map_lock;
struct rbtree fd_map;
}; };
struct procregs *proc_current_regs(); struct procregs *proc_current_regs();

View file

@ -73,9 +73,14 @@ int kcmd_cat(int argc, char **argv)
return 1; return 1;
} }
int fd = open(argv[1], 0); struct file_descriptor *fd = open(argv[1], 0);
if (fd < 0) { if (!fd) {
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd)); kprintf("Failed to open %s: fd is NULL\n", argv[1]);
return 1;
}
if (fd->fd < 0) {
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
return 1; return 1;
} }
@ -168,7 +173,17 @@ int kcmd_stat(int argc, char **argv)
return 1; return 1;
} }
int fd = open(argv[1], 0); struct file_descriptor *fd = open(argv[1], 0);
if (!fd) {
kprintf("Failed to open %s: fd is NULL\n", argv[1]);
return 1;
}
if (fd->fd < 0) {
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
return 1;
}
struct statbuf st; struct statbuf st;
int ret = statfd(fd, &st); int ret = statfd(fd, &st);
@ -207,9 +222,14 @@ int kcmd_elf(int argc, char **argv)
return 1; return 1;
} }
int fd = open(argv[1], 0); struct file_descriptor *fd = open(argv[1], 0);
if (fd == NULL) {
kprintf("Failed to open %s: fd is NULL\n", argv[1]);
return 1;
}
if (fd < 0) { if (fd < 0) {
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd)); kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
return 1; return 1;
} }

View file

@ -38,20 +38,24 @@ int elf_check_compat(char *buf)
pid_t elf_load_proc(char *fname) pid_t elf_load_proc(char *fname)
{ {
int fd = open(fname, O_RDONLY); struct file_descriptor *fdesc = open(fname, 0);
if (fd < 0)
return fd;
seek(fd, 0, SEEK_END); if (!fdesc)
size_t size = tell(fd); return -ENOENT;
seek(fd, 0, SEEK_SET);
if (fdesc->fd < 0)
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(fd, buf, size); read(fdesc, buf, size);
close(fd); close(fdesc);
int ret; int ret;

View file

@ -3,8 +3,11 @@
#include <kernel/proc.h> #include <kernel/proc.h>
#include <kernel/rbtree.h> #include <kernel/rbtree.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <dev/apic.h> #include <dev/apic.h>
#include <fs/vfs.h>
static struct rbtree *proc_tree; static struct rbtree *proc_tree;
static pid_t *proc_current; static pid_t *proc_current;
@ -81,6 +84,24 @@ void schedule()
} }
} }
static int proc_insert_charfd(struct proc *proc, int fdno, int flags)
{
struct file_descriptor *fd = fd_special();
fd->vnode = (struct vnode){ 0 };
fd->pos = 0;
fd->fd = fdno;
fd->flags = flags;
fd->mode = FD_TYPE_CHARDEV;
fd->buf = NULL;
fd->buf_size = 0;
fd->fs = NULL;
struct rbnode *fd_node = rbt_insert(&proc->fd_map, fdno);
fd_node->value = (uintptr_t)fd;
return fdno;
}
struct proc *proc_create() struct proc *proc_create()
{ {
struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN); struct proc *proc = kzalloc(sizeof(struct proc), ALLOC_KERN);
@ -91,6 +112,11 @@ struct proc *proc_create()
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid); struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
proc_node->value = (uintptr_t)proc; proc_node->value = (uintptr_t)proc;
/* insert stdin, stdout, stderr */
proc_insert_charfd(proc, 0, O_RDONLY); /* stdin */
proc_insert_charfd(proc, 1, O_WRONLY); /* stdout */
proc_insert_charfd(proc, 2, O_WRONLY); /* stderr */
return proc; return proc;
} }