pipes: initial implementation

This commit is contained in:
Matheus
2022-10-10 10:46:27 -03:00
parent af69128800
commit 85bd0c0412
7 changed files with 158 additions and 6 deletions
+25 -4
View File
@@ -3,6 +3,7 @@
#include <stdbool.h>
#include <kernel/alloc.h>
#include <string.h>
#include <kernel/pipe.h>
int fd_release(fd_t* fd){
spinlock_release(&fd->lock);
@@ -99,18 +100,36 @@ int fd_free(fdtable_t* fdtable, int ifd){
}
fd_t* fd = fdtable->fd[ifd];
spinlock_acquire(&fd->lock);
if(--fd->refcount > 0){
if(__atomic_sub_fetch(&fd->refcount, 1, __ATOMIC_RELAXED) > 0){
fdtable->fd[ifd] = NULL;
spinlock_release(&fd->lock);
spinlock_release(&fdtable->lock);
return 0;
}
spinlock_acquire(&fd->lock);
fdtable->fd[ifd] = NULL;
spinlock_release(&fdtable->lock);
if(GETTYPE(fd->mode) == TYPE_FIFO){
pipe_t* pipe = fd->node->objdata;
spinlock_acquire(&pipe->lock);
if(FD_FLAGS_READ & fd->flags){
pipe->readers--;
event_signal(&pipe->revent, true);
}
if(FD_FLAGS_WRITE & fd->flags){
pipe->writers--;
event_signal(&pipe->wevent, true);
}
spinlock_release(&pipe->lock);
}
int err = 0;
if(fd->node)
err = vfs_close(fd->node);
@@ -230,7 +249,7 @@ int fd_duplicate(fdtable_t* table, int src, int dest, int type, int* ret){
destfd = table->fd[dest];
if(destfd){
spinlock_acquire(&destfd->lock);
if(--destfd->refcount == 0){
if(__atomic_sub_fetch(&destfd->refcount, 1, __ATOMIC_RELAXED) == 0){
if(destfd->node) vfs_close(destfd->node);
free(destfd);
}
@@ -244,12 +263,14 @@ int fd_duplicate(fdtable_t* table, int src, int dest, int type, int* ret){
// everything is set now
table->fd[dest] = srcfd;
srcfd->refcount++;
__atomic_add_fetch(&srcfd->refcount, 1, __ATOMIC_RELAXED);
*ret = dest;
fd_release(srcfd);
spinlock_release(&table->lock);
return 0;
}
+2
View File
@@ -6,6 +6,7 @@
#include <string.h>
#include <dirent.h>
fs_t kerneltmpfs;
static int tmpfs_mount(dirnode_t* mountpoint, vnode_t* device, int mountflags, void* fsinfo){
@@ -158,5 +159,6 @@ static fscalls_t funcs = {
fscalls_t* tmpfs_getfuncs(){
kerneltmpfs.calls = &funcs;
return &funcs;
}
+11
View File
@@ -6,6 +6,7 @@
#include <kernel/devman.h>
#include <arch/spinlock.h>
#include <dirent.h>
#include <kernel/pipe.h>
dirnode_t* vfsroot;
hashtable fsfuncs;
@@ -64,6 +65,10 @@ int vfs_write(int* error, vnode_t* node, void* buff, size_t count, size_t offset
int type = GETTYPE(node->st.st_mode);
if(type == TYPE_FIFO)
return pipe_write(node->objdata, buff, count, error);
if(type == TYPE_DIR || type == TYPE_LINK){
*error = EINVAL;
return -1;
@@ -108,6 +113,10 @@ int vfs_read(int* error, vnode_t* node, void* buff, size_t count, size_t offset)
int type = GETTYPE(node->st.st_mode);
if(type == TYPE_FIFO){
return pipe_read(node->objdata, buff, count, error);
}
if(type == TYPE_DIR || type == TYPE_LINK){
*error = EINVAL;
return -1;
@@ -135,6 +144,8 @@ int vfs_close(vnode_t* node){
vfs_releasenode(node);
int status = 0;
// TODO free pipe if one
if(node->refcount == 0)
status = node->fs->calls->close(node);