mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-09 11:16:47 +00:00
将具体磁盘类型与fat32文件系统进行分离
This commit is contained in:
parent
0fcba99282
commit
339053a20e
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -129,7 +129,10 @@
|
||||
"proc-types.h": "c",
|
||||
"traceback.h": "c",
|
||||
"bitcount.h": "c",
|
||||
"limits.h": "c"
|
||||
"limits.h": "c",
|
||||
"block.h": "c",
|
||||
"blk_types.h": "c",
|
||||
"mutex.h": "c"
|
||||
},
|
||||
"C_Cpp.errorSquiggles": "Enabled",
|
||||
"esbonio.sphinx.confDir": ""
|
||||
|
@ -16,7 +16,18 @@ struct block_device_operation
|
||||
long (*open)();
|
||||
long (*close)();
|
||||
long (*ioctl)(long cmd, long arg);
|
||||
long (*transfer)(long cmd, ul LBA_start, ul count, uint64_t buffer, uint8_t arg0, uint8_t arg1);
|
||||
|
||||
/**
|
||||
* @brief 块设备驱动程序的传输函数
|
||||
*
|
||||
* @param gd 磁盘设备结构体
|
||||
* @param cmd 控制命令
|
||||
* @param base_addr 48位LBA地址
|
||||
* @param count total sectors to read
|
||||
* @param buf 缓冲区线性地址
|
||||
* @return long
|
||||
*/
|
||||
long (*transfer)(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -54,6 +65,7 @@ struct block_device_request_queue
|
||||
struct block_device
|
||||
{
|
||||
sector_t bd_start_sector; // 该分区的起始扇区
|
||||
uint64_t bd_start_LBA; // 起始LBA号
|
||||
sector_t bd_sectors_num; // 该分区的扇区数
|
||||
struct vfs_superblock_t *bd_superblock; // 执行超级块的指针
|
||||
struct blk_gendisk *bd_disk; // 当前分区所属的磁盘
|
||||
@ -61,6 +73,9 @@ struct block_device
|
||||
uint16_t bd_partno; // 在磁盘上的分区号
|
||||
};
|
||||
|
||||
// 定义blk_gendisk中的标志位
|
||||
#define BLK_GF_AHCI (1 << 0)
|
||||
|
||||
/**
|
||||
* @brief 磁盘设备结构体
|
||||
*
|
||||
|
10
kernel/common/block.h
Normal file
10
kernel/common/block.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include "blk_types.h"
|
||||
|
||||
/**
|
||||
* @brief 将磁盘注册到块设备框架中
|
||||
*
|
||||
* @param gendisk 磁盘结构体
|
||||
* @return int 错误码
|
||||
*/
|
||||
int blk_register_gendisk(struct blk_gendisk * gendisk);
|
@ -3,7 +3,7 @@
|
||||
#include <common/atomic.h>
|
||||
#include <common/spinlock.h>
|
||||
#include <common/glib.h>
|
||||
#include <process/process.h>
|
||||
#include <process/proc-types.h>
|
||||
|
||||
/**
|
||||
* @brief Mutex - 互斥锁
|
||||
|
@ -4,12 +4,17 @@
|
||||
#include <syscall/syscall.h>
|
||||
#include <syscall/syscall_num.h>
|
||||
#include <sched/sched.h>
|
||||
#include <common/string.h>
|
||||
#include <common/block.h>
|
||||
#include <filesystem/MBR.h>
|
||||
|
||||
struct pci_device_structure_header_t *ahci_devs[MAX_AHCI_DEVICES];
|
||||
|
||||
struct block_device_request_queue ahci_req_queue;
|
||||
|
||||
uint32_t count_ahci_devices = 0;
|
||||
struct blk_gendisk ahci_gendisk0 = {0}; // 暂时硬性指定一个ahci_device
|
||||
|
||||
static uint32_t count_ahci_devices = 0;
|
||||
|
||||
static uint64_t ahci_port_base_vaddr; // 端口映射base addr
|
||||
static uint64_t ahci_port_base_phys_addr; // 端口映射的物理基地址(ahci控制器的参数的地址都是物理地址)
|
||||
@ -25,6 +30,113 @@ static int ahci_find_cmdslot(HBA_PORT *port);
|
||||
// 计算HBA_MEM的虚拟内存地址
|
||||
#define cal_HBA_MEM_VIRT_ADDR(device_num) (AHCI_MAPPING_BASE + (ul)(((struct pci_device_structure_general_device_t *)(ahci_devs[device_num]))->BAR5 - ((((struct pci_device_structure_general_device_t *)(ahci_devs[0]))->BAR5) & PAGE_2M_MASK)))
|
||||
|
||||
long ahci_open();
|
||||
long ahci_close();
|
||||
static long ahci_ioctl(long cmd, long arg);
|
||||
static long ahci_transfer(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf);
|
||||
|
||||
struct block_device_operation ahci_operation =
|
||||
{
|
||||
.open = ahci_open,
|
||||
.close = ahci_close,
|
||||
.ioctl = ahci_ioctl,
|
||||
.transfer = ahci_transfer,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ahci驱动器在block_device中的私有数据结构体
|
||||
*
|
||||
*/
|
||||
struct ahci_blk_private_data
|
||||
{
|
||||
uint16_t ahci_ctrl_num; // ahci控制器号
|
||||
uint16_t ahci_port_num; // ahci端口号
|
||||
struct MBR_disk_partition_table_t *part_table; // 分区表
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 申请ahci设备的私有信息结构体
|
||||
*
|
||||
* @return struct ahci_blk_private_data* 申请到的私有信息结构体
|
||||
*/
|
||||
static struct ahci_blk_private_data *__alloc_private_data()
|
||||
{
|
||||
struct ahci_blk_private_data *data = (struct ahci_blk_private_data *)kzalloc(sizeof(struct ahci_blk_private_data), 0);
|
||||
data->part_table = (struct MBR_disk_partition_table_t *)kzalloc(512, 0);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 释放ahci设备的分区的私有信息结构体
|
||||
*
|
||||
* @param pdata 待释放的结构体
|
||||
* @return int 错误码
|
||||
*/
|
||||
static int __release_private_data(struct ahci_blk_private_data *pdata)
|
||||
{
|
||||
kfree(pdata->part_table);
|
||||
kfree(pdata);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 初始化gendisk结构体(暂时只支持1个gendisk)
|
||||
*
|
||||
*/
|
||||
static int ahci_init_gendisk()
|
||||
{
|
||||
memset(&ahci_gendisk0, 0, sizeof(ahci_gendisk0));
|
||||
strcpy(ahci_gendisk0.disk_name, "ahci0");
|
||||
ahci_gendisk0.flags = BLK_GF_AHCI;
|
||||
ahci_gendisk0.fops = &ahci_operation;
|
||||
mutex_init(&ahci_gendisk0.open_mutex);
|
||||
ahci_gendisk0.request_queue = &ahci_req_queue;
|
||||
// 为存储分区结构,分配内存空间
|
||||
ahci_gendisk0.private_data = __alloc_private_data();
|
||||
// 读取分区表
|
||||
// 暂时假设全都是MBR分区表的
|
||||
// todo: 支持GPT
|
||||
|
||||
((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->ahci_ctrl_num = 0;
|
||||
((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->ahci_port_num = 0;
|
||||
|
||||
MBR_read_partition_table(&ahci_gendisk0, ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->part_table);
|
||||
struct MBR_disk_partition_table_t *ptable = ((struct ahci_blk_private_data *)ahci_gendisk0.private_data)->part_table;
|
||||
|
||||
// 求出可用分区数量
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
// 分区可用
|
||||
if (ptable->DPTE[i].type !=0)
|
||||
++ahci_gendisk0.part_cnt;
|
||||
}
|
||||
if (ahci_gendisk0.part_cnt)
|
||||
{
|
||||
// 分配分区结构体数组的空间
|
||||
ahci_gendisk0.partition = (struct block_device *)kzalloc(ahci_gendisk0.part_cnt * sizeof(struct block_device), 0);
|
||||
int cnt = 0;
|
||||
// 循环遍历每个分区
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
// 分区可用
|
||||
if (ptable->DPTE[i].type !=0)
|
||||
{
|
||||
// 初始化分区结构体
|
||||
ahci_gendisk0.partition[cnt].bd_disk = &ahci_gendisk0;
|
||||
ahci_gendisk0.partition[cnt].bd_partno = cnt;
|
||||
ahci_gendisk0.partition[cnt].bd_queue = &ahci_req_queue;
|
||||
ahci_gendisk0.partition[cnt].bd_sectors_num = ptable->DPTE[i].total_sectors;
|
||||
ahci_gendisk0.partition[cnt].bd_start_sector = ptable->DPTE[i].starting_sector;
|
||||
ahci_gendisk0.partition[cnt].bd_superblock = NULL; // 挂载文件系统时才会初始化superblock
|
||||
ahci_gendisk0.partition[cnt].bd_start_LBA = ptable->DPTE[i].starting_LBA;
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 初始化ahci模块
|
||||
*
|
||||
@ -65,6 +177,8 @@ void ahci_init()
|
||||
ahci_req_queue.in_service = NULL;
|
||||
wait_queue_init(&ahci_req_queue.wait_queue_list, NULL);
|
||||
ahci_req_queue.request_count = 0;
|
||||
|
||||
ahci_init_gendisk();
|
||||
kinfo("AHCI initialized.");
|
||||
}
|
||||
|
||||
@ -362,7 +476,7 @@ static bool ahci_write(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_
|
||||
cmdfis->counth = count >> 8;
|
||||
// printk("[slot]{%d}", slot);
|
||||
port->ci = 1; // Issue command
|
||||
|
||||
|
||||
current_pcb->flags |= PF_NEED_SCHED;
|
||||
sched();
|
||||
int retval = AHCI_SUCCESS;
|
||||
@ -486,9 +600,8 @@ static long ahci_query_disk()
|
||||
list_del(&(ahci_req_queue.in_service->wait_queue.wait_list));
|
||||
--ahci_req_queue.request_count;
|
||||
|
||||
long ret_val;
|
||||
long ret_val = 0;
|
||||
|
||||
|
||||
switch (pack->blk_pak.cmd)
|
||||
{
|
||||
case AHCI_CMD_READ_DMA_EXT:
|
||||
@ -524,21 +637,21 @@ static void ahci_submit(struct ahci_request_packet_t *pack)
|
||||
/**
|
||||
* @brief ahci驱动程序的传输函数
|
||||
*
|
||||
* @param gd 磁盘设备结构体
|
||||
* @param cmd 控制命令
|
||||
* @param base_addr 48位LBA地址
|
||||
* @param count total sectors to read
|
||||
* @param buf 缓冲区线性地址
|
||||
* @param ahci_ctrl_num ahci控制器号
|
||||
* @param port_num ahci控制器端口号
|
||||
* @return long
|
||||
*/
|
||||
static long ahci_transfer(long cmd, uint64_t base_addr, uint64_t count, uint64_t buf, uint8_t ahci_ctrl_num, uint8_t port_num)
|
||||
static long ahci_transfer(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf)
|
||||
{
|
||||
struct ahci_request_packet_t *pack = NULL;
|
||||
struct ahci_blk_private_data *pdata = (struct ahci_blk_private_data *)gd->private_data;
|
||||
|
||||
if (cmd == AHCI_CMD_READ_DMA_EXT || cmd == AHCI_CMD_WRITE_DMA_EXT)
|
||||
{
|
||||
pack = ahci_make_request(cmd, base_addr, count, buf, ahci_ctrl_num, port_num);
|
||||
pack = ahci_make_request(cmd, base_addr, count, buf, pdata->ahci_ctrl_num, pdata->ahci_port_num);
|
||||
ahci_submit(pack);
|
||||
}
|
||||
else
|
||||
@ -557,10 +670,3 @@ static long ahci_transfer(long cmd, uint64_t base_addr, uint64_t count, uint64_t
|
||||
static long ahci_ioctl(long cmd, long arg)
|
||||
{
|
||||
}
|
||||
struct block_device_operation ahci_operation =
|
||||
{
|
||||
.open = ahci_open,
|
||||
.close = ahci_close,
|
||||
.ioctl = ahci_ioctl,
|
||||
.transfer = ahci_transfer,
|
||||
};
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "usb.h"
|
||||
#include "xhci/xhci.h"
|
||||
#include <common/kprint.h>
|
||||
#include <common/errno.h>
|
||||
#include <driver/pci/pci.h>
|
||||
#include <debug/bug.h>
|
||||
#include <common/spinlock.h>
|
||||
@ -28,7 +29,7 @@ int usb_init()
|
||||
if (WARN_ON(usb_pdevs_count == 0))
|
||||
{
|
||||
kwarn("There is no usb hardware in this computer!");
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
kdebug("usb_pdevs_count=%d", usb_pdevs_count);
|
||||
// 初始化每个usb控制器
|
||||
@ -54,7 +55,7 @@ int usb_init()
|
||||
|
||||
default:
|
||||
kerror("Error value of usb_pdevs[%d]->ProgIF: %#02x", i, usb_pdevs[i]->ProgIF);
|
||||
return;
|
||||
return -EINVAL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1704,7 +1704,7 @@ void xhci_init(struct pci_device_structure_general_device_t *dev_hdr)
|
||||
// 创建scratchpad buffers
|
||||
for (int i = 0; i < max_scratchpad_buf; ++i)
|
||||
{
|
||||
uint64_t buf_vaddr = kzalloc(xhci_hc[cid].page_size, 0);
|
||||
uint64_t buf_vaddr = (uint64_t)kzalloc(xhci_hc[cid].page_size, 0);
|
||||
__write8b(xhci_hc[cid].scratchpad_buf_array_vaddr, virt_2_phys(buf_vaddr));
|
||||
}
|
||||
}
|
||||
|
@ -9,12 +9,9 @@ struct MBR_disk_partition_table_t MBR_partition_tables[MBR_MAX_AHCI_CTRL_NUM][MB
|
||||
*
|
||||
* @param ahci_ctrl_num ahci控制器编号
|
||||
* @param ahci_port_num ahci端口编号
|
||||
* @param buf 输出缓冲区(512字节)
|
||||
*/
|
||||
struct MBR_disk_partition_table_t *MBR_read_partition_table(uint8_t ahci_ctrl_num, uint8_t ahci_port_num)
|
||||
int MBR_read_partition_table(struct blk_gendisk *gd, void *buf)
|
||||
{
|
||||
unsigned char buf[512];
|
||||
memset(buf, 0, 512);
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, 0, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
|
||||
MBR_partition_tables[ahci_ctrl_num][ahci_port_num] = *(struct MBR_disk_partition_table_t *)buf;
|
||||
return &MBR_partition_tables[ahci_ctrl_num][ahci_port_num];
|
||||
return gd->fops->transfer(gd, AHCI_CMD_READ_DMA_EXT, 0, 1, (uint64_t)buf);
|
||||
}
|
@ -10,6 +10,7 @@
|
||||
*/
|
||||
#pragma once
|
||||
#include <common/glib.h>
|
||||
#include <common/blk_types.h>
|
||||
|
||||
#define MBR_MAX_AHCI_CTRL_NUM 4 // 系统支持的最大的ahci控制器数量
|
||||
#define MBR_MAX_AHCI_PORT_NUM 32 // 系统支持的每个ahci控制器对应的MBR磁盘数量(对应ahci磁盘号)
|
||||
@ -53,5 +54,6 @@ extern struct MBR_disk_partition_table_t MBR_partition_tables[MBR_MAX_AHCI_CTRL_
|
||||
*
|
||||
* @param ahci_ctrl_num ahci控制器编号
|
||||
* @param ahci_port_num ahci端口编号
|
||||
* @param buf 输出缓冲区(512字节)
|
||||
*/
|
||||
struct MBR_disk_partition_table_t *MBR_read_partition_table(uint8_t ahci_ctrl_num, uint8_t ahci_port_num);
|
||||
int MBR_read_partition_table(struct blk_gendisk* gd, void *buf);
|
@ -15,12 +15,10 @@ static struct vfs_filesystem_type_t vfs_fs = {"filesystem", 0};
|
||||
* @brief 挂载文件系统
|
||||
*
|
||||
* @param name 文件系统名
|
||||
* @param DPTE 分区表entry
|
||||
* @param DPT_type 分区表类型
|
||||
* @param buf 文件系统的引导扇区
|
||||
* @param blk 块设备结构体
|
||||
* @return struct vfs_superblock_t*
|
||||
*/
|
||||
struct vfs_superblock_t *vfs_mount_fs(char *name, void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num)
|
||||
struct vfs_superblock_t *vfs_mount_fs(char *name, struct block_device *blk)
|
||||
{
|
||||
|
||||
struct vfs_filesystem_type_t *p = NULL;
|
||||
@ -28,7 +26,7 @@ struct vfs_superblock_t *vfs_mount_fs(char *name, void *DPTE, uint8_t DPT_type,
|
||||
{
|
||||
if (!strcmp(p->name, name)) // 存在符合的文件系统
|
||||
{
|
||||
return p->read_superblock(DPTE, DPT_type, buf, ahci_ctrl_num, ahci_port_num, part_num);
|
||||
return p->read_superblock(blk);
|
||||
}
|
||||
}
|
||||
kdebug("unsupported fs: %s", name);
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
#include <common/glib.h>
|
||||
#include <common/fcntl.h>
|
||||
#include <common/blk_types.h>
|
||||
|
||||
struct vfs_superblock_t *vfs_root_sb = NULL;
|
||||
|
||||
@ -53,6 +54,7 @@ struct vfs_superblock_t
|
||||
{
|
||||
struct vfs_dir_entry_t *root;
|
||||
struct vfs_super_block_operations_t *sb_ops;
|
||||
struct block_device * blk_device;
|
||||
void *private_sb_info;
|
||||
};
|
||||
|
||||
@ -91,7 +93,7 @@ struct vfs_filesystem_type_t
|
||||
{
|
||||
char *name;
|
||||
int fs_flags;
|
||||
struct vfs_superblock_t *(*read_superblock)(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。其中DPTE为磁盘分区表entry(MBR、GPT不同)
|
||||
struct vfs_superblock_t *(*read_superblock)(struct block_device *blk); // 解析文件系统引导扇区的函数,为文件系统创建超级块结构。
|
||||
struct vfs_filesystem_type_t *next;
|
||||
};
|
||||
|
||||
@ -174,12 +176,10 @@ uint64_t vfs_unregister_filesystem(struct vfs_filesystem_type_t *fs);
|
||||
* @brief 挂载文件系统
|
||||
*
|
||||
* @param name 文件系统名
|
||||
* @param DPTE 分区表entry
|
||||
* @param DPT_type 分区表类型
|
||||
* @param buf 文件系统的引导扇区
|
||||
* @param blk 块设备结构体
|
||||
* @return struct vfs_superblock_t*
|
||||
*/
|
||||
struct vfs_superblock_t *vfs_mount_fs(char *name, void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num);
|
||||
struct vfs_superblock_t *vfs_mount_fs(char *name, struct block_device *blk);
|
||||
|
||||
/**
|
||||
* @brief 按照路径查找文件
|
||||
|
13
kernel/filesystem/block.c
Normal file
13
kernel/filesystem/block.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <common/block.h>
|
||||
|
||||
/**
|
||||
* @brief 将磁盘注册到块设备框架中
|
||||
*
|
||||
* @param gendisk 磁盘结构体
|
||||
* @return int 错误码
|
||||
*/
|
||||
int blk_register_gendisk(struct blk_gendisk * gendisk)
|
||||
{
|
||||
// todo: 将磁盘注册到devfs中
|
||||
return 0;
|
||||
}
|
10
kernel/filesystem/devfs/devfs.c
Normal file
10
kernel/filesystem/devfs/devfs.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include "devfs.h"
|
||||
|
||||
/**
|
||||
* @brief 初始化devfs
|
||||
*
|
||||
*/
|
||||
void devfs_init()
|
||||
{
|
||||
|
||||
}
|
7
kernel/filesystem/devfs/devfs.h
Normal file
7
kernel/filesystem/devfs/devfs.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief 初始化devfs
|
||||
*
|
||||
*/
|
||||
void devfs_init();
|
@ -13,6 +13,8 @@ struct vfs_dir_entry_operations_t fat32_dEntry_ops;
|
||||
struct vfs_file_operations_t fat32_file_ops;
|
||||
struct vfs_inode_operations_t fat32_inode_ops;
|
||||
|
||||
extern struct blk_gendisk ahci_gendisk0;
|
||||
|
||||
/**
|
||||
* @brief 注册指定磁盘上的指定分区的fat32文件系统
|
||||
*
|
||||
@ -25,18 +27,8 @@ struct vfs_inode_operations_t fat32_inode_ops;
|
||||
struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t ahci_port_num, uint8_t part_num)
|
||||
{
|
||||
|
||||
struct MBR_disk_partition_table_t *DPT = MBR_read_partition_table(ahci_ctrl_num, ahci_port_num);
|
||||
|
||||
// for(i = 0 ;i < 512 ; i++)
|
||||
// color_printk(PURPLE,WHITE,"%02x",buf[i]);
|
||||
printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT->DPTE[part_num].starting_LBA, DPT->DPTE[part_num].type);
|
||||
uint8_t buf[512] = {0};
|
||||
|
||||
// 读取文件系统的boot扇区
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, DPT->DPTE[part_num].starting_LBA, 1, (uint64_t)&buf, ahci_ctrl_num, ahci_port_num);
|
||||
|
||||
// 挂载文件系统到vfs
|
||||
return vfs_mount_fs("FAT32", (void *)(&DPT->DPTE[part_num]), VFS_DPT_MBR, buf, ahci_ctrl_num, ahci_port_num, part_num);
|
||||
return vfs_mount_fs("FAT32", (ahci_gendisk0.partition + 0));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,9 +61,9 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru
|
||||
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
|
||||
struct block_device *blk = parent_inode->sb->blk_device;
|
||||
|
||||
uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_clus);
|
||||
uint8_t *buf = kzalloc(fsbi->bytes_per_clus, 0);
|
||||
|
||||
// 计算父目录项的起始簇号
|
||||
uint32_t cluster = finode->first_clus;
|
||||
@ -87,8 +79,7 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru
|
||||
// kdebug("sector=%d",sector);
|
||||
|
||||
// 读取父目录项的起始簇数据
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
// ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fat32_part_info[part_id].bootsector.BPB_SecPerClus, (uint64_t)buf, fat32_part_info[part_id].ahci_ctrl_num, fat32_part_info[part_id].ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf);
|
||||
|
||||
tmp_dEntry = (struct fat32_Directory_t *)buf;
|
||||
|
||||
@ -287,7 +278,7 @@ struct vfs_dir_entry_t *fat32_lookup(struct vfs_index_node_t *parent_inode, stru
|
||||
}
|
||||
|
||||
// 当前簇没有发现目标文件名,寻找下一个簇
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
|
||||
|
||||
if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到目标文件名
|
||||
{
|
||||
@ -314,7 +305,7 @@ find_lookup_success:; // 找到目标dentry
|
||||
|
||||
finode->first_clus = ((tmp_dEntry->DIR_FstClusHI << 16) | tmp_dEntry->DIR_FstClusLO) & 0x0fffffff;
|
||||
finode->dEntry_location_clus = cluster;
|
||||
finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; //计算dentry的偏移量
|
||||
finode->dEntry_location_clus_offset = tmp_dEntry - (struct fat32_Directory_t *)buf; // 计算dentry的偏移量
|
||||
// kdebug("finode->dEntry_location_clus=%#018lx", finode->dEntry_location_clus);
|
||||
// kdebug("finode->dEntry_location_clus_offset=%#018lx", finode->dEntry_location_clus_offset);
|
||||
finode->create_date = tmp_dEntry->DIR_CrtDate;
|
||||
@ -339,44 +330,33 @@ find_lookup_success:; // 找到目标dentry
|
||||
/**
|
||||
* @brief 创建fat32文件系统的超级块
|
||||
*
|
||||
* @param DPTE 磁盘分区表entry
|
||||
* @param DPT_type 磁盘分区表类型
|
||||
* @param buf fat32文件系统的引导扇区
|
||||
* @param blk 块设备结构体
|
||||
* @return struct vfs_superblock_t* 创建好的超级块
|
||||
*/
|
||||
struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num)
|
||||
struct vfs_superblock_t *fat32_read_superblock(struct block_device *blk)
|
||||
{
|
||||
if (DPT_type != VFS_DPT_MBR) // 暂时只支持MBR分区表
|
||||
{
|
||||
kerror("fat32_read_superblock(): Unsupported DPT!");
|
||||
return NULL;
|
||||
}
|
||||
// 读取文件系统的boot扇区
|
||||
uint8_t buf[512] = {0};
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, blk->bd_start_LBA, 1, (uint64_t)&buf);
|
||||
|
||||
// 分配超级块的空间
|
||||
struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kmalloc(sizeof(struct vfs_superblock_t), 0);
|
||||
memset(sb_ptr, 0, sizeof(struct vfs_superblock_t));
|
||||
|
||||
struct vfs_superblock_t *sb_ptr = (struct vfs_superblock_t *)kzalloc(sizeof(struct vfs_superblock_t), 0);
|
||||
blk->bd_superblock = sb_ptr;
|
||||
sb_ptr->sb_ops = &fat32_sb_ops;
|
||||
sb_ptr->private_sb_info = kmalloc(sizeof(fat32_sb_info_t), 0);
|
||||
memset(sb_ptr->private_sb_info, 0, sizeof(fat32_sb_info_t));
|
||||
sb_ptr->private_sb_info = kzalloc(sizeof(fat32_sb_info_t), 0);
|
||||
sb_ptr->blk_device = blk;
|
||||
|
||||
struct fat32_BootSector_t *fbs = (struct fat32_BootSector_t *)buf;
|
||||
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(sb_ptr->private_sb_info);
|
||||
|
||||
// MBR分区表entry
|
||||
struct MBR_disk_partition_table_entry_t *MBR_DPTE = (struct MBR_disk_partition_table_entry_t *)DPTE;
|
||||
fsbi->ahci_ctrl_num = ahci_ctrl_num;
|
||||
fsbi->ahci_port_num = ahci_port_num;
|
||||
fsbi->part_num = part_num;
|
||||
|
||||
fsbi->starting_sector = MBR_DPTE->starting_LBA;
|
||||
fsbi->sector_count = MBR_DPTE->total_sectors;
|
||||
fsbi->starting_sector = blk->bd_start_LBA;
|
||||
fsbi->sector_count = blk->bd_sectors_num;
|
||||
fsbi->sec_per_clus = fbs->BPB_SecPerClus;
|
||||
fsbi->bytes_per_clus = fbs->BPB_SecPerClus * fbs->BPB_BytesPerSec;
|
||||
fsbi->bytes_per_sec = fbs->BPB_BytesPerSec;
|
||||
fsbi->first_data_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
|
||||
fsbi->FAT1_base_sector = MBR_DPTE->starting_LBA + fbs->BPB_RsvdSecCnt;
|
||||
fsbi->first_data_sector = blk->bd_start_LBA + fbs->BPB_RsvdSecCnt + fbs->BPB_FATSz32 * fbs->BPB_NumFATs;
|
||||
fsbi->FAT1_base_sector = blk->bd_start_LBA + fbs->BPB_RsvdSecCnt;
|
||||
fsbi->FAT2_base_sector = fsbi->FAT1_base_sector + fbs->BPB_FATSz32;
|
||||
fsbi->sec_per_FAT = fbs->BPB_FATSz32;
|
||||
fsbi->NumFATs = fbs->BPB_NumFATs;
|
||||
@ -387,7 +367,8 @@ struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, voi
|
||||
|
||||
// fsinfo扇区的信息
|
||||
memset(&fsbi->fsinfo, 0, sizeof(struct fat32_FSInfo_t));
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, MBR_DPTE->starting_LBA + fbs->BPB_FSInfo, 1, (uint64_t)&fsbi->fsinfo, ahci_ctrl_num, ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, blk->bd_start_LBA + fsbi->fsinfo_sector_addr_infat, 1, (uint64_t)&fsbi->fsinfo);
|
||||
|
||||
printk_color(BLUE, BLACK, "FAT32 FSInfo\n\tFSI_LeadSig:%#018lx\n\tFSI_StrucSig:%#018lx\n\tFSI_Free_Count:%#018lx\n", fsbi->fsinfo.FSI_LeadSig, fsbi->fsinfo.FSI_StrucSig, fsbi->fsinfo.FSI_Free_Count);
|
||||
|
||||
// 初始化超级块的dir entry
|
||||
@ -476,8 +457,8 @@ void fat32_write_inode(struct vfs_index_node_t *inode)
|
||||
|
||||
struct fat32_Directory_t *buf = (struct fat32_Directory_t *)kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_clus);
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
|
||||
inode->sb->blk_device->bd_disk->fops->transfer(inode->sb->blk_device->bd_disk, AHCI_CMD_READ_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf);
|
||||
// 计算目标dEntry所在的位置
|
||||
struct fat32_Directory_t *fdEntry = buf + finode->dEntry_location_clus_offset;
|
||||
|
||||
@ -487,8 +468,7 @@ void fat32_write_inode(struct vfs_index_node_t *inode)
|
||||
fdEntry->DIR_FstClusHI = (finode->first_clus >> 16) | (fdEntry->DIR_FstClusHI & 0xf000);
|
||||
|
||||
// 将dir entry写回磁盘
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
|
||||
inode->sb->blk_device->bd_disk->fops->transfer(inode->sb->blk_device->bd_disk, AHCI_CMD_WRITE_DMA_EXT, fLBA, fsbi->sec_per_clus, (uint64_t)buf);
|
||||
kfree(buf);
|
||||
}
|
||||
|
||||
@ -554,6 +534,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos
|
||||
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)(file_ptr->dEntry->dir_inode->private_inode_info);
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
|
||||
struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
|
||||
|
||||
// First cluster num of the file
|
||||
uint64_t cluster = finode->first_clus;
|
||||
@ -571,7 +552,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos
|
||||
|
||||
// find the actual cluster on disk of the specified position
|
||||
for (int i = 0; i < clus_offset_in_file; ++i)
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk,fsbi, cluster);
|
||||
|
||||
// 如果需要读取的数据边界大于文件大小
|
||||
if (*position + count > file_ptr->dEntry->dir_inode->file_size)
|
||||
@ -591,7 +572,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos
|
||||
uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
|
||||
// 读取一个簇的数据
|
||||
int errno = ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
|
||||
if (errno != AHCI_SUCCESS)
|
||||
{
|
||||
kerror("FAT32 FS(read) error!");
|
||||
@ -616,7 +597,7 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos
|
||||
|
||||
*position += step_trans_len; // 更新文件指针
|
||||
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk,fsbi, cluster);
|
||||
} while (bytes_remain && (cluster < 0x0ffffff8) && cluster != 0);
|
||||
|
||||
kfree(tmp_buffer);
|
||||
@ -627,8 +608,6 @@ long fat32_read(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *pos
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 向fat32文件系统写入数据
|
||||
*
|
||||
@ -641,6 +620,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po
|
||||
{
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)(file_ptr->dEntry->dir_inode->sb->private_sb_info);
|
||||
struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
|
||||
|
||||
// First cluster num of the file
|
||||
uint32_t cluster = finode->first_clus;
|
||||
@ -661,15 +641,13 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po
|
||||
{
|
||||
// 跳转到position所在的簇
|
||||
for (uint64_t i = 0; i < clus_offset_in_file; ++i)
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk,fsbi, cluster);
|
||||
}
|
||||
// kdebug("cluster(start)=%d", cluster);
|
||||
// 没有可用的磁盘空间
|
||||
if (!cluster)
|
||||
return -ENOSPC;
|
||||
|
||||
|
||||
|
||||
int64_t bytes_remain = count;
|
||||
|
||||
if (count < 0) // 要写入的字节数小于0
|
||||
@ -687,7 +665,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po
|
||||
{
|
||||
// kdebug("read existed sec=%ld", sector);
|
||||
// 读取一个簇的数据
|
||||
int errno = ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
|
||||
if (errno != AHCI_SUCCESS)
|
||||
{
|
||||
// kerror("FAT32 FS(write) read disk error!");
|
||||
@ -709,7 +687,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po
|
||||
memcpy(tmp_buffer + bytes_offset, buf, step_trans_len);
|
||||
|
||||
// 写入数据到对应的簇
|
||||
int errno = ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
int errno = blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buffer);
|
||||
if (errno != AHCI_SUCCESS)
|
||||
{
|
||||
kerror("FAT32 FS(write) write disk error!");
|
||||
@ -726,7 +704,7 @@ long fat32_write(struct vfs_file_t *file_ptr, char *buf, int64_t count, long *po
|
||||
|
||||
int next_clus = 0;
|
||||
if (bytes_remain)
|
||||
next_clus = fat32_read_FAT_entry(fsbi, cluster);
|
||||
next_clus = fat32_read_FAT_entry(blk,fsbi, cluster);
|
||||
else
|
||||
break;
|
||||
if (next_clus >= 0x0ffffff8) // 已经到达了最后一个簇,需要分配新簇
|
||||
@ -855,6 +833,8 @@ long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t
|
||||
inode->private_inode_info = (void *)finode;
|
||||
inode->blocks = fsbi->sec_per_clus;
|
||||
|
||||
struct block_device *blk = inode->sb->blk_device;
|
||||
|
||||
// 计算总共需要多少个目录项
|
||||
uint32_t cnt_longname = (dest_dEntry->name_length + 25) / 26;
|
||||
// 默认都是创建长目录项来存储
|
||||
@ -897,7 +877,7 @@ long fat32_create(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_t
|
||||
|
||||
// ====== 将目录项写回磁盘
|
||||
// kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr);
|
||||
|
||||
// 注意:parent字段需要在调用函数的地方进行设置
|
||||
|
||||
@ -948,7 +928,6 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
// 寻找空闲目录项
|
||||
struct fat32_Directory_t *empty_fat32_dentry = fat32_find_empty_dentry(parent_inode, cnt_longname + 1, 0, &tmp_dentry_sector, &tmp_parent_dentry_clus, &tmp_dentry_clus_buf_addr);
|
||||
|
||||
|
||||
// ====== 初始化inode =======
|
||||
struct vfs_index_node_t *inode = (struct vfs_index_node_t *)kmalloc(sizeof(struct vfs_index_node_t), 0);
|
||||
memset(inode, 0, sizeof(struct vfs_index_node_t));
|
||||
@ -959,6 +938,8 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
inode->inode_ops = &fat32_inode_ops;
|
||||
inode->sb = parent_inode->sb;
|
||||
|
||||
struct block_device *blk = inode->sb->blk_device;
|
||||
|
||||
// ===== 初始化inode的文件系统私有信息 ====
|
||||
|
||||
inode->private_inode_info = (fat32_inode_info_t *)kmalloc(sizeof(fat32_inode_info_t), 0);
|
||||
@ -997,7 +978,7 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
|
||||
// ====== 将目录项写回磁盘
|
||||
// kdebug("tmp_dentry_sector=%ld", tmp_dentry_sector);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, tmp_dentry_sector, fsbi->sec_per_clus, tmp_dentry_clus_buf_addr);
|
||||
// ====== 初始化新的文件夹的目录项 =====
|
||||
{
|
||||
// kdebug("to create dot and dot dot.");
|
||||
@ -1030,7 +1011,7 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
|
||||
uint64_t sector = fsbi->first_data_sector + (new_dir_clus - 2) * fsbi->sec_per_clus;
|
||||
// kdebug("add dot and dot dot: sector=%ld", sector);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf);
|
||||
}
|
||||
|
||||
// 注意:parent字段需要在调用函数的地方进行设置
|
||||
@ -1077,8 +1058,9 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
|
||||
{
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)file_ptr->dEntry->dir_inode->private_inode_info;
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)file_ptr->dEntry->dir_inode->sb->private_sb_info;
|
||||
struct block_device *blk = file_ptr->dEntry->dir_inode->sb->blk_device;
|
||||
|
||||
unsigned char *buf = (unsigned char *)kmalloc(fsbi->bytes_per_clus, 0);
|
||||
unsigned char *buf = (unsigned char *)kzalloc(fsbi->bytes_per_clus, 0);
|
||||
uint32_t cluster = finode->first_clus;
|
||||
|
||||
// 当前文件指针所在位置的簇号(文件内偏移量)
|
||||
@ -1087,7 +1069,7 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
|
||||
// 循环读取fat entry,直到读取到文件当前位置的所在簇号
|
||||
for (int i = 0; i < clus_num; ++i)
|
||||
{
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk,fsbi, cluster);
|
||||
if (cluster > 0x0ffffff7) // 文件结尾
|
||||
{
|
||||
kerror("file position out of range! (cluster not exists)");
|
||||
@ -1105,7 +1087,8 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
|
||||
// 计算文件夹当前位置所在簇的起始扇区号
|
||||
uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
// 读取文件夹目录项当前位置起始扇区的数据
|
||||
if (AHCI_SUCCESS != ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num))
|
||||
|
||||
if (AHCI_SUCCESS != blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf))
|
||||
{
|
||||
// 读取失败
|
||||
kerror("Failed to read the file's first sector.");
|
||||
@ -1233,7 +1216,7 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
|
||||
}
|
||||
|
||||
// 当前簇不存在目录项
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
|
||||
}
|
||||
|
||||
kfree(buf);
|
||||
|
@ -128,9 +128,6 @@ struct fat32_LongDirectory_t
|
||||
struct fat32_partition_info_t
|
||||
{
|
||||
uint16_t partition_id; // 全局fat32分区id
|
||||
uint8_t ahci_ctrl_num;
|
||||
uint8_t ahci_port_num;
|
||||
uint8_t part_num; // 硬盘中的分区号
|
||||
|
||||
struct fat32_BootSector_t bootsector;
|
||||
struct fat32_FSInfo_t fsinfo;
|
||||
@ -179,12 +176,10 @@ struct vfs_superblock_t *fat32_register_partition(uint8_t ahci_ctrl_num, uint8_t
|
||||
/**
|
||||
* @brief 创建fat32文件系统的超级块
|
||||
*
|
||||
* @param DPTE 磁盘分区表entry
|
||||
* @param DPT_type 磁盘分区表类型
|
||||
* @param buf fat32文件系统的引导扇区
|
||||
* @param blk 块设备结构体
|
||||
* @return struct vfs_superblock_t* 创建好的超级块
|
||||
*/
|
||||
struct vfs_superblock_t *fat32_read_superblock(void *DPTE, uint8_t DPT_type, void *buf, int8_t ahci_ctrl_num, int8_t ahci_port_num, int8_t part_num);
|
||||
struct vfs_superblock_t *fat32_read_superblock(struct block_device* blk);
|
||||
|
||||
/**
|
||||
* @brief 创建新的文件
|
||||
|
@ -18,7 +18,7 @@ int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int
|
||||
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)inode->sb->private_sb_info;
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)inode->private_inode_info;
|
||||
|
||||
struct block_device *blk = inode->sb->blk_device;
|
||||
uint64_t sec_per_fat = fsbi->sec_per_FAT;
|
||||
|
||||
// todo: 对alloc的过程加锁
|
||||
@ -32,8 +32,7 @@ int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int
|
||||
if (clus_idx >= num_clusters)
|
||||
goto done;
|
||||
memset(buf, 0, fsbi->bytes_per_sec);
|
||||
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + i, 1, (uint64_t)buf);
|
||||
// 依次检查簇是否空闲
|
||||
for (int j = 0; j < ent_per_sec; ++j)
|
||||
{
|
||||
@ -73,7 +72,7 @@ done:;
|
||||
cluster = tmp_clus;
|
||||
while (true)
|
||||
{
|
||||
tmp_clus = fat32_read_FAT_entry(fsbi, cluster);
|
||||
tmp_clus = fat32_read_FAT_entry(blk, fsbi, cluster);
|
||||
if (tmp_clus <= 0x0ffffff7)
|
||||
cluster = tmp_clus;
|
||||
else
|
||||
@ -85,10 +84,10 @@ done:;
|
||||
for (int i = idx; i < num_clusters; ++i)
|
||||
{
|
||||
// kdebug("write cluster i=%d : cluster=%d, value= %d", i, cluster, clusters[i]);
|
||||
fat32_write_FAT_entry(fsbi, cluster, clusters[i]);
|
||||
fat32_write_FAT_entry(blk, fsbi, cluster, clusters[i]);
|
||||
cluster = clusters[i];
|
||||
}
|
||||
fat32_write_FAT_entry(fsbi, cluster, 0x0ffffff8);
|
||||
fat32_write_FAT_entry(blk, fsbi, cluster, 0x0ffffff8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -119,11 +118,12 @@ int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster)
|
||||
/**
|
||||
* @brief 读取指定簇的FAT表项
|
||||
*
|
||||
* @param blk 块设备结构体
|
||||
* @param fsbi fat32超级块私有信息结构体
|
||||
* @param cluster 指定簇
|
||||
* @return uint32_t 下一个簇的簇号
|
||||
*/
|
||||
uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
|
||||
uint32_t fat32_read_FAT_entry(struct block_device *blk, fat32_sb_info_t *fsbi, uint32_t cluster)
|
||||
{
|
||||
// 计算每个扇区内含有的FAT表项数
|
||||
// FAT每项4bytes
|
||||
@ -133,8 +133,8 @@ uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
|
||||
memset(buf, 0, fsbi->bytes_per_sec);
|
||||
|
||||
// 读取一个sector的数据,
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)&buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)&buf);
|
||||
|
||||
// 返回下一个fat表项的值(也就是下一个cluster)
|
||||
return buf[cluster & (fat_ent_per_sec - 1)] & 0x0fffffff;
|
||||
@ -143,28 +143,29 @@ uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster)
|
||||
/**
|
||||
* @brief 写入指定簇的FAT表项
|
||||
*
|
||||
* @param blk 块设备结构体
|
||||
* @param fsbi fat32超级块私有信息结构体
|
||||
* @param cluster 指定簇
|
||||
* @param value 要写入该fat表项的值
|
||||
* @return uint32_t errcode
|
||||
*/
|
||||
uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
|
||||
uint32_t fat32_write_FAT_entry(struct block_device *blk, fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value)
|
||||
{
|
||||
// 计算每个扇区内含有的FAT表项数
|
||||
// FAT每项4bytes
|
||||
uint32_t fat_ent_per_sec = (fsbi->bytes_per_sec >> 2); // 该值应为2的n次幂
|
||||
uint32_t *buf = kmalloc(fsbi->bytes_per_sec, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_sec);
|
||||
uint32_t *buf = kzalloc(fsbi->bytes_per_sec, 0);
|
||||
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf);
|
||||
|
||||
buf[cluster & (fat_ent_per_sec - 1)] = (buf[cluster & (fat_ent_per_sec - 1)] & 0xf0000000) | (value & 0x0fffffff);
|
||||
// 向FAT1和FAT2写入数据
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT1_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, fsbi->FAT2_base_sector + (cluster / fat_ent_per_sec), 1,
|
||||
(uint64_t)buf);
|
||||
|
||||
kfree(buf);
|
||||
return 0;
|
||||
}
|
||||
@ -186,8 +187,9 @@ struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *paren
|
||||
struct fat32_inode_info_t *finode = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
|
||||
fat32_sb_info_t *fsbi = (fat32_sb_info_t *)parent_inode->sb->private_sb_info;
|
||||
|
||||
uint8_t *buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(buf, 0, fsbi->bytes_per_clus);
|
||||
uint8_t *buf = kzalloc(fsbi->bytes_per_clus, 0);
|
||||
|
||||
struct block_device *blk = parent_inode->sb->blk_device;
|
||||
|
||||
// 计算父目录项的起始簇号
|
||||
uint32_t cluster = finode->first_clus;
|
||||
@ -202,7 +204,7 @@ struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *paren
|
||||
uint64_t sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
|
||||
// 读取父目录项的起始簇数据
|
||||
ahci_operation.transfer(AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_READ_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)buf);
|
||||
tmp_dEntry = (struct fat32_Directory_t *)buf;
|
||||
// 计数连续的空目录项
|
||||
uint32_t count_continuity = 0;
|
||||
@ -233,7 +235,7 @@ struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *paren
|
||||
|
||||
// 当前簇没有发现符合条件的空闲目录项,寻找下一个簇
|
||||
uint64_t old_cluster = cluster;
|
||||
cluster = fat32_read_FAT_entry(fsbi, cluster);
|
||||
cluster = fat32_read_FAT_entry(blk, fsbi, cluster);
|
||||
if (cluster >= 0x0ffffff7) // 寻找完父目录的所有簇,都没有找到符合要求的空目录项
|
||||
{
|
||||
|
||||
@ -248,9 +250,8 @@ struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *paren
|
||||
|
||||
// 将这个新的簇清空
|
||||
sector = fsbi->first_data_sector + (cluster - 2) * fsbi->sec_per_clus;
|
||||
void *tmp_buf = kmalloc(fsbi->bytes_per_clus, 0);
|
||||
memset(tmp_buf, 0, fsbi->bytes_per_clus);
|
||||
ahci_operation.transfer(AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buf, fsbi->ahci_ctrl_num, fsbi->ahci_port_num);
|
||||
void *tmp_buf = kzalloc(fsbi->bytes_per_clus, 0);
|
||||
blk->bd_disk->fops->transfer(blk->bd_disk, AHCI_CMD_WRITE_DMA_EXT, sector, fsbi->sec_per_clus, (uint64_t)tmp_buf);
|
||||
kfree(tmp_buf);
|
||||
}
|
||||
}
|
||||
|
@ -26,21 +26,23 @@ int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster);
|
||||
/**
|
||||
* @brief 读取指定簇的FAT表项
|
||||
*
|
||||
* @param blk 块设备结构体
|
||||
* @param fsbi fat32超级块私有信息结构体
|
||||
* @param cluster 指定簇
|
||||
* @return uint32_t 下一个簇的簇号
|
||||
*/
|
||||
uint32_t fat32_read_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster);
|
||||
uint32_t fat32_read_FAT_entry(struct block_device * blk, fat32_sb_info_t *fsbi, uint32_t cluster);
|
||||
|
||||
/**
|
||||
* @brief 写入指定簇的FAT表项
|
||||
*
|
||||
* @param blk 块设备结构体
|
||||
* @param fsbi fat32超级块私有信息结构体
|
||||
* @param cluster 指定簇
|
||||
* @param value 要写入该fat表项的值
|
||||
* @return uint32_t errcode
|
||||
*/
|
||||
uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value);
|
||||
uint32_t fat32_write_FAT_entry(struct block_device * blk, fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief 在父亲inode的目录项簇中,寻找连续num个空的目录项
|
||||
|
17
kernel/filesystem/rootfs/Makefile
Normal file
17
kernel/filesystem/rootfs/Makefile
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
CFLAGS += -I .
|
||||
|
||||
|
||||
kernel_fs_rootfs_objs:= $(shell find ./*.c)
|
||||
|
||||
|
||||
ECHO:
|
||||
@echo "$@"
|
||||
|
||||
|
||||
$(kernel_fs_rootfs_objs): ECHO
|
||||
gcc $(CFLAGS) -c $@ -o $@.o
|
||||
|
||||
|
||||
all: $(kernel_fs_rootfs_objs)
|
||||
|
6
kernel/filesystem/rootfs/rootfs.c
Normal file
6
kernel/filesystem/rootfs/rootfs.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include "rootfs.h"
|
||||
#include <filesystem/VFS/VFS.h>
|
||||
|
||||
void rootfs_init()
|
||||
{
|
||||
}
|
3
kernel/filesystem/rootfs/rootfs.h
Normal file
3
kernel/filesystem/rootfs/rootfs.h
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void rootfs_init();
|
@ -145,7 +145,6 @@ void system_initialize()
|
||||
// ata_init();
|
||||
pci_init();
|
||||
io_mfence();
|
||||
ahci_init();
|
||||
|
||||
// test_slab();
|
||||
// test_mm();
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <sched/sched.h>
|
||||
#include <common/unistd.h>
|
||||
#include <debug/traceback/traceback.h>
|
||||
#include <driver/disk/ahci/ahci.h>
|
||||
|
||||
#include <ktest/ktest.h>
|
||||
|
||||
@ -462,6 +463,7 @@ exec_failed:;
|
||||
ul initial_kernel_thread(ul arg)
|
||||
{
|
||||
// kinfo("initial proc running...\targ:%#018lx", arg);
|
||||
ahci_init();
|
||||
fat32_init();
|
||||
// 使用单独的内核线程来初始化usb驱动程序
|
||||
int usb_pid = kernel_thread(usb_init, 0, 0);
|
||||
@ -481,7 +483,6 @@ ul initial_kernel_thread(ul arg)
|
||||
waitpid(tpid[i], NULL, NULL);
|
||||
kinfo("All test done.");
|
||||
|
||||
|
||||
// 准备切换到用户态
|
||||
struct pt_regs *regs;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user