From 017758ea418538dc95ea6288f2b2fdd8a5a45581 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 18 Oct 2023 21:53:43 -0400 Subject: [PATCH] ext2: remove ext2_dir struct and funcs --- fs/ext2.c | 38 +++++++++----------------------------- fs/gpt.c | 2 +- include/errno.h | 2 ++ include/fs/ext2.h | 9 --------- 4 files changed, 12 insertions(+), 39 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index b236bdc..429fc95 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -1,4 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include "kernel/lock.h" +#include "stdio.h" +#include "string.h" #include #include @@ -7,7 +10,7 @@ static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb { char *buf = kmalloc(1024, ALLOC_DMA); if (!buf) - return -1; + return -ENOMEM; #ifdef KDEBUG kprintf(LOG_DEBUG "Reading superblock from device %s, start_lba: %X\n", bdev->name, bdev->lba_start); @@ -22,7 +25,7 @@ static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb struct ext2_superblock *sb = (struct ext2_superblock *)buf; if (sb->magic != EXT2_SUPER_MAGIC) { kfree(buf); - return -1; + return -EINVAL; } memcpy(sb_struct, buf, sizeof(*sb_struct)); @@ -62,7 +65,7 @@ int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, uint32_t inode) struct ext2_group_desc *gd = kmalloc(fs->block_size, ALLOC_DMA); if (!gd) - return -1; + return -ENOMEM; int ret = ext2_read_block(fs, gd, fs->sb.first_data_block + 1 + group); if (ret < 0) { @@ -75,7 +78,7 @@ int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, uint32_t inode) struct ext2_inode *in = kmalloc(fs->block_size, ALLOC_DMA); if (!in) - return -1; + return -ENOMEM; ret = ext2_read_block(fs, in, inode_table + block); if (ret < 0) { @@ -99,7 +102,7 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode) struct ext2_group_desc *gd = kmalloc(fs->block_size, ALLOC_DMA); if (!gd) - return -1; + return -ENOMEM; int ret = ext2_read_block(fs, gd, fs->sb.first_data_block + 1 + group); if (ret < 0) { @@ -112,7 +115,7 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode) struct ext2_inode *out = kmalloc(fs->block_size, ALLOC_DMA); if (!out) - return -1; + return -ENOMEM; ret = ext2_read_block(fs, out, inode_table + block); if (ret < 0) { @@ -127,29 +130,6 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode) return ret; } -int ext2_read_dir(struct ext2fs *fs, struct ext2_inode *in, struct ext2_dir *out, uint32_t index) -{ - uint32_t block = index / (fs->block_size / sizeof(struct ext2_dir)); - uint32_t offset = index % (fs->block_size / sizeof(struct ext2_dir)); - - uint32_t block_num = in->block[block]; - - struct ext2_dir *buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!buf) - return -1; - - int ret = ext2_read_block(fs, buf, block_num); - if (ret < 0) { - kfree(buf); - return ret; - } - - memcpy(out, (void *)((uint64_t)buf + offset), sizeof(*out)); - kfree(buf); - - return ret; -} - struct ext2fs *ext2_init_fs(struct block_device *bdev) { struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN); diff --git a/fs/gpt.c b/fs/gpt.c index 6fae1b3..7cddfa2 100644 --- a/fs/gpt.c +++ b/fs/gpt.c @@ -58,7 +58,7 @@ void block_gpt_init(struct block_device *dev) if (entry->lba_first == 0) continue; - if (entry->lba_first > dev->block_count) + if (entry->lba_first >= dev->block_count) continue; /* Name and register the partition */ diff --git a/include/errno.h b/include/errno.h index 0a1c698..575432d 100644 --- a/include/errno.h +++ b/include/errno.h @@ -3,6 +3,8 @@ #ifndef _ERRNO_H_ #define _ERRNO_H_ +#define ENOMEM 1 #define ENOENT 2 +#define EINVAL 3 #endif /* _ERRNO_H_ */ diff --git a/include/fs/ext2.h b/include/fs/ext2.h index d53bc2f..96ada32 100644 --- a/include/fs/ext2.h +++ b/include/fs/ext2.h @@ -121,15 +121,6 @@ struct ext2_dir_entry { char name[]; } PACKED; -struct ext2_dir { - struct ext2_dir_entry *entry; - uint32_t inode; - char *name; - size_t name_len; - - struct ext2_dir *next; -}; - struct ext2fs { struct ext2_superblock sb; struct block_device *bdev;