From fe5a7b301f4700c806570fbea0e564a180d8a5a9 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 21 Nov 2025 13:44:40 -0500 Subject: kern: vfs: Add vnode VOP wrappers Signed-off-by: Ian Moffett --- sys/inc/kern/vnode.h | 25 +++++++++++++++++++++++++ sys/kern/vfs_subr.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) (limited to 'sys') diff --git a/sys/inc/kern/vnode.h b/sys/inc/kern/vnode.h index b50042d..37b7127 100644 --- a/sys/inc/kern/vnode.h +++ b/sys/inc/kern/vnode.h @@ -77,6 +77,31 @@ struct vnode { void *data; }; +/* + * Read the contents of a file described by a vnode into + * a buffer + * + * @vp: Vnode to read + * @buf: Buffer to read file into + * @size: Number of bytes to read + * @off: Offset to read starting at + * + * Returns the number of bytes read + */ +ssize_t vnode_read(struct vnode *vp, void *buf, size_t size, off_t off); + +/* + * Write data into a file described by a vnode + * + * @vp: Vnode to write + * @buf: Buffer to write from + * @size: Length of data to write + * @off: Offset to write at + * + * Returns the number of bytes written + */ +ssize_t vnode_write(struct vnode *vp, const void *buf, size_t size, off_t off); + /* * Initialize a vnode by type * diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index 3fd8503..2c08f36 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -89,3 +89,53 @@ vnode_release(struct vnode *vp) kfree(vp); return 0; } + +ssize_t +vnode_read(struct vnode *vp, void *buf, size_t size, off_t off) +{ + struct vop_buf_args args; + struct vops *vops; + + if (vp == NULL || buf == NULL) { + return -EINVAL; + } + + if (size == 0) { + return -EINVAL; + } + + vops = &vp->vops; + if (vops->read == NULL) { + return -ENOTSUP; + } + + args.buffer = buf; + args.len = size; + args.offset = off; + return vops->read(&args); +} + +ssize_t +vnode_write(struct vnode *vp, const void *buf, size_t size, off_t off) +{ + struct vop_buf_args args; + struct vops *vops; + + if (vp == NULL || buf == NULL) { + return -EINVAL; + } + + if (size == 0) { + return -EINVAL; + } + + vops = &vp->vops; + if (vops->write == NULL) { + return -ENOTSUP; + } + + args.buffer = (void *)buf; + args.len = size; + args.offset = off; + return vops->write(&args); +} -- cgit v1.2.3