From b7c8ce1d2b2aec2c7dcf056b9fa72e16206cc85e Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 15 Jan 2020 13:05:53 +0200 Subject: [PATCH] Clean comments up, merge ext2 inode block index code --- doc/notes.txt | 2 -- include/sys/chr.h | 1 - include/sys/dev.h | 3 --- sys/debug.c | 1 - sys/init.c | 21 ------------------ sys/vfs/ext2/ext2.c | 1 - sys/vfs/ext2/ext2blk.c | 49 ++++++++++++++++++----------------------- sys/vfs/ext2/ext2vnop.c | 3 +-- 8 files changed, 23 insertions(+), 58 deletions(-) delete mode 100644 doc/notes.txt diff --git a/doc/notes.txt b/doc/notes.txt deleted file mode 100644 index 9aa78cb..0000000 --- a/doc/notes.txt +++ /dev/null @@ -1,2 +0,0 @@ -TODO: Rewrite thread_init, it's shit -TODO: execve() after fork() should inherit old thread's ioctx diff --git a/include/sys/chr.h b/include/sys/chr.h index a687e21..077b87f 100644 --- a/include/sys/chr.h +++ b/include/sys/chr.h @@ -4,7 +4,6 @@ struct chrdev { void *dev_data; - // TODO: maybe something like flush() ssize_t (*write) (struct chrdev *chr, const void *buf, size_t pos, size_t lim); ssize_t (*read) (struct chrdev *chr, void *buf, size_t pos, size_t lim); }; diff --git a/include/sys/dev.h b/include/sys/dev.h index 48930ca..a303f39 100644 --- a/include/sys/dev.h +++ b/include/sys/dev.h @@ -3,9 +3,6 @@ struct vnode; -// TODO: something like device name alias -// for example, /dev/root aliased to -// /dev/ramN or /dev/sdXN enum dev_class { DEV_CLASS_BLOCK = 1, DEV_CLASS_CHAR = 2, diff --git a/sys/debug.c b/sys/debug.c index 34f347f..3fc48d7 100644 --- a/sys/debug.c +++ b/sys/debug.c @@ -45,7 +45,6 @@ void fmtsiz(char *out, size_t sz) { out[l++] = 0; } -// TODO: make debugc a __weak function of a character void debugc(int level, char c) { #if defined(ARCH_AMD64) rs232_send(RS232_COM1, c); diff --git a/sys/init.c b/sys/init.c index 93289e7..a3c2723 100644 --- a/sys/init.c +++ b/sys/init.c @@ -16,8 +16,6 @@ #include "sys/mm.h" static void user_init_start(void) { - // TODO: Instead of allocating and reading the whole buffer, rewrite binfmt_elf in a way - // to use lseek and stuff const char *init_filename = "/init"; struct ofile fd; struct stat st; @@ -57,21 +55,12 @@ static void user_init_start(void) { panic("Failed to initialize user init process: %s\n", kstrerror(res)); } - //file_buffer = kmalloc(st.st_size); - //_assert(file_buffer); - - //if ((bread = vfs_read(kernel_ioctx, &fd, file_buffer, st.st_size)) != st.st_size) { - // panic("Failed to read init binary\n"); - //} - if ((res = elf_load(user_init, kernel_ioctx, &fd)) != 0) { panic("Failed to load ELF binary: %s\n", kstrerror(res)); } vfs_close(kernel_ioctx, &fd); - //kfree(file_buffer); - // Setup I/O context for init task struct ofile *stdin, *stdout, *stderr; @@ -81,7 +70,6 @@ static void user_init_start(void) { _assert(stdout); stderr = stdout; - // TODO: use dev_find and open its node instead struct vnode *stdout_device; if ((res = dev_find(DEV_CLASS_CHAR, "tty0", &stdout_device)) != 0) { panic("%s: %s\n", "tty0", kstrerror(res)); @@ -95,7 +83,6 @@ static void user_init_start(void) { } ++stdout->refcount; - // TODO: use STDIN_/STDOUT_/STDERR_FILENO macros user_init->fds[0] = stdin; user_init->fds[1] = stdout; user_init->fds[2] = stderr; @@ -128,14 +115,6 @@ void init_func(void *arg) { panic("Failed to mount root device: %s\n", kstrerror(res)); } - //// Mount something else - //if ((res = dev_find(DEV_CLASS_BLOCK, "sda1", &root_dev)) != 0) { - // panic("sda1: %s\n", kstrerror(res)); - //} - //if ((res = vfs_mount(kernel_ioctx, "/mnt", root_dev->dev, "ext2", NULL)) != 0) { - // panic("Failed to mount root device: %s\n", kstrerror(res)); - //} - // Start user init binary user_init_start(); diff --git a/sys/vfs/ext2/ext2.c b/sys/vfs/ext2/ext2.c index 224e86b..10cacdb 100644 --- a/sys/vfs/ext2/ext2.c +++ b/sys/vfs/ext2/ext2.c @@ -94,7 +94,6 @@ static int ext2_fs_umount(struct fs *fs) { } static struct vnode *ext2_fs_get_root(struct fs *fs) { - // TODO: make sure get_root is only called once per filesystem struct ext2_extsb *sb = fs->fs_private; kdebug("ext2_fs_get_root()\n"); diff --git a/sys/vfs/ext2/ext2blk.c b/sys/vfs/ext2/ext2blk.c index 92ca48f..51f143b 100644 --- a/sys/vfs/ext2/ext2blk.c +++ b/sys/vfs/ext2/ext2blk.c @@ -41,39 +41,34 @@ int ext2_write_block(struct fs *ext2, uint32_t block_no, const void *buf) { return res; } -int ext2_write_inode_block(struct fs *ext2, struct ext2_inode *inode, uint32_t index, const void *buf) { +static uint32_t ext2_get_inode_block(struct fs *ext2, struct ext2_inode *inode, uint32_t index) { if (index < 12) { - uint32_t block_number = inode->direct_blocks[index]; - return ext2_write_block(ext2, block_number, buf); + return inode->direct_blocks[index]; } else { - // TODO - //abort(); - panic("Not implemented\n"); + struct ext2_extsb *sb = (struct ext2_extsb *) ext2->fs_private; + char buf[1024]; + int res; + + if (index < 12 + (sb->block_size / 4)) { + if ((res = ext2_read_block(ext2, inode->l1_indirect_block, buf)) < 0) { + return res; + } + + return ((uint32_t *) buf)[index - 12]; + } else { + panic("Inode block index has too high indirection level\n"); + } } } +int ext2_write_inode_block(struct fs *ext2, struct ext2_inode *inode, uint32_t index, const void *buf) { + uint32_t block_number = ext2_get_inode_block(ext2, inode, index); + return ext2_write_block(ext2, block_number, buf); +} + int ext2_read_inode_block(struct fs *ext2, struct ext2_inode *inode, uint32_t index, void *buf) { - if (index < 12) { - // Use direct ptrs - uint32_t block_number = inode->direct_blocks[index]; - return ext2_read_block(ext2, block_number, buf); - } else { - struct ext2_extsb *sb = (struct ext2_extsb *) ext2->fs_private; - // Use buf as indirection block buffer (I think we're allowed to do so) - - if (index < 12 + (sb->block_size / 4)) { - // Single indirection - if (ext2_read_block(ext2, inode->l1_indirect_block, buf) < 0) { - return -EIO; - } - - uint32_t block_number = ((uint32_t *) buf)[index - 12]; - return ext2_read_block(ext2, block_number, buf); - } else { - // Not implemented yet - return -EIO; - } - } + uint32_t block_number = ext2_get_inode_block(ext2, inode, index); + return ext2_read_block(ext2, block_number, buf); } int ext2_read_inode(struct fs *ext2, struct ext2_inode *inode, uint32_t ino) { diff --git a/sys/vfs/ext2/ext2vnop.c b/sys/vfs/ext2/ext2vnop.c index 93f7c76..3b2df92 100644 --- a/sys/vfs/ext2/ext2vnop.c +++ b/sys/vfs/ext2/ext2vnop.c @@ -280,7 +280,7 @@ static int ext2_vnode_creat(struct vnode *at, const char *name, uid_t uid, gid_t ent_inode->uid = uid; ent_inode->gid = gid; - // TODO: only regular files can be created this way now + // NOTE: only regular files can be created this way now ent_inode->type_perm = (mode & 0x1FF) | (EXT2_TYPE_REG); ent_inode->disk_sector_count = 0; ent_inode->size_lower = 0; @@ -479,7 +479,6 @@ static int ext2_vnode_truncate(struct vnode *vn, size_t length) { } } -// TODO: replace this with getdents static ssize_t ext2_vnode_readdir(struct ofile *fd, struct dirent *vfsdir) { struct vnode *vn = fd->vnode; struct ext2_inode *inode = (struct ext2_inode *) vn->fs_data;