xref: /DragonOS/kernel/src/driver/base/block/disk_info.rs (revision 9fa0e95eeed8630a8a69c874090af2f10e8eee02)
1 #![allow(dead_code)]
2 use alloc::sync::{Arc, Weak};
3 use system_error::SystemError;
4 
5 use super::block_device::{BlockDevice, GeneralBlockRange};
6 
7 pub type SectorT = u64;
8 
9 /// @brief: 磁盘的分区信息 - (保留了c版本的数据信息)
10 #[derive(Debug)]
11 pub struct Partition {
12     pub start_sector: SectorT,           // 该分区的起始扇区
13     pub lba_start: u64,                  // 起始LBA号
14     pub sectors_num: u64,                // 该分区的扇区数
15     disk: Option<Weak<dyn BlockDevice>>, // 当前分区所属的磁盘
16     pub partno: u16,                     // 在磁盘上的分区号
17 }
18 
19 /// @brief: 分区信息 - 成员函数
20 impl Partition {
21     /// @brief: 为 disk new 一个分区结构体
new( start_sector: SectorT, lba_start: u64, sectors_num: u64, disk: Weak<dyn BlockDevice>, partno: u16, ) -> Arc<Self>22     pub fn new(
23         start_sector: SectorT,
24         lba_start: u64,
25         sectors_num: u64,
26         disk: Weak<dyn BlockDevice>,
27         partno: u16,
28     ) -> Arc<Self> {
29         return Arc::new(Partition {
30             start_sector,
31             lba_start,
32             sectors_num,
33             disk: Some(disk),
34             partno,
35         });
36     }
37 
new_raw(start_sector: SectorT, lba_start: u64, sectors_num: u64, partno: u16) -> Self38     pub fn new_raw(start_sector: SectorT, lba_start: u64, sectors_num: u64, partno: u16) -> Self {
39         return Partition {
40             start_sector,
41             lba_start,
42             sectors_num,
43             disk: None,
44             partno,
45         };
46     }
47 
48     /// @brief 获取当前分区所属的磁盘的Arc指针
49     #[inline]
disk(&self) -> Arc<dyn BlockDevice>50     pub fn disk(&self) -> Arc<dyn BlockDevice> {
51         return self.disk.as_ref().unwrap().upgrade().unwrap();
52     }
53 }
54 
55 impl TryInto<GeneralBlockRange> for Partition {
56     type Error = SystemError;
57 
try_into(self) -> Result<GeneralBlockRange, Self::Error>58     fn try_into(self) -> Result<GeneralBlockRange, Self::Error> {
59         if let Some(range) = GeneralBlockRange::new(
60             self.lba_start as usize,
61             (self.lba_start + self.sectors_num) as usize,
62         ) {
63             return Ok(range);
64         } else {
65             return Err(SystemError::EINVAL);
66         }
67     }
68 }
69