From 380da5fe2e8ef39cfca6e92ef53c1b3f44ab77c6 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 27 Mar 2024 19:27:12 -0400 Subject: kernel: vfs: Add getattr vop Signed-off-by: Ian Moffett --- sys/fs/initramfs.c | 25 ++++++++++++++++++++++++- sys/include/sys/vfs.h | 1 + sys/include/sys/vnode.h | 7 +++++++ sys/kern/vfs_subr.c | 6 ++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/sys/fs/initramfs.c b/sys/fs/initramfs.c index 00d1020..b7f0c71 100644 --- a/sys/fs/initramfs.c +++ b/sys/fs/initramfs.c @@ -146,6 +146,28 @@ vop_read(struct vnode *vp, struct sio_txn *sio) return sio->len; } +static int +vop_getattr(struct vnode *vp, struct vattr *vattr) +{ + struct tar_hdr *hdr = vp->data; + + if (hdr == NULL) { + return -EIO; + } + + switch (hdr->type) { + case TAR_TYPEFLAG_NORMAL: + vattr->type = VREG; + break; + case TAR_TYPEFLAG_DIR: + vattr->type = VDIR; + break; + } + + vattr->size = getsize(hdr->size); + return 0; +} + static char * get_module(const char *path, uint64_t *size) { for (uint64_t i = 0; i < mod_req.response->module_count; ++i) { @@ -232,5 +254,6 @@ struct vfsops g_initramfs_ops = { struct vops g_initramfs_vops = { .vget = vop_vget, - .read = vop_read + .read = vop_read, + .getattr = vop_getattr }; diff --git a/sys/include/sys/vfs.h b/sys/include/sys/vfs.h index b454d39..d79409e 100644 --- a/sys/include/sys/vfs.h +++ b/sys/include/sys/vfs.h @@ -52,6 +52,7 @@ int vfs_rootname(const char *path, char **new_path); bool vfs_is_valid_path(const char *path); ssize_t vfs_hash_path(const char *path); ssize_t vfs_read(struct vnode *vp, struct sio_txn *sio); +int vfs_getattr(struct vnode *vp, struct vattr *vattr); #endif /* defined(_KERNEL) */ diff --git a/sys/include/sys/vnode.h b/sys/include/sys/vnode.h index 0352312..ac66044 100644 --- a/sys/include/sys/vnode.h +++ b/sys/include/sys/vnode.h @@ -36,11 +36,18 @@ #include struct vnode; +struct vattr; struct vops { int(*vget)(struct vnode *parent, const char *name, struct vnode **vp); int(*read)(struct vnode *vp, struct sio_txn *sio); int(*write)(struct vnode *vp, struct sio_txn *sio); + int(*getattr)(struct vnode *vp, struct vattr *vattr); +}; + +struct vattr { + size_t size; /* File size in bytes */ + int type; /* Vnode type */ }; struct vnode { diff --git a/sys/kern/vfs_subr.c b/sys/kern/vfs_subr.c index bbaec12..cd16704 100644 --- a/sys/kern/vfs_subr.c +++ b/sys/kern/vfs_subr.c @@ -229,3 +229,9 @@ vfs_read(struct vnode *vp, struct sio_txn *sio) { return vp->vops->read(vp, sio); } + +int +vfs_getattr(struct vnode *vp, struct vattr *vattr) +{ + return vp->vops->getattr(vp, vattr); +} -- cgit v1.2.3