vfs: add open and close functions
This commit is contained in:
parent
9476b3f810
commit
f2dd8ae8a7
4 changed files with 78 additions and 102 deletions
82
fs/vfs.c
82
fs/vfs.c
|
|
@ -8,7 +8,9 @@
|
|||
#include <fs/vfs.h>
|
||||
|
||||
static struct rbtree *kfd;
|
||||
static slab_t *fd_slab;
|
||||
static struct fs *rootfs;
|
||||
static int fd_counter = 0;
|
||||
|
||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||
|
||||
|
|
@ -21,24 +23,27 @@ void vfs_init(const char *rootdev_name)
|
|||
if (!rootdev) {
|
||||
kprintf(LOG_ERROR "Failed to find root device %s\n", rootdev_name);
|
||||
return;
|
||||
} else {
|
||||
kprintf(LOG_DEBUG "Found root device %s\n", rootdev_name);
|
||||
|
||||
/* TODO: detect filesystem type */
|
||||
/* for now, assume ext2 */
|
||||
struct fs *fs = ext2_init_fs(rootdev);
|
||||
if (!fs) {
|
||||
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootdev_name);
|
||||
return;
|
||||
} else {
|
||||
kprintf(LOG_DEBUG "Mounted root device %s\n", rootdev_name);
|
||||
}
|
||||
|
||||
fs->mount_point = kzalloc(2, ALLOC_KERN);
|
||||
fs->mount_point[0] = '/';
|
||||
|
||||
rootfs = fs;
|
||||
}
|
||||
|
||||
kprintf(LOG_DEBUG "Found root device %s\n", rootdev_name);
|
||||
|
||||
/* TODO: detect filesystem type */
|
||||
/* for now, assume ext2 */
|
||||
struct fs *fs = ext2_init_fs(rootdev);
|
||||
if (!fs) {
|
||||
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootdev_name);
|
||||
return;
|
||||
} else {
|
||||
kprintf(LOG_DEBUG "Mounted root device %s\n", rootdev_name);
|
||||
}
|
||||
|
||||
fs->mount_point = kzalloc(2, ALLOC_KERN);
|
||||
fs->mount_point[0] = '/';
|
||||
|
||||
rootfs = fs;
|
||||
|
||||
/* init file descriptor slab */
|
||||
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
|
||||
}
|
||||
|
||||
int write(int fd, const void *buf, size_t count)
|
||||
|
|
@ -53,11 +58,50 @@ int read(int fd, void *buf, size_t count)
|
|||
|
||||
int open(const char *pathname, int flags)
|
||||
{
|
||||
char *strtok_last = NULL;
|
||||
return 0;
|
||||
/* allocate a file descriptor */
|
||||
struct file_descriptor *fd = slab_alloc(fd_slab);
|
||||
if (!fd) {
|
||||
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* find the file */
|
||||
struct file file;
|
||||
int result = rootfs->ops.open(rootfs, &file, pathname);
|
||||
|
||||
if (result < 0) {
|
||||
kprintf(LOG_ERROR "Failed to open %s\n", pathname);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* add the file descriptor to the tree */
|
||||
fd->fd = fd_counter;
|
||||
fd->fs = rootfs;
|
||||
fd_counter += 1;
|
||||
|
||||
struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
|
||||
fdnode->value = (uint64_t)fd;
|
||||
|
||||
return fd->fd;
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||
|
||||
if (!fdnode)
|
||||
return -EBADF;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||
fdesc->fs->ops.close(fdesc->fs, &fdesc->file);
|
||||
|
||||
slab_free(fd_slab, fdesc);
|
||||
rbt_delete(kfd, fdnode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int seek(int fd, size_t offset, int whence)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue