forked from cahirwpz/mimiker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic file descriptor support (cahirwpz#152)
- Loading branch information
1 parent
63e6fea
commit 42fb0f0
Showing
23 changed files
with
669 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#ifndef _SYS_FILE_H_ | ||
#define _SYS_FILE_H_ | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <mutex.h> | ||
#include <uio.h> | ||
|
||
typedef struct thread thread_t; | ||
typedef struct file file_t; | ||
typedef struct vnode vnode_t; | ||
|
||
typedef int fo_read_t(file_t *f, thread_t *td, uio_t *uio); | ||
typedef int fo_write_t(file_t *f, thread_t *td, uio_t *uio); | ||
typedef int fo_close_t(file_t *f, thread_t *td); | ||
|
||
typedef struct { | ||
fo_read_t *fo_read; | ||
fo_write_t *fo_write; | ||
fo_close_t *fo_close; | ||
} fileops_t; | ||
|
||
typedef enum { | ||
FT_VNODE = 1, /* regular file */ | ||
FT_PIPE = 2, /* pipe */ | ||
} filetype_t; | ||
|
||
#define FF_READ 0x0001 | ||
#define FF_WRITE 0x0002 | ||
#define FF_APPEND 0x0004 | ||
|
||
/* File open flags as passed to sys_open. These need match what newlib provides | ||
to user programs. */ | ||
#define O_RDONLY 0 | ||
#define O_WRONLY 1 | ||
#define O_RDWR 2 | ||
|
||
typedef struct file { | ||
void *f_data; /* File specific data */ | ||
fileops_t *f_ops; | ||
filetype_t f_type; /* File type */ | ||
vnode_t *f_vnode; | ||
int f_count; /* Reference count, ready for disposal if -1 */ | ||
unsigned f_flags; /* File flags FF_* */ | ||
mtx_t f_mtx; | ||
} file_t; | ||
|
||
void file_init(); | ||
|
||
void file_ref(file_t *f); | ||
void file_unref(file_t *f); | ||
|
||
file_t *file_alloc(); | ||
void file_destroy(file_t *f); | ||
/* Drop reference counter and possibly destroy the file. */ | ||
void file_release(file_t *f); | ||
|
||
static inline int FOP_READ(file_t *f, thread_t *td, uio_t *uio) { | ||
return f->f_ops->fo_read(f, td, uio); | ||
} | ||
|
||
static inline int FOP_WRITE(file_t *f, thread_t *td, uio_t *uio) { | ||
return f->f_ops->fo_write(f, td, uio); | ||
} | ||
|
||
static inline int FOP_CLOSE(file_t *f, thread_t *td) { | ||
return f->f_ops->fo_close(f, td); | ||
} | ||
|
||
extern fileops_t badfileops; | ||
|
||
#endif /* !_SYS_FILE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef _SYS_FILEDESC_H_ | ||
#define _SYS_FILEDESC_H_ | ||
|
||
#include <file.h> | ||
#include <bitstring.h> | ||
#include <mutex.h> | ||
|
||
/* The initial size of space allocated for file descriptors. According | ||
to FreeBSD, this is more than enough for most applications. Each | ||
process starts with this many descriptors, and more are allocated | ||
on demand. */ | ||
#define NDFILE 20 | ||
/* Separate macro defining a hard limit on open files. */ | ||
#define MAXFILES 20 | ||
|
||
typedef struct fdtab { | ||
file_t *fdt_files[NDFILE]; /* Open files array */ | ||
bitstr_t fdt_map[bitstr_size(NDFILE)]; /* Bitmap of used fds */ | ||
unsigned fdt_flags; | ||
unsigned fdt_nfiles; /* Number of files allocated */ | ||
int fdt_count; /* Reference count, ready for disposal if -1 */ | ||
mtx_t fdt_mtx; | ||
} fdtab_t; | ||
|
||
/* Prepares memory pools for file descriptors and tables. */ | ||
void fd_init(); | ||
|
||
void fdtab_ref(fdtab_t *fdt); | ||
void fdtab_unref(fdtab_t *fdt); | ||
|
||
/* Allocates a new descriptor table. */ | ||
fdtab_t *fdtab_alloc(); | ||
/* Allocates a new descriptor table making it a copy of an existing one. */ | ||
fdtab_t *fdtab_copy(fdtab_t *fdt); | ||
/* Frees the table and possibly closes underlying files. */ | ||
void fdtab_destroy(fdtab_t *fdt); | ||
/* Drop reference counter and possibly destroy the table. */ | ||
void fdtab_release(fdtab_t *fdt); | ||
/* Assign a file structure to a new descriptor. */ | ||
int fdtab_install_file(fdtab_t *fdt, file_t *f, int *fdp); | ||
/* Extracts a reference to file from descriptor table for given number. */ | ||
int fdtab_get_file(fdtab_t *fdt, int fd, int flags, file_t **fp); | ||
/* Closes a file descriptor. | ||
* If it was the last reference to a file, the file is closed as well. */ | ||
int fdtab_close_fd(fdtab_t *fdt, int fd); | ||
|
||
#endif /* !_SYS_FILEDESC_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef _SYS_VFS_SYSCALLS_H_ | ||
#define _SYS_VFS_SYSCALLS_H_ | ||
|
||
#include <uio.h> | ||
|
||
typedef struct thread thread_t; | ||
typedef struct syscall_args syscall_args_t; | ||
|
||
/* Kernel interface */ | ||
int do_open(thread_t *td, char *pathname, int flags, int mode, int *fd); | ||
int do_close(thread_t *td, int fd); | ||
int do_read(thread_t *td, int fd, uio_t *uio); | ||
int do_write(thread_t *td, int fd, uio_t *uio); | ||
|
||
/* Syscall interface */ | ||
int sys_open(thread_t *td, syscall_args_t *args); | ||
int sys_close(thread_t *td, syscall_args_t *args); | ||
int sys_read(thread_t *td, syscall_args_t *args); | ||
int sys_write(thread_t *td, syscall_args_t *args); | ||
|
||
#endif /* !_SYS_VFS_SYSCALLS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <file.h> | ||
#include <malloc.h> | ||
#include <stdc.h> | ||
#include <errno.h> | ||
#include <mutex.h> | ||
|
||
static MALLOC_DEFINE(file_pool, "file pool"); | ||
|
||
void file_init() { | ||
kmalloc_init(file_pool); | ||
kmalloc_add_pages(file_pool, 2); | ||
} | ||
|
||
void file_ref(file_t *f) { | ||
mtx_lock(&f->f_mtx); | ||
assert(f->f_count >= 0); | ||
f->f_count++; | ||
mtx_unlock(&f->f_mtx); | ||
} | ||
|
||
void file_unref(file_t *f) { | ||
mtx_lock(&f->f_mtx); | ||
assert(f->f_count > 0); | ||
if (--f->f_count == 0) | ||
f->f_count = -1; | ||
mtx_unlock(&f->f_mtx); | ||
} | ||
|
||
file_t *file_alloc() { | ||
file_t *f = kmalloc(file_pool, sizeof(file_t), M_ZERO); | ||
f->f_ops = &badfileops; | ||
mtx_init(&f->f_mtx); | ||
return f; | ||
} | ||
|
||
/* May be called after the last reference to a file has been dropped. */ | ||
void file_destroy(file_t *f) { | ||
assert(f->f_count <= 0); | ||
|
||
/* Note: If the file failed to open, we shall not close it. In such case its | ||
fileops are set to badfileops. */ | ||
/* TODO: What if an error happens during close? */ | ||
if (f->f_ops != &badfileops) | ||
FOP_CLOSE(f, thread_self()); | ||
|
||
kfree(file_pool, f); | ||
} | ||
|
||
void file_release(file_t *f) { | ||
if (f) { | ||
file_unref(f); | ||
if (f->f_count < 0) | ||
file_destroy(f); | ||
} | ||
} | ||
|
||
/* Operations on invalid file descriptors */ | ||
static int badfo_read(file_t *f, struct thread *td, uio_t *uio) { | ||
return -EBADF; | ||
} | ||
|
||
static int badfo_write(file_t *f, struct thread *td, uio_t *uio) { | ||
return -EBADF; | ||
} | ||
|
||
static int badfo_close(file_t *f, struct thread *td) { | ||
return -EBADF; | ||
} | ||
|
||
fileops_t badfileops = { | ||
.fo_read = badfo_read, | ||
.fo_write = badfo_write, | ||
.fo_close = badfo_close, | ||
}; |
Oops, something went wrong.