From 6bbdbb98fa109925c051ddf789681ed0df3e0942 Mon Sep 17 00:00:00 2001 From: acevest Date: Thu, 22 Jun 2023 08:18:32 +0800 Subject: [PATCH] ata_read_ext2_sb use block_read --- drivers/ata.c | 32 +++----------------------------- drivers/block.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- include/disk.h | 2 +- include/fs.h | 10 ++++++---- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/drivers/ata.c b/drivers/ata.c index 2384f4a..0df04fa 100644 --- a/drivers/ata.c +++ b/drivers/ata.c @@ -8,7 +8,6 @@ */ #include #include -#include #include #include #include @@ -90,31 +89,6 @@ void ata_read_partions(ide_part_t *part, const char *buf) { } } -// 读hda0 的 super block -void ata_read_ext2_sb() { - ide_part_t *part = ide_drives[0].partions + 1; - const int size = 1024; - const int offset = 1024; - char *buf = kmalloc(size, 0); - disk_request_t r; - r.dev = MAKE_DEV(DEV_MAJOR_IDE0, 1); - r.command = DISK_REQ_READ; - r.pos = part->lba_start + offset / SECT_SIZE; - r.count = size / SECT_SIZE; - r.buf = buf; - send_disk_request(&r); - ext2_sb_t *p = (ext2_sb_t *)buf; - printk("inodes count %u inodes per group %u free %u\n", p->s_inodes_count, p->s_inodes_per_group, - p->s_free_inodes_count); - printk("blocks count %u blocks per group %u free %u magic %04x\n", p->s_blocks_count, p->s_blocks_per_group, - p->s_free_blocks_count, p->s_magic); - printk("first ino %u inode size %u first data block %u\n", p->s_first_ino, p->s_inode_size, p->s_first_data_block); - printk("log block size %u write time %u\n", p->s_log_block_size, p->s_wtime); - p->s_volume_name[63] = 0; - printk("volume %s\n", p->s_volume_name); - kfree(buf); -} - // 《AT Attachment 8 - ATA/ATAPI Command Set》 void ide_ata_init() { for (int i = 0; i < MAX_IDE_DRIVE_CNT; i++) { @@ -265,15 +239,15 @@ void ide_read_partions() { } } -void ide_disk_read(dev_t dev, uint32_t block, uint32_t size, char *buf) { +void ide_disk_read(dev_t dev, uint32_t sect_nr, uint32_t count, char *buf) { ide_drive_t *drv = ide_get_drive(dev); uint64_t lba_offset = drv->partions[DEV_MINOR((dev))].lba_start; disk_request_t r; r.dev = dev; r.command = DISK_REQ_READ; - r.pos = lba_offset + block / SECT_SIZE; - r.count = size / SECT_SIZE; + r.pos = lba_offset + sect_nr; + r.count = count; r.buf = buf; send_disk_request(&r); } diff --git a/drivers/block.c b/drivers/block.c index 6cf9a00..ed538dd 100644 --- a/drivers/block.c +++ b/drivers/block.c @@ -11,7 +11,17 @@ #include #include -blk_buffer_t *block_read(dev_t dev, uint32_t block, uint32_t size) { +#define BLOCK_BUFFER_HASH_TABLE_SIZE 37 +atomic_t hash_cnt; +blk_buffer_t *block_buffer_hash_table[BLOCK_BUFFER_HASH_TABLE_SIZE] = { + 0, +}; + +int hash(dev_t dev, uint32_t block) { return ((co ~dev) ^ block) % BLOCK_BUFFER_HASH_TABLE_SIZE; } + +blk_buffer_t *get_hash_block_buffer(dev_t dev, uint32_t block, uint16_t size) {} + +blk_buffer_t *block_read(dev_t dev, uint32_t block) { blk_buffer_t *bb = 0; assert(DEV_MAJOR(dev) == DEV_MAJOR_IDE0); @@ -20,7 +30,37 @@ blk_buffer_t *block_read(dev_t dev, uint32_t block, uint32_t size) { // 目前不提供hash表组强起来的缓冲功能 // 直接读 - ide_disk_read(dev, block, size, bb->data); + // TODO:根据dev得到正确的blocksize + const int blocksize = 1024; + + bb->data = kmalloc(blocksize, 0); // debug + + ide_disk_read(dev, block * blocksize / 512, 1, bb->data); return bb; } + +#include +// 读hda1 的 super block +void ata_read_ext2_sb() { + // 初始默认blocksize = 1024 + // 则ext2_superblock应该在第1个block的offset为0的位置 + // ext2_superblock默认大小1024 + + const int offset = 0; + const int size = offset + 1024; + const int block = 1; + + blk_buffer_t *bb = block_read(MAKE_DEV(DEV_MAJOR_IDE0, 1), block); + + ext2_sb_t *p = (ext2_sb_t *)(bb->data + offset); + printk("inodes count %u inodes per group %u free %u\n", p->s_inodes_count, p->s_inodes_per_group, + p->s_free_inodes_count); + printk("blocks count %u blocks per group %u free %u magic %04x\n", p->s_blocks_count, p->s_blocks_per_group, + p->s_free_blocks_count, p->s_magic); + printk("first ino %u inode size %u first data block %u\n", p->s_first_ino, p->s_inode_size, p->s_first_data_block); + printk("log block size %u write time %u\n", p->s_log_block_size, p->s_wtime); + p->s_volume_name[63] = 0; + printk("volume %s\n", p->s_volume_name); + kfree(bb->data); +} diff --git a/include/disk.h b/include/disk.h index 76d382c..5c38949 100644 --- a/include/disk.h +++ b/include/disk.h @@ -47,4 +47,4 @@ typedef struct { void send_disk_request(disk_request_t *r); -void ide_disk_read(dev_t dev, uint32_t block, uint32_t size, char *buf); +void ide_disk_read(dev_t dev, uint32_t sect_nr, uint32_t count, char *buf); diff --git a/include/fs.h b/include/fs.h index b9057b5..6cfc181 100644 --- a/include/fs.h +++ b/include/fs.h @@ -109,12 +109,14 @@ static inline pInode find_empty_inode() typedef uint32_t dev_t; -typedef struct blk_bufer { - uint32_t blocknr; // block number - char *data; // - uint16_t size; // block size +typedef struct blk_buffer { + uint32_t block; // block number + char *data; // + uint16_t block_size; // block size dev_t dev; page_t *page; + struct blk_buffer *next; + struct blk_buffer *hash_next; } blk_buffer_t; #endif //_FS_H -- 2.44.0