From 31d654e6503229c52172d72eaa018e1f274b6d48 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 6 May 2022 15:00:25 +0200 Subject: kernel: remove the union in `struct handle` --- src/kernel/handle.c | 23 +++++++++-------------- src/kernel/handle.h | 12 ++---------- src/kernel/syscalls.c | 12 ++++++------ src/kernel/vfs/request.c | 4 ++-- 4 files changed, 19 insertions(+), 32 deletions(-) (limited to 'src/kernel') diff --git a/src/kernel/handle.c b/src/kernel/handle.c index 103c788..ac8db1f 100644 --- a/src/kernel/handle.c +++ b/src/kernel/handle.c @@ -16,22 +16,17 @@ void handle_close(struct handle *h) { assert(h->refcount > 0); if (--(h->refcount) > 0) return; - switch (h->type) { - case HANDLE_FILE: - vfsreq_create((struct vfs_request) { - .type = VFSOP_CLOSE, - .id = h->file.id, - .caller = NULL, - .backend = h->file.backend, - }); - vfs_backend_refdown(h->file.backend); - break; - case HANDLE_FS_FRONT: - vfs_backend_refdown(h->fs.backend); - break; - case HANDLE_INVALID: panic_invalid_state(); + if (h->type == HANDLE_FILE) { + vfsreq_create((struct vfs_request) { + .type = VFSOP_CLOSE, + .id = h->file_id, + .caller = NULL, + .backend = h->backend, + }); } + vfs_backend_refdown(h->backend); + // TODO sanity check to check if refcount is true. handle_sanity? // TODO tests which would catch premature frees diff --git a/src/kernel/handle.h b/src/kernel/handle.h index c7a0728..088ff93 100644 --- a/src/kernel/handle.h +++ b/src/kernel/handle.h @@ -15,16 +15,8 @@ enum handle_type { struct handle { enum handle_type type; - union { - // TODO consolidate backend fields - struct { - struct vfs_backend *backend; - int id; - } file; - struct { - struct vfs_backend *backend; - } fs; - }; + struct vfs_backend *backend; + int file_id; // only applicable to HANDLE_FILE size_t refcount; }; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 59358a8..21b9cf2 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -66,7 +66,7 @@ int _syscall_fork(int flags, handle_t __user *fs_front) { child->controlled = backend; - process_current->handles[front]->fs.backend = backend; + process_current->handles[front]->backend = backend; if (fs_front) { /* failure ignored. if you pass an invalid pointer to this function, @@ -144,7 +144,7 @@ int _syscall_mount(handle_t hid, const char __user *path, int len) { if (hid >= 0) { // mounting a real backend? struct handle *handle = process_handle_get(process_current, hid, HANDLE_FS_FRONT); if (!handle) goto fail; - backend = handle->fs.backend; + backend = handle->backend; backend->refcount++; } // otherwise backend == NULL @@ -178,10 +178,10 @@ int _syscall_read(handle_t handle_num, void __user *buf, size_t len, int offset) .buf = (userptr_t) buf, .len = len, }, - .id = handle->file.id, + .id = handle->file_id, .offset = offset, .caller = process_current, - .backend = handle->file.backend, + .backend = handle->backend, }); return -1; // dummy } @@ -195,10 +195,10 @@ int _syscall_write(handle_t handle_num, const void __user *buf, size_t len, int .buf = (userptr_t) buf, .len = len, }, - .id = handle->file.id, + .id = handle->file_id, .offset = offset, .caller = process_current, - .backend = handle->file.backend, + .backend = handle->backend, }); return -1; // dummy } diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index d6f348a..30c0e77 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -40,9 +40,9 @@ void vfsreq_finish(struct vfs_request *req, int ret) { panic_invalid_state(); // we check for free handles before the open() call struct handle *backing = handle_init(HANDLE_FILE); - backing->file.backend = req->backend; + backing->backend = req->backend; req->backend->refcount++; - backing->file.id = ret; + backing->file_id = ret; req->caller->handles[handle] = backing; ret = handle; } else { -- cgit v1.2.3