1b087521eSChiichen /// 引入Module 2*ae5ede03SLoGin use crate::{ 3*ae5ede03SLoGin driver::base::{ 4*ae5ede03SLoGin device::{mkdev, Device, DeviceNumber, IdTable, BLOCKDEVS, DEVICE_MANAGER}, 5*ae5ede03SLoGin map::{ 6*ae5ede03SLoGin DeviceStruct, DEV_MAJOR_DYN_END, DEV_MAJOR_DYN_EXT_END, DEV_MAJOR_DYN_EXT_START, 7*ae5ede03SLoGin DEV_MAJOR_HASH_SIZE, DEV_MAJOR_MAX, MINOR_MASK, 8*ae5ede03SLoGin }, 9*ae5ede03SLoGin }, 10*ae5ede03SLoGin kerror, 11*ae5ede03SLoGin syscall::SystemError, 12*ae5ede03SLoGin }; 13b087521eSChiichen use alloc::{sync::Arc, vec::Vec}; 14b087521eSChiichen use core::any::Any; 15b087521eSChiichen 16b087521eSChiichen use super::disk_info::Partition; 17b087521eSChiichen 18b087521eSChiichen /// 该文件定义了 Device 和 BlockDevice 的接口 19b087521eSChiichen /// Notice 设备错误码使用 Posix 规定的 int32_t 的错误码表示,而不是自己定义错误enum 20b087521eSChiichen 21b087521eSChiichen // 使用方法: 22b087521eSChiichen // 假设 blk_dev 是块设备 23b087521eSChiichen // <blk_dev as Device>::read_at() 调用的是Device的函数 24b087521eSChiichen // <blk_dev as BlockDevice>::read_at() 调用的是BlockDevice的函数 25b087521eSChiichen 26b087521eSChiichen /// 定义类型 27b087521eSChiichen pub type BlockId = usize; 28b087521eSChiichen 29b087521eSChiichen /// 定义常量 30b087521eSChiichen pub const BLK_SIZE_LOG2_LIMIT: u8 = 12; // 设定块设备的块大小不能超过 1 << 12. 31b087521eSChiichen /// 在DragonOS中,我们认为磁盘的每个LBA大小均为512字节。(注意,文件系统的1个扇区可能事实上是多个LBA) 32b087521eSChiichen pub const LBA_SIZE: usize = 512; 33b087521eSChiichen 34b087521eSChiichen /// @brief 块设备的迭代器 35b087521eSChiichen /// @usage 某次操作读/写块设备的[L,R]范围内的字节, 36b087521eSChiichen /// 那么可以使用此结构体进行迭代遍历,每次调用next()返回一个BlockRange 37b087521eSChiichen pub struct BlockIter { 38b087521eSChiichen pub begin: usize, // 迭代器的起始位置 -> 块设备的地址 (单位是字节) 39b087521eSChiichen pub end: usize, 40b087521eSChiichen pub blk_size_log2: u8, 41b087521eSChiichen pub multiblock: bool, // 是否启用连续整块同时遍历 42b087521eSChiichen } 43b087521eSChiichen 44b087521eSChiichen /// @brief Range搭配迭代器BlockIter使用,[L,R]区间被分割成多个小的Range 45b087521eSChiichen /// Range要么是整块,要么是一块的某一部分 46b087521eSChiichen /// 细节: range = [begin, end) 左闭右开 47b087521eSChiichen pub struct BlockRange { 48b087521eSChiichen pub lba_start: usize, // 起始块的lba_id 49b087521eSChiichen pub lba_end: usize, // 终止块的lba_id 50b087521eSChiichen pub begin: usize, // 起始位置在块内的偏移量, 如果BlockIter启用Multiblock,则是多个块的偏移量 51b087521eSChiichen pub end: usize, // 结束位置在块内的偏移量,单位是字节 52b087521eSChiichen pub blk_size_log2: u8, 53b087521eSChiichen } 54b087521eSChiichen 55b087521eSChiichen impl BlockIter { 56b087521eSChiichen #[allow(dead_code)] 57b087521eSChiichen pub fn new(start_addr: usize, end_addr: usize, blk_size_log2: u8) -> BlockIter { 58b087521eSChiichen return BlockIter { 59b087521eSChiichen begin: start_addr, 60b087521eSChiichen end: end_addr, 61b087521eSChiichen blk_size_log2: blk_size_log2, 62b087521eSChiichen multiblock: false, 63b087521eSChiichen }; 64b087521eSChiichen } 65b087521eSChiichen pub fn new_multiblock(start_addr: usize, end_addr: usize, blk_size_log2: u8) -> BlockIter { 66b087521eSChiichen return BlockIter { 67b087521eSChiichen begin: start_addr, 68b087521eSChiichen end: end_addr, 69b087521eSChiichen blk_size_log2: blk_size_log2, 70b087521eSChiichen multiblock: true, 71b087521eSChiichen }; 72b087521eSChiichen } 73b087521eSChiichen 74b087521eSChiichen /// 获取下一个整块或者不完整的块 75b087521eSChiichen pub fn next_block(&mut self) -> BlockRange { 76b087521eSChiichen let blk_size_log2 = self.blk_size_log2; 77b087521eSChiichen let blk_size = 1usize << self.blk_size_log2; 78b087521eSChiichen let lba_id = self.begin / blk_size; 79b087521eSChiichen let begin = self.begin % blk_size; 80b087521eSChiichen let end = if lba_id == self.end / blk_size { 81b087521eSChiichen self.end % blk_size 82b087521eSChiichen } else { 83b087521eSChiichen blk_size 84b087521eSChiichen }; 85b087521eSChiichen 86b087521eSChiichen self.begin += end - begin; 87b087521eSChiichen 88b087521eSChiichen return BlockRange { 89b087521eSChiichen lba_start: lba_id, 90b087521eSChiichen lba_end: lba_id + 1, 91b087521eSChiichen begin: begin, 92b087521eSChiichen end: end, 93b087521eSChiichen blk_size_log2: blk_size_log2, 94b087521eSChiichen }; 95b087521eSChiichen } 96b087521eSChiichen 97b087521eSChiichen /// 如果能返回多个连续的整块,则返回;否则调用next_block()返回不完整的块 98b087521eSChiichen pub fn next_multiblock(&mut self) -> BlockRange { 99b087521eSChiichen let blk_size_log2 = self.blk_size_log2; 100b087521eSChiichen let blk_size = 1usize << self.blk_size_log2; 101b087521eSChiichen let lba_start = self.begin / blk_size; 102b087521eSChiichen let lba_end = self.end / blk_size; 103b087521eSChiichen 104b087521eSChiichen // 如果不是整块,先返回非整块的小部分 105b087521eSChiichen if __bytes_to_lba(self.begin, blk_size) 106b087521eSChiichen != __bytes_to_lba(self.begin + blk_size - 1, blk_size) 107b087521eSChiichen || lba_start == lba_end 108b087521eSChiichen { 109b087521eSChiichen return self.next_block(); 110b087521eSChiichen } 111b087521eSChiichen 112b087521eSChiichen let begin = self.begin % blk_size; // 因为是多个整块,这里必然是0 113b087521eSChiichen let end = __lba_to_bytes(lba_end, blk_size) - self.begin; 114b087521eSChiichen 115b087521eSChiichen self.begin += end - begin; 116b087521eSChiichen 117b087521eSChiichen return BlockRange { 118b087521eSChiichen lba_start: lba_start, 119b087521eSChiichen lba_end: lba_end, 120b087521eSChiichen begin: begin, 121b087521eSChiichen end: end, 122b087521eSChiichen blk_size_log2: blk_size_log2, 123b087521eSChiichen }; 124b087521eSChiichen } 125b087521eSChiichen } 126b087521eSChiichen 127b087521eSChiichen /// BlockIter 函数实现 128b087521eSChiichen impl Iterator for BlockIter { 129b087521eSChiichen type Item = BlockRange; 130b087521eSChiichen 131b087521eSChiichen fn next(&mut self) -> Option<<Self as Iterator>::Item> { 132b087521eSChiichen if self.begin >= self.end { 133b087521eSChiichen return None; 134b087521eSChiichen } 135b087521eSChiichen if self.multiblock { 136b087521eSChiichen return Some(self.next_multiblock()); 137b087521eSChiichen } else { 138b087521eSChiichen return Some(self.next_block()); 139b087521eSChiichen } 140b087521eSChiichen } 141b087521eSChiichen } 142b087521eSChiichen 143b087521eSChiichen /// BlockRange 函数实现 144b087521eSChiichen impl BlockRange { 145b087521eSChiichen #[allow(dead_code)] 146b087521eSChiichen pub fn is_empty(&self) -> bool { 147b087521eSChiichen return self.end == self.begin; 148b087521eSChiichen } 149b087521eSChiichen pub fn len(&self) -> usize { 150b087521eSChiichen return self.end - self.begin; 151b087521eSChiichen } 152b087521eSChiichen /// 判断是不是整块 153b087521eSChiichen pub fn is_full(&self) -> bool { 154b087521eSChiichen return self.len() == (1usize << self.blk_size_log2); 155b087521eSChiichen } 156b087521eSChiichen /// 判断是不是多个整块连在一起 157b087521eSChiichen pub fn is_multi(&self) -> bool { 158b087521eSChiichen return self.len() >= (1usize << self.blk_size_log2) 159b087521eSChiichen && (self.len() % (1usize << self.blk_size_log2) == 0); 160b087521eSChiichen } 161b087521eSChiichen /// 获取 BlockRange 在块设备内部的起始位置 (单位是字节) 162b087521eSChiichen pub fn origin_begin(&self) -> usize { 163b087521eSChiichen return (self.lba_start << self.blk_size_log2) + self.begin; 164b087521eSChiichen } 165b087521eSChiichen /// 获取 BlockRange 在块设备内部的结尾位置 (单位是字节) 166b087521eSChiichen pub fn origin_end(&self) -> usize { 167b087521eSChiichen return (self.lba_start << self.blk_size_log2) + self.end; 168b087521eSChiichen } 169b087521eSChiichen } 170b087521eSChiichen 171b087521eSChiichen /// 从字节地址转换到lba id 172b087521eSChiichen #[inline] 173b087521eSChiichen pub fn __bytes_to_lba(addr: usize, blk_size: usize) -> BlockId { 174b087521eSChiichen return addr / blk_size; 175b087521eSChiichen } 176b087521eSChiichen 177b087521eSChiichen /// 从lba id转换到字节地址, 返回lba_id的最左侧字节 178b087521eSChiichen #[inline] 179b087521eSChiichen pub fn __lba_to_bytes(lba_id: usize, blk_size: usize) -> BlockId { 180b087521eSChiichen return lba_id * blk_size; 181b087521eSChiichen } 182b087521eSChiichen 183b087521eSChiichen /// @brief 块设备应该实现的操作 184b087521eSChiichen pub trait BlockDevice: Device { 185b087521eSChiichen /// @brief: 在块设备中,从第lba_id_start个块开始,读取count个块数据,存放到buf中 186b087521eSChiichen /// 187b087521eSChiichen /// @parameter lba_id_start: 起始块 188b087521eSChiichen /// @parameter count: 读取块的数量 189b087521eSChiichen /// @parameter buf: 目标数组 190b087521eSChiichen /// @return: 如果操作成功,返回 Ok(操作的长度) 其中单位是字节; 191b087521eSChiichen /// 否则返回Err(错误码),其中错误码为负数; 192b087521eSChiichen /// 如果操作异常,但是并没有检查出什么错误,将返回Err(已操作的长度) 193b087521eSChiichen fn read_at( 194b087521eSChiichen &self, 195b087521eSChiichen lba_id_start: BlockId, 196b087521eSChiichen count: usize, 197b087521eSChiichen buf: &mut [u8], 198b087521eSChiichen ) -> Result<usize, SystemError>; 199b087521eSChiichen 200b087521eSChiichen /// @brief: 在块设备中,从第lba_id_start个块开始,把buf中的count个块数据,存放到设备中 201b087521eSChiichen /// @parameter lba_id_start: 起始块 202b087521eSChiichen /// @parameter count: 写入块的数量 203b087521eSChiichen /// @parameter buf: 目标数组 204b087521eSChiichen /// @return: 如果操作成功,返回 Ok(操作的长度) 其中单位是字节; 205b087521eSChiichen /// 否则返回Err(错误码),其中错误码为负数; 206b087521eSChiichen /// 如果操作异常,但是并没有检查出什么错误,将返回Err(已操作的长度) 207b087521eSChiichen fn write_at( 208b087521eSChiichen &self, 209b087521eSChiichen lba_id_start: BlockId, 210b087521eSChiichen count: usize, 211b087521eSChiichen buf: &[u8], 212b087521eSChiichen ) -> Result<usize, SystemError>; 213b087521eSChiichen 214b087521eSChiichen /// @brief: 同步磁盘信息,把所有的dirty数据写回硬盘 - 待实现 215b087521eSChiichen fn sync(&self) -> Result<(), SystemError>; 216b087521eSChiichen 217b087521eSChiichen /// @brief: 每个块设备都必须固定自己块大小,而且该块大小必须是2的幂次 218b087521eSChiichen /// @return: 返回一个固定量,硬编码(编程的时候固定的常量). 219b087521eSChiichen fn blk_size_log2(&self) -> u8; 220b087521eSChiichen 221b087521eSChiichen // TODO: 待实现 open, close 222b087521eSChiichen 223b087521eSChiichen /// @brief 本函数用于实现动态转换。 224b087521eSChiichen /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 225b087521eSChiichen fn as_any_ref(&self) -> &dyn Any; 226b087521eSChiichen 227b087521eSChiichen /// @brief 本函数用于将BlockDevice转换为Device。 228b087521eSChiichen /// 由于实现了BlockDevice的结构体,本身也实现了Device Trait, 因此转换是可能的。 229b087521eSChiichen /// 思路:在BlockDevice的结构体中新增一个self_ref变量,返回self_ref.upgrade()即可。 230b087521eSChiichen fn device(&self) -> Arc<dyn Device>; 231b087521eSChiichen 232b087521eSChiichen /// @brief 返回块设备的块大小(单位:字节) 233b087521eSChiichen fn block_size(&self) -> usize; 234b087521eSChiichen 235b087521eSChiichen /// @brief 返回当前磁盘上的所有分区的Arc指针数组 236b087521eSChiichen fn partitions(&self) -> Vec<Arc<Partition>>; 237b087521eSChiichen 238b087521eSChiichen fn write_at_bytes(&self, offset: usize, len: usize, buf: &[u8]) -> Result<usize, SystemError> { 239b087521eSChiichen // assert!(len <= buf.len()); 240b087521eSChiichen if len > buf.len() { 241b087521eSChiichen return Err(SystemError::E2BIG); 242b087521eSChiichen } 243b087521eSChiichen 244b087521eSChiichen let iter = BlockIter::new_multiblock(offset, offset + len, self.blk_size_log2()); 245b087521eSChiichen let multi = iter.multiblock; 246b087521eSChiichen 247b087521eSChiichen for range in iter { 248b087521eSChiichen let buf_begin = range.origin_begin() - offset; // 本次读操作的起始位置/已经读了这么多字节 249b087521eSChiichen let buf_end = range.origin_end() - offset; 250b087521eSChiichen let buf_slice = &buf[buf_begin..buf_end]; 251b087521eSChiichen let count: usize = (range.lba_end - range.lba_start).try_into().unwrap(); 252b087521eSChiichen let full = multi && range.is_multi() || !multi && range.is_full(); 253b087521eSChiichen 254b087521eSChiichen if full { 255b087521eSChiichen self.write_at(range.lba_start, count, buf_slice)?; 256b087521eSChiichen } else { 257b087521eSChiichen if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT { 258b087521eSChiichen return Err(SystemError::E2BIG); 259b087521eSChiichen } 260b087521eSChiichen 261b087521eSChiichen let mut temp = Vec::new(); 262b087521eSChiichen temp.resize(1usize << self.blk_size_log2(), 0); 263b087521eSChiichen // 由于块设备每次读写都是整块的,在不完整写入之前,必须把不完整的地方补全 264b087521eSChiichen self.write_at(range.lba_start, 1, &mut temp[..])?; 265b087521eSChiichen // 把数据从临时buffer复制到目标buffer 266b087521eSChiichen temp[range.begin..range.end].copy_from_slice(&buf_slice); 267b087521eSChiichen self.write_at(range.lba_start, 1, &temp[..])?; 268b087521eSChiichen } 269b087521eSChiichen } 270b087521eSChiichen return Ok(len); 271b087521eSChiichen //self.0.lock().write_at(lba_id_start, count, buf) 272b087521eSChiichen } 273b087521eSChiichen 274b087521eSChiichen fn read_at_bytes( 275b087521eSChiichen &self, 276b087521eSChiichen offset: usize, 277b087521eSChiichen len: usize, 278b087521eSChiichen buf: &mut [u8], 279b087521eSChiichen ) -> Result<usize, SystemError> { 280b087521eSChiichen if len > buf.len() { 281b087521eSChiichen return Err(SystemError::E2BIG); 282b087521eSChiichen } 283b087521eSChiichen 284b087521eSChiichen let iter = BlockIter::new_multiblock(offset, offset + len, self.blk_size_log2()); 285b087521eSChiichen let multi = iter.multiblock; 286b087521eSChiichen 287b087521eSChiichen // 枚举每一个range 288b087521eSChiichen for range in iter { 289b087521eSChiichen let buf_begin = range.origin_begin() - offset; // 本次读操作的起始位置/已经读了这么多字节 290b087521eSChiichen let buf_end = range.origin_end() - offset; 291b087521eSChiichen let buf_slice = &mut buf[buf_begin..buf_end]; 292b087521eSChiichen let count: usize = (range.lba_end - range.lba_start).try_into().unwrap(); 293b087521eSChiichen let full = multi && range.is_multi() || !multi && range.is_full(); 294b087521eSChiichen 295b087521eSChiichen if full { 296b087521eSChiichen // 调用 BlockDevice::read_at() 直接把引用传进去,不是把整个数组move进去 297b087521eSChiichen self.read_at(range.lba_start, count, buf)?; 298b087521eSChiichen } else { 299b087521eSChiichen // 判断块的长度不能超过最大值 300b087521eSChiichen if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT { 301b087521eSChiichen return Err(SystemError::E2BIG); 302b087521eSChiichen } 303b087521eSChiichen 304b087521eSChiichen let mut temp = Vec::new(); 305b087521eSChiichen temp.resize(1usize << self.blk_size_log2(), 0); 306b087521eSChiichen self.read_at(range.lba_start, 1, &mut temp[..])?; 307b087521eSChiichen 308b087521eSChiichen // 把数据从临时buffer复制到目标buffer 309b087521eSChiichen buf_slice.copy_from_slice(&temp[range.begin..range.end]); 310b087521eSChiichen } 311b087521eSChiichen } 312b087521eSChiichen return Ok(len); 313b087521eSChiichen 314b087521eSChiichen // kdebug!( 315b087521eSChiichen // "ahci read at {lba_id_start}, count={count}, lock={:?}", 316b087521eSChiichen // self.0 317b087521eSChiichen // ); 318b087521eSChiichen } 319b087521eSChiichen } 320*ae5ede03SLoGin 321*ae5ede03SLoGin /// @brief 块设备框架函数集 322*ae5ede03SLoGin pub struct BlockDeviceOps; 323*ae5ede03SLoGin 324*ae5ede03SLoGin impl BlockDeviceOps { 325*ae5ede03SLoGin /// @brief: 主设备号转下标 326*ae5ede03SLoGin /// @parameter: major: 主设备号 327*ae5ede03SLoGin /// @return: 返回下标 328*ae5ede03SLoGin #[allow(dead_code)] 329*ae5ede03SLoGin fn major_to_index(major: usize) -> usize { 330*ae5ede03SLoGin return major % DEV_MAJOR_HASH_SIZE; 331*ae5ede03SLoGin } 332*ae5ede03SLoGin 333*ae5ede03SLoGin /// @brief: 动态获取主设备号 334*ae5ede03SLoGin /// @parameter: None 335*ae5ede03SLoGin /// @return: 如果成功,返回主设备号,否则,返回错误码 336*ae5ede03SLoGin #[allow(dead_code)] 337*ae5ede03SLoGin fn find_dynamic_major() -> Result<usize, SystemError> { 338*ae5ede03SLoGin let blockdevs = BLOCKDEVS.lock(); 339*ae5ede03SLoGin // 寻找主设备号为234~255的设备 340*ae5ede03SLoGin for index in (DEV_MAJOR_DYN_END..DEV_MAJOR_HASH_SIZE).rev() { 341*ae5ede03SLoGin if let Some(item) = blockdevs.get(index) { 342*ae5ede03SLoGin if item.is_empty() { 343*ae5ede03SLoGin return Ok(index); // 返回可用的主设备号 344*ae5ede03SLoGin } 345*ae5ede03SLoGin } 346*ae5ede03SLoGin } 347*ae5ede03SLoGin // 寻找主设备号在384~511的设备 348*ae5ede03SLoGin for index in (DEV_MAJOR_DYN_EXT_END + 1..DEV_MAJOR_DYN_EXT_START + 1).rev() { 349*ae5ede03SLoGin if let Some(blockdevss) = blockdevs.get(Self::major_to_index(index)) { 350*ae5ede03SLoGin let mut flag = true; 351*ae5ede03SLoGin for item in blockdevss { 352*ae5ede03SLoGin if item.device_number().major() == index { 353*ae5ede03SLoGin flag = false; 354*ae5ede03SLoGin break; 355*ae5ede03SLoGin } 356*ae5ede03SLoGin } 357*ae5ede03SLoGin if flag { 358*ae5ede03SLoGin // 如果数组中不存在主设备号等于index的设备 359*ae5ede03SLoGin return Ok(index); // 返回可用的主设备号 360*ae5ede03SLoGin } 361*ae5ede03SLoGin } 362*ae5ede03SLoGin } 363*ae5ede03SLoGin return Err(SystemError::EBUSY); 364*ae5ede03SLoGin } 365*ae5ede03SLoGin 366*ae5ede03SLoGin /// @brief: 注册设备号,该函数需要指定主设备号 367*ae5ede03SLoGin /// @parameter: from: 主设备号 368*ae5ede03SLoGin /// count: 次设备号数量 369*ae5ede03SLoGin /// name: 字符设备名 370*ae5ede03SLoGin /// @return: 如果注册成功,返回设备号,否则,返回错误码 371*ae5ede03SLoGin #[allow(dead_code)] 372*ae5ede03SLoGin pub fn register_blockdev_region( 373*ae5ede03SLoGin from: DeviceNumber, 374*ae5ede03SLoGin count: usize, 375*ae5ede03SLoGin name: &'static str, 376*ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 377*ae5ede03SLoGin Self::__register_blockdev_region(from, count, name) 378*ae5ede03SLoGin } 379*ae5ede03SLoGin 380*ae5ede03SLoGin /// @brief: 注册设备号,该函数自动分配主设备号 381*ae5ede03SLoGin /// @parameter: baseminor: 主设备号 382*ae5ede03SLoGin /// count: 次设备号数量 383*ae5ede03SLoGin /// name: 字符设备名 384*ae5ede03SLoGin /// @return: 如果注册成功,返回,否则,返回false 385*ae5ede03SLoGin #[allow(dead_code)] 386*ae5ede03SLoGin pub fn alloc_blockdev_region( 387*ae5ede03SLoGin baseminor: usize, 388*ae5ede03SLoGin count: usize, 389*ae5ede03SLoGin name: &'static str, 390*ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 391*ae5ede03SLoGin Self::__register_blockdev_region(mkdev(0, baseminor), count, name) 392*ae5ede03SLoGin } 393*ae5ede03SLoGin 394*ae5ede03SLoGin /// @brief: 注册设备号 395*ae5ede03SLoGin /// @parameter: device_number: 设备号,主设备号如果为0,则动态分配 396*ae5ede03SLoGin /// minorct: 次设备号数量 397*ae5ede03SLoGin /// name: 字符设备名 398*ae5ede03SLoGin /// @return: 如果注册成功,返回设备号,否则,返回错误码 399*ae5ede03SLoGin fn __register_blockdev_region( 400*ae5ede03SLoGin device_number: DeviceNumber, 401*ae5ede03SLoGin minorct: usize, 402*ae5ede03SLoGin name: &'static str, 403*ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 404*ae5ede03SLoGin let mut major = device_number.major(); 405*ae5ede03SLoGin let baseminor = device_number.minor(); 406*ae5ede03SLoGin if major >= DEV_MAJOR_MAX { 407*ae5ede03SLoGin kerror!( 408*ae5ede03SLoGin "DEV {} major requested {} is greater than the maximum {}\n", 409*ae5ede03SLoGin name, 410*ae5ede03SLoGin major, 411*ae5ede03SLoGin DEV_MAJOR_MAX - 1 412*ae5ede03SLoGin ); 413*ae5ede03SLoGin } 414*ae5ede03SLoGin if minorct > MINOR_MASK + 1 - baseminor { 415*ae5ede03SLoGin kerror!("DEV {} minor range requested ({}-{}) is out of range of maximum range ({}-{}) for a single major\n", 416*ae5ede03SLoGin name, baseminor, baseminor + minorct - 1, 0, MINOR_MASK); 417*ae5ede03SLoGin } 418*ae5ede03SLoGin let blockdev = DeviceStruct::new(mkdev(major, baseminor), minorct, name); 419*ae5ede03SLoGin if major == 0 { 420*ae5ede03SLoGin // 如果主设备号为0,则自动分配主设备号 421*ae5ede03SLoGin major = Self::find_dynamic_major().expect("Find synamic major error.\n"); 422*ae5ede03SLoGin } 423*ae5ede03SLoGin if let Some(items) = BLOCKDEVS.lock().get_mut(Self::major_to_index(major)) { 424*ae5ede03SLoGin let mut insert_index: usize = 0; 425*ae5ede03SLoGin for (index, item) in items.iter().enumerate() { 426*ae5ede03SLoGin insert_index = index; 427*ae5ede03SLoGin match item.device_number().major().cmp(&major) { 428*ae5ede03SLoGin core::cmp::Ordering::Less => continue, 429*ae5ede03SLoGin core::cmp::Ordering::Greater => { 430*ae5ede03SLoGin break; // 大于则向后插入 431*ae5ede03SLoGin } 432*ae5ede03SLoGin core::cmp::Ordering::Equal => { 433*ae5ede03SLoGin if item.device_number().minor() + item.minorct() <= baseminor { 434*ae5ede03SLoGin continue; // 下一个主设备号大于或者次设备号大于被插入的次设备号最大值 435*ae5ede03SLoGin } 436*ae5ede03SLoGin if item.base_minor() >= baseminor + minorct { 437*ae5ede03SLoGin break; // 在此处插入 438*ae5ede03SLoGin } 439*ae5ede03SLoGin return Err(SystemError::EBUSY); // 存在重合的次设备号 440*ae5ede03SLoGin } 441*ae5ede03SLoGin } 442*ae5ede03SLoGin } 443*ae5ede03SLoGin items.insert(insert_index, blockdev); 444*ae5ede03SLoGin } 445*ae5ede03SLoGin return Ok(mkdev(major, baseminor)); 446*ae5ede03SLoGin } 447*ae5ede03SLoGin 448*ae5ede03SLoGin /// @brief: 注销设备号 449*ae5ede03SLoGin /// @parameter: major: 主设备号,如果为0,动态分配 450*ae5ede03SLoGin /// baseminor: 起始次设备号 451*ae5ede03SLoGin /// minorct: 次设备号数量 452*ae5ede03SLoGin /// @return: 如果注销成功,返回(),否则,返回错误码 453*ae5ede03SLoGin fn __unregister_blockdev_region( 454*ae5ede03SLoGin device_number: DeviceNumber, 455*ae5ede03SLoGin minorct: usize, 456*ae5ede03SLoGin ) -> Result<(), SystemError> { 457*ae5ede03SLoGin if let Some(items) = BLOCKDEVS 458*ae5ede03SLoGin .lock() 459*ae5ede03SLoGin .get_mut(Self::major_to_index(device_number.major())) 460*ae5ede03SLoGin { 461*ae5ede03SLoGin for (index, item) in items.iter().enumerate() { 462*ae5ede03SLoGin if item.device_number() == device_number && item.minorct() == minorct { 463*ae5ede03SLoGin // 设备号和数量都相等 464*ae5ede03SLoGin items.remove(index); 465*ae5ede03SLoGin return Ok(()); 466*ae5ede03SLoGin } 467*ae5ede03SLoGin } 468*ae5ede03SLoGin } 469*ae5ede03SLoGin return Err(SystemError::EBUSY); 470*ae5ede03SLoGin } 471*ae5ede03SLoGin 472*ae5ede03SLoGin /// @brief: 块设备注册 473*ae5ede03SLoGin /// @parameter: cdev: 字符设备实例 474*ae5ede03SLoGin /// dev_t: 字符设备号 475*ae5ede03SLoGin /// range: 次设备号范围 476*ae5ede03SLoGin /// @return: none 477*ae5ede03SLoGin #[allow(dead_code)] 478*ae5ede03SLoGin pub fn bdev_add(bdev: Arc<dyn BlockDevice>, id_table: IdTable) { 479*ae5ede03SLoGin if Into::<usize>::into(id_table.device_number()) == 0 { 480*ae5ede03SLoGin kerror!("Device number can't be 0!\n"); 481*ae5ede03SLoGin } 482*ae5ede03SLoGin DEVICE_MANAGER.add_device(id_table, bdev.device()) 483*ae5ede03SLoGin } 484*ae5ede03SLoGin 485*ae5ede03SLoGin /// @brief: block设备注销 486*ae5ede03SLoGin /// @parameter: dev_t: 字符设备号 487*ae5ede03SLoGin /// range: 次设备号范围 488*ae5ede03SLoGin /// @return: none 489*ae5ede03SLoGin #[allow(dead_code)] 490*ae5ede03SLoGin pub fn bdev_del(_devnum: DeviceNumber, _range: usize) { 491*ae5ede03SLoGin unimplemented!(); 492*ae5ede03SLoGin } 493*ae5ede03SLoGin } 494