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/ext2.c
82
fs/ext2.c
|
|
@ -218,82 +218,6 @@ int ext2_seek_file(void *extfs, struct file *file, size_t offset)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef KDEBUG
|
||||
static void ext2_print_dir(struct ext2fs *fs, struct ext2_inode *inode, int indent)
|
||||
{
|
||||
static const char *types[] = { "UNKNOWN", "FILE", "DIR", "CHARDEV", "BLOCKDEV", "FIFO", "SOCKET", "SYMLINK" };
|
||||
/* print the names of files and directories in the root directory */
|
||||
|
||||
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
void *oentry = entry;
|
||||
if (!entry)
|
||||
return;
|
||||
|
||||
ext2_read_block(fs, entry, inode->block[0]);
|
||||
|
||||
while (entry->inode) {
|
||||
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
|
||||
if (!name)
|
||||
break;
|
||||
|
||||
memcpy(name, entry->name, entry->name_len);
|
||||
name[entry->name_len] = '\0';
|
||||
|
||||
kprintf(LOG_DEBUG);
|
||||
for (int i = 0; i < indent; i++)
|
||||
kprintf(" ");
|
||||
kprintf("[%s] %s\n", types[MIN(7, entry->file_type)], name);
|
||||
|
||||
if (entry->file_type == EXT2_DE_DIR) {
|
||||
if (!strcmp(name, ".") || !strcmp(name, ".."))
|
||||
goto next;
|
||||
|
||||
struct ext2_inode *in = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||
if (!in)
|
||||
break;
|
||||
|
||||
ext2_read_inode(fs, in, entry->inode);
|
||||
ext2_print_dir(fs, in, indent + 1);
|
||||
kfree(in);
|
||||
} else if (entry->file_type == EXT2_DE_FILE) {
|
||||
/* print the contents of the file */
|
||||
struct ext2_inode *in = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||
if (!in)
|
||||
break;
|
||||
|
||||
ext2_read_inode(fs, in, entry->inode);
|
||||
|
||||
char *buf = kmalloc(in->size, ALLOC_KERN);
|
||||
|
||||
if (!buf) {
|
||||
kfree(in);
|
||||
break;
|
||||
}
|
||||
|
||||
ext2_read_block(fs, buf, in->block[0]);
|
||||
|
||||
kprintf(LOG_DEBUG);
|
||||
for (int i = 0; i < indent + 1; i++)
|
||||
kprintf(" ");
|
||||
|
||||
for (uint32_t i = 0; i < in->size; i++) {
|
||||
if (buf[i] == '\n')
|
||||
kprintf("\n");
|
||||
else
|
||||
console_write(buf[i]);
|
||||
}
|
||||
}
|
||||
next:
|
||||
|
||||
kfree(name);
|
||||
|
||||
entry = (void *)entry + entry->rec_len;
|
||||
}
|
||||
|
||||
kfree(oentry);
|
||||
}
|
||||
#endif /* KDEBUG */
|
||||
|
||||
struct fs *ext2_init_fs(struct block_device *bdev)
|
||||
{
|
||||
struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN);
|
||||
|
|
@ -328,12 +252,6 @@ struct fs *ext2_init_fs(struct block_device *bdev)
|
|||
|
||||
kfree(buf);
|
||||
|
||||
#ifdef KDEBUG
|
||||
struct ext2_inode root;
|
||||
ext2_read_inode(extfs, &root, 2);
|
||||
ext2_print_dir(extfs, &root, 0);
|
||||
#endif /* KDEBUG */
|
||||
|
||||
struct fs *ret = kmalloc(sizeof(struct fs), ALLOC_KERN);
|
||||
ret->fs = extfs;
|
||||
ret->type = VFS_TYPE_EXT2;
|
||||
|
|
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@
|
|||
#define ENOMEM 1
|
||||
#define ENOENT 2
|
||||
#define EINVAL 3
|
||||
#define EBADF 4
|
||||
|
||||
#endif /* _ERRNO_H_ */
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
#include <kernel/common.h>
|
||||
|
||||
#define VFSE_IS_BDEV 0xFFFA
|
||||
|
||||
#define VFS_TYPE_EXT2 0x0001
|
||||
|
||||
struct inode {
|
||||
|
|
@ -38,9 +40,20 @@ struct file {
|
|||
|
||||
uint32_t inode_num; /* inode number */
|
||||
uint64_t size; /* size of the file */
|
||||
uint64_t pos; /* current position in the file */
|
||||
|
||||
void *fs_file;
|
||||
char *path; /* path to the file */
|
||||
|
||||
void *fs_file; /* filesystem specific object */
|
||||
};
|
||||
|
||||
struct file_descriptor {
|
||||
struct file file;
|
||||
|
||||
uint64_t pos;
|
||||
int fd;
|
||||
uint32_t flags;
|
||||
|
||||
struct fs *fs;
|
||||
};
|
||||
|
||||
struct fs_ops {
|
||||
|
|
|
|||
Loading…
Reference in a new issue