vfs: remove kernel file descriptor numbers
This commit is contained in:
parent
18bcf40094
commit
f8b801236a
6 changed files with 119 additions and 87 deletions
97
fs/vfs.c
97
fs/vfs.c
|
|
@ -14,15 +14,8 @@ static int fd_counter = 0;
|
|||
|
||||
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)
|
||||
return -EISDIR;
|
||||
|
||||
|
|
@ -39,15 +32,8 @@ int write(int fd, void *buf, size_t count)
|
|||
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)
|
||||
return -EISDIR;
|
||||
|
||||
|
|
@ -64,27 +50,20 @@ int read(int fd, void *buf, size_t count)
|
|||
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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int open(const char *pathname, int flags)
|
||||
struct file_descriptor *open(const char *pathname, int flags)
|
||||
{
|
||||
/* allocate a file descriptor */
|
||||
struct file_descriptor *fd = slab_alloc(fd_slab);
|
||||
if (!fd) {
|
||||
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
|
||||
return -ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (result < 0) {
|
||||
return result;
|
||||
fd->fd = result;
|
||||
return fd;
|
||||
} else if (result == VFSE_IS_BDEV) {
|
||||
/* 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);
|
||||
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);
|
||||
|
||||
if (!fdnode)
|
||||
return -EBADF;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||
|
||||
struct rbnode *node = rbt_search(kfd, fdesc->fd);
|
||||
rbt_delete(kfd, node);
|
||||
slab_free(fd_slab, fdesc);
|
||||
rbt_delete(kfd, fdnode);
|
||||
|
||||
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) {
|
||||
case SEEK_SET:
|
||||
fdesc->pos = offset;
|
||||
|
|
@ -150,15 +117,8 @@ int seek(int fd, size_t offset, int whence)
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
@ -174,24 +134,19 @@ int mkdir(const char *pathname)
|
|||
|
||||
DIR *opendir(const char *name)
|
||||
{
|
||||
int fd = open(name, 0);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)rbt_search(kfd, fd)->value;
|
||||
|
||||
struct file_descriptor *fdesc = open(name, 0);
|
||||
if (!fdesc)
|
||||
return NULL;
|
||||
|
||||
struct fs *fs = fdesc->fs;
|
||||
|
||||
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
|
||||
close(fd);
|
||||
close(fdesc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
|
||||
dir->fd = fd;
|
||||
dir->fdesc = fdesc;
|
||||
dir->fs = fs;
|
||||
dir->vnode = &fdesc->vnode;
|
||||
dir->pos = 0;
|
||||
|
|
@ -200,6 +155,24 @@ DIR *opendir(const char *name)
|
|||
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)
|
||||
{
|
||||
if (!dir)
|
||||
|
|
@ -227,7 +200,7 @@ int closedir(DIR *dir)
|
|||
if (!dir)
|
||||
return -EBADF;
|
||||
|
||||
int ret = close(dir->fd);
|
||||
int ret = close(dir->fdesc);
|
||||
kfree(dir->dirents);
|
||||
kfree(dir);
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@
|
|||
#define O_TRUNC 0x0010
|
||||
#define O_APPEND 0x0020
|
||||
|
||||
#define FD_TYPE_CHARDEV 0x0001
|
||||
|
||||
typedef uint32_t ino_t;
|
||||
typedef uint8_t ftype_t;
|
||||
typedef size_t off_t;
|
||||
|
|
@ -55,7 +57,7 @@ typedef struct _DIR {
|
|||
struct dirent *dirents;
|
||||
size_t num_dirents;
|
||||
struct vnode *vnode;
|
||||
int fd;
|
||||
struct file_descriptor *fdesc;
|
||||
uint64_t pos;
|
||||
} DIR;
|
||||
|
||||
|
|
@ -77,6 +79,9 @@ struct file_descriptor {
|
|||
uint32_t flags;
|
||||
uint32_t mode;
|
||||
|
||||
char *buf;
|
||||
size_t buf_size;
|
||||
|
||||
struct fs *fs;
|
||||
};
|
||||
|
||||
|
|
@ -102,14 +107,14 @@ struct statbuf {
|
|||
};
|
||||
|
||||
void vfs_init(const char *rootfs);
|
||||
int write(int fd, void *buf, size_t count);
|
||||
int read(int fd, void *buf, size_t count);
|
||||
int open(const char *pathname, int flags);
|
||||
int close(int fd);
|
||||
int seek(int fd, size_t offset, int whence);
|
||||
size_t tell(int fd);
|
||||
int write(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);
|
||||
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 statfd(int fd, struct statbuf *statbuf);
|
||||
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf);
|
||||
int mkdir(const char *pathname);
|
||||
|
||||
DIR *opendir(const char *name);
|
||||
|
|
@ -118,5 +123,7 @@ int closedir(DIR *dir);
|
|||
|
||||
char *basename(char *path);
|
||||
char *dirname(char *path);
|
||||
struct file_descriptor *fd_special();
|
||||
void fd_special_free(struct file_descriptor *fd);
|
||||
|
||||
#endif /* _VFS_H_ */
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
#define PROC_STOPPED 1
|
||||
#define PROC_BLOCKED 2
|
||||
|
||||
typedef uint64_t pid_t;
|
||||
typedef int64_t pid_t;
|
||||
|
||||
struct procregs {
|
||||
uint64_t rax;
|
||||
|
|
@ -50,6 +50,8 @@ struct proc {
|
|||
|
||||
struct rbtree page_map;
|
||||
spinlock_t page_map_lock;
|
||||
|
||||
struct rbtree fd_map;
|
||||
};
|
||||
|
||||
struct procregs *proc_current_regs();
|
||||
|
|
|
|||
32
init/cmd.c
32
init/cmd.c
|
|
@ -73,9 +73,14 @@ int kcmd_cat(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int fd = open(argv[1], 0);
|
||||
if (fd < 0) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd));
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +173,17 @@ int kcmd_stat(int argc, char **argv)
|
|||
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;
|
||||
int ret = statfd(fd, &st);
|
||||
|
|
@ -207,9 +222,14 @@ int kcmd_elf(int argc, char **argv)
|
|||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
20
sched/load.c
20
sched/load.c
|
|
@ -38,20 +38,24 @@ int elf_check_compat(char *buf)
|
|||
|
||||
pid_t elf_load_proc(char *fname)
|
||||
{
|
||||
int fd = open(fname, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
struct file_descriptor *fdesc = open(fname, 0);
|
||||
|
||||
seek(fd, 0, SEEK_END);
|
||||
size_t size = tell(fd);
|
||||
seek(fd, 0, SEEK_SET);
|
||||
if (!fdesc)
|
||||
return -ENOENT;
|
||||
|
||||
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);
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
read(fd, buf, size);
|
||||
close(fd);
|
||||
read(fdesc, buf, size);
|
||||
close(fdesc);
|
||||
|
||||
int ret;
|
||||
|
||||
|
|
|
|||
26
sched/proc.c
26
sched/proc.c
|
|
@ -3,8 +3,11 @@
|
|||
#include <kernel/proc.h>
|
||||
#include <kernel/rbtree.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <dev/apic.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
|
||||
static struct rbtree *proc_tree;
|
||||
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 = kzalloc(sizeof(struct proc), ALLOC_KERN);
|
||||
|
|
@ -91,6 +112,11 @@ struct proc *proc_create()
|
|||
struct rbnode *proc_node = rbt_insert(proc_tree, proc->pid);
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue