From eb2c3f0f8b2e50adc3f5e008d72c32a8a3059569 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 7 Oct 2025 12:38:22 -0400 Subject: os: vnode: Add refcounts to vnodes Keep track of how many referencing are on a vnode so one isn't freed early while another object is still using it. Signed-off-by: Ian Moffett --- src/sys/include/os/vnode.h | 5 +++++ src/sys/os/vfs_subr.c | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/sys/include/os/vnode.h b/src/sys/include/os/vnode.h index ed98206..354c73c 100644 --- a/src/sys/include/os/vnode.h +++ b/src/sys/include/os/vnode.h @@ -31,6 +31,7 @@ #define _OS_VNODE_H_ 1 #include +#include /* Forward declarations */ struct vnode; @@ -82,16 +83,20 @@ struct vop { * [V] Set up by VFS * [F/V]: Both F and V * + * @refcount: How many objects have a reference * @type: Vnode type [F/V] * @vops: Vnode operations hooks [F] * @data: Filesystem specific data [F] */ struct vnode { + int refcount; vtype_t type; struct vop *vops; void *data; }; +#define vnode_ref(VP) (atomic_inc_int(&(VP)->refcount)) + /* * Allocate a new vnode * diff --git a/src/sys/os/vfs_subr.c b/src/sys/os/vfs_subr.c index 6cb542c..64c7492 100644 --- a/src/sys/os/vfs_subr.c +++ b/src/sys/os/vfs_subr.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -77,6 +78,7 @@ vfs_valloc(struct vnode **resp, vtype_t type, int flags) } memset(vp, 0, sizeof(*vp)); + vp->refcount = 1; vp->type = type; *resp = vp; return 0; @@ -89,6 +91,9 @@ vfs_vrel(struct vnode *vp, int flags) return -EINVAL; } + if (atomic_dec_int(&vp->refcount) > 1) { + return 0; + } kfree(vp); return 0; } -- cgit v1.2.3