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