From 3bf07641ee5ba1c6ec56b81a7f34abe1267d3ac1 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 4 May 2022 13:49:38 +0200 Subject: kernel: refcount vfs_backend what a mess --- src/kernel/vfs/mount.c | 3 +++ src/kernel/vfs/request.c | 17 +++++++++++++++++ src/kernel/vfs/request.h | 10 ++++++++++ 3 files changed, 30 insertions(+) (limited to 'src/kernel/vfs') diff --git a/src/kernel/vfs/mount.c b/src/kernel/vfs/mount.c index 3fe192d..d13c3d6 100644 --- a/src/kernel/vfs/mount.c +++ b/src/kernel/vfs/mount.c @@ -8,6 +8,7 @@ struct vfs_mount *vfs_mount_seed(void) { struct vfs_backend *backend = kmalloc(sizeof *backend); backend->type = VFS_BACK_ROOT; backend->potential_handlers = 1; + backend->refcount = 1; *mount = (struct vfs_mount){ .prev = NULL, .prefix = NULL, @@ -45,6 +46,8 @@ void vfs_mount_remref(struct vfs_mount *mnt) { if (--(mnt->refs) > 0) return; struct vfs_mount *prev = mnt->prev; + if (mnt->backend) + vfs_backend_refdown(mnt->backend); kfree(mnt->prefix); kfree(mnt); diff --git a/src/kernel/vfs/request.c b/src/kernel/vfs/request.c index 1f3193f..edb1fce 100644 --- a/src/kernel/vfs/request.c +++ b/src/kernel/vfs/request.c @@ -11,6 +11,9 @@ int vfs_request_create(struct vfs_request req_) { memcpy(req, &req_, sizeof *req); /* freed in vfs_request_finish or vfs_request_cancel */ + if (req->backend) + req->backend->refcount++; + if (req->caller) { process_transition(req->caller, PS_WAITS4FS); req->caller->waits4fs.req = req; @@ -87,6 +90,7 @@ int vfs_request_finish(struct vfs_request *req, int ret) { struct handle *backing = handle_init(HANDLE_FILE); backing->file.backend = req->backend; + req->backend->refcount++; backing->file.id = ret; req->caller->handles[handle] = backing; ret = handle; @@ -101,11 +105,13 @@ int vfs_request_finish(struct vfs_request *req, int ret) { process_transition(req->caller, PS_RUNNING); } + vfs_backend_refdown(req->backend); kfree(req); return ret; } void vfs_request_cancel(struct vfs_request *req, int ret) { + // TODO merge with vfs_request_finish if (req->caller) { assert(req->caller->state == PS_WAITS4FS); @@ -118,5 +124,16 @@ void vfs_request_cancel(struct vfs_request *req, int ret) { regs_savereturn(&req->caller->regs, ret); process_transition(req->caller, PS_RUNNING); } + + vfs_backend_refdown(req->backend); kfree(req); } + +void vfs_backend_refdown(struct vfs_backend *b) { + assert(b); + assert(b->refcount > 0); + if (--(b->refcount) > 0) return; + + assert(!b->queue); + kfree(b); +} diff --git a/src/kernel/vfs/request.h b/src/kernel/vfs/request.h index 657ae85..69dad07 100644 --- a/src/kernel/vfs/request.h +++ b/src/kernel/vfs/request.h @@ -11,6 +11,14 @@ enum vfs_backend_type { // describes something which can act as an access function struct vfs_backend { + size_t refcount; + /* references: + * struct vfs_mount + * struct vfs_request + * struct process + * struct handle + */ + enum vfs_backend_type type; size_t potential_handlers; // 0 - orphaned @@ -52,3 +60,5 @@ int vfs_backend_accept(struct vfs_backend *); int vfs_request_finish(struct vfs_request *, int ret); void vfs_request_cancel(struct vfs_request *, int ret); + +void vfs_backend_refdown(struct vfs_backend *); -- cgit v1.2.3