Browse Source

new: 增加block_device和gendisk抽象

fslongjin 2 years ago
parent
commit
9f98a07345

+ 79 - 0
kernel/common/blk_types.h

@@ -0,0 +1,79 @@
+#pragma once
+
+#include <common/glib.h>
+#include "stdint.h"
+#include <common/semaphore.h>
+#include <common/mutex.h>
+
+#define BLK_TYPE_AHCI 0
+
+#define DISK_NAME_LEN 32 // 磁盘名称的最大长度
+
+struct blk_gendisk;
+
+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 块设备请求队列内的packet
+ *
+ */
+struct block_device_request_packet
+{
+    uchar cmd;
+    uint64_t LBA_start;
+    uint32_t count;
+    uint64_t buffer_vaddr;
+
+    uint8_t device_type; // 0: ahci
+    void (*end_handler)(ul num, ul arg);
+
+    wait_queue_node_t wait_queue;
+};
+
+/**
+ * @brief 块设备的请求队列
+ *
+ */
+struct block_device_request_queue
+{
+    wait_queue_node_t wait_queue_list;
+    struct block_device_request_packet *in_service; // 正在请求的结点
+    ul request_count;
+};
+
+/**
+ * @brief 块设备结构体(对应磁盘的一个分区)
+ *
+ */
+struct block_device
+{
+    sector_t bd_start_sector;                    // 该分区的起始扇区
+    sector_t bd_sectors_num;                     // 该分区的扇区数
+    struct vfs_superblock_t *bd_superblock;      // 执行超级块的指针
+    struct blk_gendisk *bd_disk;                 // 当前分区所属的磁盘
+    struct block_device_request_queue *bd_queue; // 请求队列
+    uint16_t bd_partno;                          // 在磁盘上的分区号
+};
+
+/**
+ * @brief 磁盘设备结构体
+ *
+ */
+struct blk_gendisk
+{
+    char disk_name[DISK_NAME_LEN]; // 磁盘驱动器名称
+    uint16_t part_cnt;             // 磁盘分区计数
+    uint16_t flags;
+    struct block_device *partition;                   // 磁盘分区数组
+    const struct block_device_operation *fops;        // 磁盘操作
+    struct block_device_request_queue *request_queue; // 磁盘请求队列
+    void *private_data;
+
+    mutex_t open_mutex; // open()/close()操作的互斥锁
+};

+ 3 - 1
kernel/common/sys/types.h

@@ -13,7 +13,7 @@ typedef uint32_t gid_t;
 typedef long long ssize_t;
 
 typedef int __pid_t;
-#define pid_t __pid_t
+#define pid_t uint64_t
 typedef __SIZE_TYPE__ size_t;
 
 typedef char *caddr_t;
@@ -37,6 +37,8 @@ typedef uint32_t clock_t;
 typedef uint64_t fsblkcnt_t;
 typedef uint64_t fsfilcnt_t;
 
+typedef uint64_t sector_t;
+
 #define __socklen_t_defined
 #define __socklen_t uint32_t
 typedef __socklen_t socklen_t;

+ 1 - 1
kernel/driver/disk/ahci/ahci.h

@@ -1,6 +1,6 @@
 #pragma once
 
-#include <driver/disk/block_device.h>
+#include <common/blk_types.h>
 #include <driver/pci/pci.h>
 #include <mm/mm.h>
 

+ 0 - 42
kernel/driver/disk/block_device.h

@@ -1,42 +0,0 @@
-#pragma once
-
-#include <common/glib.h>
-#include "stdint.h"
-#include <common/semaphore.h>
-
-#define BLK_TYPE_AHCI 0
-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 块设备请求队列内的packet
- * 
- */
-struct block_device_request_packet
-{
-    uchar cmd;
-    uint64_t LBA_start;
-    uint32_t count;
-    uint64_t buffer_vaddr;
-
-    uint8_t device_type;    // 0: ahci
-    void (*end_handler)(ul num, ul arg);
-
-   wait_queue_node_t wait_queue;
-};
-
-/**
- * @brief 块设备的请求队列
- * 
- */
-struct block_device_request_queue
-{
-    wait_queue_node_t wait_queue_list;
-    struct block_device_request_packet * in_service;    // 正在请求的结点
-    ul request_count;
-};