devfs: add vnodes to devfs tree
This commit is contained in:
parent
100d26f504
commit
c487c4c9dd
8 changed files with 152 additions and 26 deletions
|
|
@ -35,6 +35,12 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
|
|||
return bdev;
|
||||
}
|
||||
|
||||
struct block_device **block_get_all_devices(int *n)
|
||||
{
|
||||
*n = block_devices_count;
|
||||
return block_devices;
|
||||
}
|
||||
|
||||
struct block_device *block_get_device(const char *name)
|
||||
{
|
||||
for (size_t i = 0; i < block_devices_count; i++) {
|
||||
|
|
|
|||
78
fs/devfs.c
78
fs/devfs.c
|
|
@ -1,8 +1,11 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/block.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
struct fs *devfs;
|
||||
|
||||
static int devfs_read(struct vnode *vnode, void *buf, size_t offset, size_t size)
|
||||
{
|
||||
return 0;
|
||||
|
|
@ -35,28 +38,73 @@ static int devfs_open_dir(struct fs *fs, struct vnode *out)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
void devfs_init()
|
||||
int devfs_insert(struct vnode *dir, const char *name, uint32_t flags)
|
||||
{
|
||||
struct fs *fs = vfs_create();
|
||||
if (!fs)
|
||||
if (!dir || !name)
|
||||
return -EINVAL;
|
||||
|
||||
struct vnode *dev_vnode = vfs_create_vno();
|
||||
if (!dev_vnode)
|
||||
return -ENOMEM;
|
||||
|
||||
dev_vnode->fs = dir->fs;
|
||||
dev_vnode->flags = flags;
|
||||
dev_vnode->no_free = true;
|
||||
|
||||
spinlock_acquire(&dir->lock);
|
||||
|
||||
dir->num_dirents++;
|
||||
dir->dirents = krealloc(dir->dirents, dir->num_dirents * sizeof(struct dirent), ALLOC_KERN);
|
||||
|
||||
struct dirent *dirent = &dir->dirents[dir->num_dirents - 1];
|
||||
|
||||
memcpy(dirent, name, MIN(255, strlen(name) + 1));
|
||||
dirent->name[MIN(255, strlen(name))] = '\0';
|
||||
dirent->vnode = dev_vnode;
|
||||
dirent->reclen = sizeof(struct dirent);
|
||||
dirent->inode = 0;
|
||||
|
||||
spinlock_release(&dir->lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct fs *devfs_init()
|
||||
{
|
||||
devfs = vfs_create();
|
||||
if (!devfs)
|
||||
goto out;
|
||||
|
||||
struct fs_ops *ops = kmalloc(sizeof(struct fs_ops), ALLOC_KERN);
|
||||
if (!ops)
|
||||
goto out_ops;
|
||||
devfs->ops->read = devfs_read;
|
||||
devfs->ops->write = devfs_write;
|
||||
devfs->ops->unlink = devfs_unlink;
|
||||
devfs->ops->open_vno = devfs_open_vno;
|
||||
devfs->ops->readdir = devfs_readdir;
|
||||
devfs->ops->open_dir = devfs_open_dir;
|
||||
|
||||
ops->read = devfs_read;
|
||||
ops->write = devfs_write;
|
||||
ops->unlink = devfs_unlink;
|
||||
ops->open_vno = devfs_open_vno;
|
||||
ops->readdir = devfs_readdir;
|
||||
ops->open_dir = devfs_open_dir;
|
||||
int res = vfs_mount(devfs, "dev");
|
||||
|
||||
return;
|
||||
if(res < 0) {
|
||||
kprintf(LOG_ERROR "devfs: failed to mount devfs: %s\n", strerror(-res));
|
||||
goto out;
|
||||
}
|
||||
|
||||
out_ops:
|
||||
vfs_dealloc(fs);
|
||||
/* get all block devices */
|
||||
struct block_device **block_get_all_devices(int *n);
|
||||
|
||||
int n;
|
||||
struct block_device **bdevs = block_get_all_devices(&n);
|
||||
|
||||
if (n > 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
devfs_insert(devfs->root, bdevs[i]->name, VFS_VNO_BLKDEV);
|
||||
}
|
||||
}
|
||||
|
||||
return devfs;
|
||||
out:
|
||||
kprintf(LOG_ERROR "devfs: failed to mount devfs\n");
|
||||
panic();
|
||||
|
||||
return NULL; /* cannot possibly happen */
|
||||
}
|
||||
|
|
|
|||
84
fs/vfs.c
84
fs/vfs.c
|
|
@ -90,6 +90,12 @@ struct file *vfs_open(const char *pathname, int *err)
|
|||
if (entry->vnode) {
|
||||
/* go to next vnode */
|
||||
cur_vnode = entry->vnode;
|
||||
|
||||
if (cur_vnode->mount_ptr) {
|
||||
fs = cur_vnode->ptr->fs;
|
||||
cur_vnode = cur_vnode->ptr;
|
||||
}
|
||||
|
||||
spinlock_acquire(&cur_vnode->lock);
|
||||
cur_vnode->refcount++;
|
||||
spinlock_release(&cur_vnode->lock);
|
||||
|
|
@ -318,6 +324,9 @@ struct fs *vfs_create()
|
|||
if (!fs->ops)
|
||||
goto out_ops;
|
||||
|
||||
fs->root->no_free = true;
|
||||
fs->root->refcount = 1;
|
||||
|
||||
return fs;
|
||||
|
||||
out_ops:
|
||||
|
|
@ -338,7 +347,60 @@ void vfs_dealloc(struct fs *fs)
|
|||
ATTEMPT_FREE(fs);
|
||||
}
|
||||
|
||||
struct fs *vfs_mount(const char *dev, const char *mount_point)
|
||||
void vfs_no_free_r(struct vnode *vnode)
|
||||
{
|
||||
if (!vnode)
|
||||
return;
|
||||
|
||||
while (vnode && !vnode->no_free) {
|
||||
vnode->no_free = true;
|
||||
vnode = vnode->parent;
|
||||
}
|
||||
}
|
||||
|
||||
int vfs_mount(struct fs *fs, const char *mount_point)
|
||||
{
|
||||
struct vnode *vnode = fs->root;
|
||||
|
||||
/* find vnode of mount_point */
|
||||
int err;
|
||||
struct file *file = vfs_open(mount_point, &err);
|
||||
if (!file)
|
||||
return err;
|
||||
|
||||
struct vnode *mp_vnode = file->vnode;
|
||||
|
||||
/* mount point is a directory */
|
||||
if ((mp_vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
|
||||
vfs_close(file);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
||||
/* mount point is empty */
|
||||
for (int i = 0; i < mp_vnode->num_dirents; i++) {
|
||||
struct dirent *dirent = &mp_vnode->dirents[i];
|
||||
if (strcmp(".", dirent->name) == 0 || strcmp("..", dirent->name) == 0)
|
||||
continue;
|
||||
kprintf("dirent: %s\n", dirent->name);
|
||||
}
|
||||
|
||||
/* mount */
|
||||
spinlock_acquire(&mp_vnode->lock);
|
||||
spinlock_acquire(&vnode->lock);
|
||||
|
||||
mp_vnode->ptr = vnode;
|
||||
mp_vnode->mount_ptr = true;
|
||||
|
||||
vnode->parent = mp_vnode;
|
||||
vfs_no_free_r(vnode);
|
||||
|
||||
spinlock_release(&vnode->lock);
|
||||
spinlock_release(&mp_vnode->lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct fs *vfs_mount_root(const char *dev, const char *mount_point)
|
||||
{
|
||||
/* TODO: detect filesystem type */
|
||||
/* for now, assume ext2 */
|
||||
|
|
@ -356,24 +418,26 @@ struct fs *vfs_mount(const char *dev, const char *mount_point)
|
|||
fs->type = FS_TYPE_EXT2;
|
||||
|
||||
fs->mount_point = kmalloc(strlen(mount_point) + 1, ALLOC_KERN);
|
||||
if (!fs->mount_point)
|
||||
goto out_dealloc_fs;
|
||||
|
||||
strcpy(fs->mount_point, mount_point);
|
||||
|
||||
struct vnode *root = fs->root;
|
||||
struct dirent *dirents = NULL;
|
||||
int num_dirents = fs->ops->readdir(root, &dirents);
|
||||
if (num_dirents < 0) {
|
||||
kprintf(LOG_ERROR "Failed to read root directory\n");
|
||||
vfs_dealloc(fs);
|
||||
fs = NULL;
|
||||
goto out;
|
||||
}
|
||||
if (num_dirents < 0)
|
||||
goto out_dealloc_fs;
|
||||
|
||||
root->dirents = dirents;
|
||||
root->num_dirents = num_dirents;
|
||||
root->no_free = true;
|
||||
|
||||
out:
|
||||
return fs;
|
||||
|
||||
out_dealloc_fs:
|
||||
vfs_dealloc(fs);
|
||||
out:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void vfs_init(const char *rootdev_name)
|
||||
|
|
@ -385,7 +449,7 @@ void vfs_init(const char *rootdev_name)
|
|||
kprintf(LOG_DEBUG "Found root device %s\n", rootdev_name);
|
||||
#endif
|
||||
|
||||
struct fs *fs = vfs_mount(rootdev_name, "/");
|
||||
struct fs *fs = vfs_mount_root(rootdev_name, "/");
|
||||
if (!fs) {
|
||||
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootdev_name);
|
||||
kerror_print_blkdevs();
|
||||
|
|
|
|||
|
|
@ -2,4 +2,6 @@
|
|||
#ifndef _DEVFS_H_
|
||||
#define _DEVFS_H_
|
||||
|
||||
struct fs *devfs_init();
|
||||
|
||||
#endif /* _DEVFS_H_ */
|
||||
|
|
|
|||
|
|
@ -73,6 +73,8 @@ struct vnode {
|
|||
bool mount_ptr; /* does this node point to a root of a mounted fs? */
|
||||
bool no_free; /* permanently cached vnodes */
|
||||
|
||||
void *priv_data;
|
||||
|
||||
size_t refcount;
|
||||
spinlock_t lock;
|
||||
};
|
||||
|
|
@ -111,8 +113,6 @@ struct file_descriptor {
|
|||
|
||||
char *buf;
|
||||
size_t buf_len;
|
||||
int (*buf_write)(struct file_descriptor *fdesc, void *buf, size_t count);
|
||||
int (*buf_read)(struct file_descriptor *fdesc, void *buf, size_t count);
|
||||
|
||||
struct fs *fs;
|
||||
};
|
||||
|
|
@ -158,6 +158,7 @@ char *dirname(char *path);
|
|||
struct fs *vfs_create();
|
||||
struct vnode *vfs_create_vno();
|
||||
void vfs_dealloc(struct fs *fs);
|
||||
int vfs_mount(struct fs *fs, const char *mount_point);
|
||||
|
||||
int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#define ENOEXEC 9
|
||||
#define EFAULT 10
|
||||
#define EAGAIN 11
|
||||
#define EBUSY 12
|
||||
|
||||
const char *strerror(int errnum);
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
#include <lib/stack.h>
|
||||
|
||||
#include <fs/vfs.h>
|
||||
#include <fs/devfs.h>
|
||||
|
||||
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
|
||||
#include <limine/limine.h>
|
||||
|
|
@ -180,6 +181,8 @@ void kmain()
|
|||
vfs_init(root_path);
|
||||
}
|
||||
|
||||
devfs_init();
|
||||
|
||||
_pic_init();
|
||||
apic_init();
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ static const char *errno_str[] = {
|
|||
[ENOEXEC] = "Exec format error",
|
||||
[EFAULT] = "Bad address",
|
||||
[EAGAIN] = "Try again",
|
||||
[EBUSY] = "Device or resource busy",
|
||||
};
|
||||
|
||||
inline const char *strerror(int errnum)
|
||||
|
|
|
|||
Loading…
Reference in a new issue