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/request.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/kernel/vfs/request.c') 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); +} -- cgit v1.2.3