diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/init/fs/misc.c | 36 | ||||
-rw-r--r-- | src/init/tests/main.c | 2 | ||||
-rw-r--r-- | src/kernel/handle.c | 29 | ||||
-rw-r--r-- | src/kernel/vfs/request.c | 43 | ||||
-rw-r--r-- | src/kernel/vfs/root.c | 3 | ||||
-rw-r--r-- | src/shared/types.h | 1 |
6 files changed, 88 insertions, 26 deletions
diff --git a/src/init/fs/misc.c b/src/init/fs/misc.c index 64e3664..53373f4 100644 --- a/src/init/fs/misc.c +++ b/src/init/fs/misc.c @@ -3,6 +3,7 @@ #include <shared/flags.h> #include <shared/mem.h> #include <shared/syscalls.h> +#include <stdbool.h> bool fork2_n_mount(const char *path) { handle_t h = _syscall_fs_fork2(); @@ -70,6 +71,12 @@ static void fs_respond_delegate(struct fs_wait_response *res, handle_t delegate, _syscall_fs_respond(NULL, ret); break; + case VFSOP_CLOSE: + _syscall_close(delegate); + _syscall_fs_respond(NULL, 0); + // isn't it kinda weird that i even have to respond to close()s? + // i suppose it makes the API more consistent + default: /* unsupported / unexpected */ _syscall_fs_respond(NULL, -1); @@ -113,34 +120,49 @@ void fs_passthru(const char *prefix) { void fs_dir_inject(const char *path) { struct fs_dir_handle { + bool taken; int delegate; const char *inject; }; const size_t path_len = strlen(path); struct fs_wait_response res; - struct fs_dir_handle handles[16]; // TODO hardcoded FD_MAX - use malloc instead - int handle_next = 0; + struct fs_dir_handle handles[16] = {0}; // TODO hardcoded FD_MAX - use malloc instead static char buf[1024]; int ret; while (!_syscall_fs_wait(buf, sizeof buf, &res)) { switch (res.op) { case VFSOP_OPEN: - if (handle_next > 15) _syscall_fs_respond(NULL, -2); // we ran out of handles, which is entirely our fault. + int hid = -1; + for (int hid2 = 0; hid2 * sizeof(*handles) < sizeof(handles); hid2++) { + if (!handles[hid2].taken) { + hid = hid2; + break; + } + } + if (hid < 0) _syscall_fs_respond(NULL, -2); // we ran out of handles ret = _syscall_open(buf, res.len); /* errors handled in inject handler */ - handles[handle_next].delegate = ret; - handles[handle_next].inject = NULL; + handles[hid].delegate = ret; + handles[hid].inject = NULL; + handles[hid].taken = true; if (buf[res.len - 1] == '/' && res.len < path_len && !memcmp(path, buf, res.len)) { - handles[handle_next].inject = path + res.len; + handles[hid].inject = path + res.len; } else { /* not injecting, don't allow opening nonexistent stuff */ if (ret < 0) _syscall_fs_respond(NULL, ret); } - _syscall_fs_respond(NULL, handle_next++); + _syscall_fs_respond(NULL, hid); + break; + + case VFSOP_CLOSE: + if (handles[res.id].delegate >= 0) + _syscall_close(handles[res.id].delegate); + handles[res.id].taken = false; + _syscall_fs_respond(NULL, 0); break; case VFSOP_READ: diff --git a/src/init/tests/main.c b/src/init/tests/main.c index ed50229..1c1e817 100644 --- a/src/init/tests/main.c +++ b/src/init/tests/main.c @@ -105,5 +105,5 @@ void test_all(void) { run_forked(test_faults); run_forked(test_interrupted_fs); run_forked(test_orphaned_fs); - run_forked(stress_fork); +// run_forked(stress_fork); } diff --git a/src/kernel/handle.c b/src/kernel/handle.c index d7f19b3..91832a0 100644 --- a/src/kernel/handle.c +++ b/src/kernel/handle.c @@ -2,6 +2,7 @@ #include <kernel/mem/alloc.h> #include <kernel/panic.h> #include <kernel/proc.h> +#include <kernel/vfs/request.h> struct handle *handle_init(enum handle_type type) { struct handle *h = kmalloc(sizeof *h); @@ -13,15 +14,25 @@ struct handle *handle_init(enum handle_type type) { void handle_close(struct handle *h) { if (!h) return; assert(h->refcount > 0); - if (--(h->refcount) == 0) { - // TODO call close() in handler - // TODO count allocations and frees + if (--(h->refcount) > 0) return; - // TODO sanity check to check if refcount is true. handle_sanity? - - // TODO tests which would catch premature frees - // by that i mean duplicating a handle and killing the original process - h->type = HANDLE_INVALID; - kfree(h); + if (h->type == HANDLE_FILE) { + vfs_request_create((struct vfs_request) { + .type = VFSOP_CLOSE, + .id = h->file.id, + .caller = NULL, + .backend = h->file.backend, + }); } + + // TODO handle close(HANDLE_FS_FRONT) + + // TODO count allocations and frees + + // TODO sanity check to check if refcount is true. handle_sanity? + + // TODO tests which would catch premature frees + // by that i mean duplicating a handle and killing the original process + h->type = HANDLE_INVALID; + kfree(h); } diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 5ee1678..99d2e2c 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -4,14 +4,26 @@ #include <kernel/proc.h> #include <kernel/vfs/request.h> #include <kernel/vfs/root.h> +#include <shared/mem.h> int vfs_request_create(struct vfs_request req_) { struct vfs_request *req; - process_transition(process_current, PS_WAITS4FS); - // the request is owned by the caller - process_current->waits4fs.req = req_; - req = &process_current->waits4fs.req; + if (req_.caller) { + /* if the request has an explicit caller (isn't a close() call) + * it's owned by said caller + * + * it would be simpler if all requests were owned by the backend. + * i might end up changing that eventually. TODO? */ + process_transition(req_.caller, PS_WAITS4FS); + req_.caller->waits4fs.req = req_; + req = &req_.caller->waits4fs.req; + } else { + /* requests without explicit callers (close() calls) are owned by the + * backend, and freed in vfs_request_finish or vfs_request_cancel */ + req = kmalloc(sizeof *req); + memcpy(req, &req_, sizeof *req); + } if (!req->backend || !req->backend->potential_handlers) return vfs_request_finish(req, -1); @@ -37,7 +49,7 @@ int vfs_backend_accept(struct vfs_backend *backend) { struct vfs_request *req = backend->queue; struct process *handler = backend->handler; struct fs_wait_response res = {0}; - int len; + int len = 0; if (!handler) return -1; assert(handler->state == PS_WAITS4REQUEST); @@ -46,10 +58,12 @@ int vfs_backend_accept(struct vfs_backend *backend) { if (!req) return -1; backend->queue = req->queue_next; - len = min(req->input.len, handler->awaited_req.max_len); - if (!virt_cpy(handler->pages, handler->awaited_req.buf, - req->input.kern ? NULL : req->caller->pages, req->input.buf, len)) - goto fail; // can't copy buffer + if (req->input.buf) { + len = min(req->input.len, handler->awaited_req.max_len); + if (!virt_cpy(handler->pages, handler->awaited_req.buf, + req->input.kern ? NULL : req->caller->pages, req->input.buf, len)) + goto fail; // can't copy buffer + } res.len = len; res.capacity = req->output.len; @@ -75,6 +89,7 @@ int vfs_request_finish(struct vfs_request *req, int ret) { // open() calls need special handling // we need to wrap the id returned by the VFS in a handle passed to // the client + assert(req->caller); handle_t handle = process_find_handle(req->caller, 0); if (handle < 0) panic_invalid_state(); // we check for free handles before the open() call @@ -89,6 +104,11 @@ int vfs_request_finish(struct vfs_request *req, int ret) { if (req->input.kern) kfree(req->input.buf_kern); + if (!req->caller) { + kfree(req); // ok, this stinks. see comment at top of file + return 0; + } + assert(req->caller->state == PS_WAITS4FS || req->caller->state == PS_WAITS4IRQ); regs_savereturn(&req->caller->regs, ret); process_transition(req->caller, PS_RUNNING); @@ -96,6 +116,11 @@ int vfs_request_finish(struct vfs_request *req, int ret) { } void vfs_request_cancel(struct vfs_request *req, int ret) { + if (!req->caller) { + kfree(req); + return; + } + assert(req->caller->state == PS_WAITS4FS); if (req->input.kern) diff --git a/src/kernel/vfs/root.c b/src/kernel/vfs/root.c index 11d5f64..36e9da1 100644 --- a/src/kernel/vfs/root.c +++ b/src/kernel/vfs/root.c @@ -166,6 +166,9 @@ static int handle(struct vfs_request *req, bool *ready) { default: return -1; } + case VFSOP_CLOSE: + return 0; + default: panic_invalid_state(); } } diff --git a/src/shared/types.h b/src/shared/types.h index b696e85..f47e1bb 100644 --- a/src/shared/types.h +++ b/src/shared/types.h @@ -16,4 +16,5 @@ enum vfs_operation { VFSOP_OPEN, VFSOP_READ, VFSOP_WRITE, + VFSOP_CLOSE, }; |