devfs: alloc devfs_dev_info struct for each dev

This commit is contained in:
Michael Shaw 2024-02-12 23:48:58 -05:00
commit 6d67d7fbfe
3 changed files with 22 additions and 19 deletions

View file

@ -6,16 +6,12 @@
#include <fs/devfs.h>
static struct fs *devfs;
static struct devfs_dev_ops block_dev_ops = {
.read = (devfs_dev_ops_read)block_read,
.write = (devfs_dev_ops_write)block_write,
};
static int devfs_read(struct vnode *vnode, void *buf, size_t offset, size_t size)
{
if (!vnode->priv_data)
return -EINVAL;
struct devfs_dev_ops *ops = vnode->priv_data;
struct devfs_dev_info *ops = vnode->priv_data;
return ops->read(vnode->priv_data, buf, offset, size);
}
@ -23,7 +19,7 @@ static int devfs_write(struct vnode *vnode, void *buf, size_t offset, size_t siz
{
if (!vnode->priv_data)
return -EINVAL;
struct devfs_dev_ops *ops = vnode->priv_data;
struct devfs_dev_info *ops = vnode->priv_data;
return ops->write(vnode->priv_data, buf, offset, size);
}
@ -49,7 +45,7 @@ static int devfs_open_dir(struct fs *fs, struct vnode *out)
return -EINVAL;
}
int devfs_insert(struct vnode *dir, const char *name, uint32_t flags, struct devfs_dev_ops *ops)
int devfs_insert(struct vnode *dir, const char *name, uint32_t flags, struct devfs_dev_info *ops)
{
if (!name || !ops)
return -EINVAL;
@ -114,7 +110,15 @@ struct fs *devfs_init()
if (n > 0) {
for (int i = 0; i < n; i++) {
devfs_insert(devfs->root, bdevs[i]->name, VFS_VNO_BLKDEV, &block_dev_ops);
struct devfs_dev_info *block_dev_info = kmalloc(sizeof(struct devfs_dev_info), ALLOC_KERN);
if (!block_dev_info)
goto out;
block_dev_info->dev = &bdevs[i];
block_dev_info->read = (void *)block_read;
block_dev_info->write = (void *)block_write;
devfs_insert(devfs->root, bdevs[i]->name, VFS_VNO_BLKDEV, block_dev_info);
}
}