From d516e8a27f1a52c92d39aada03ff92985b1fdf7e Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 28 Jun 2024 23:19:09 -0400 Subject: kernel: vfs: Add vnode refcount Signed-off-by: Ian Moffett --- sys/kern/vfs_subr.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'sys/kern') diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index b732cb4..0d4be72 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -46,6 +47,7 @@ vfs_alloc_vnode(struct vnode **res, int type) memset(vp, 0, sizeof(*vp)); vp->type = type; + vp->refcount = 1; *res = vp; return 0; } @@ -83,6 +85,23 @@ vfs_release_vnode(struct vnode *vp) if (vp == NULL) return -EINVAL; + + /* + * The refcount cannot be zero before we decrement it, + * something is quite wrong if this happens. + */ + if (vp->refcount == 0) { + kprintf("Cannot release vnode, bad refcount\n"); + return -EIO; + } + + /* + * Drop the reference and don't destroy the vnode + * if it's still not zero. + */ + if (atomic_dec_int(&vp->refcount) > 0) + return 0; + if (vops->reclaim != NULL) status = vops->reclaim(vp); if (status != 0) -- cgit v1.2.3