1b087521eSChiichen /// 引入Module 2ae5ede03SLoGin use crate::{ 3ae5ede03SLoGin driver::base::{ 406d5e247SLoGin device::{mkdev, Device, DeviceError, DeviceNumber, IdTable, BLOCKDEVS}, 5ae5ede03SLoGin map::{ 6ae5ede03SLoGin DeviceStruct, DEV_MAJOR_DYN_END, DEV_MAJOR_DYN_EXT_END, DEV_MAJOR_DYN_EXT_START, 7ae5ede03SLoGin DEV_MAJOR_HASH_SIZE, DEV_MAJOR_MAX, MINOR_MASK, 8ae5ede03SLoGin }, 9ae5ede03SLoGin }, 10ae5ede03SLoGin kerror, 11ae5ede03SLoGin syscall::SystemError, 12ae5ede03SLoGin }; 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 295*1effcfe5SGnoCiYeH // 读取整个block作为有效数据 296b087521eSChiichen if full { 297b087521eSChiichen // 调用 BlockDevice::read_at() 直接把引用传进去,不是把整个数组move进去 298*1effcfe5SGnoCiYeH self.read_at(range.lba_start, count, buf_slice)?; 299b087521eSChiichen } else { 300b087521eSChiichen // 判断块的长度不能超过最大值 301b087521eSChiichen if self.blk_size_log2() > BLK_SIZE_LOG2_LIMIT { 302b087521eSChiichen return Err(SystemError::E2BIG); 303b087521eSChiichen } 304b087521eSChiichen 305b087521eSChiichen let mut temp = Vec::new(); 306b087521eSChiichen temp.resize(1usize << self.blk_size_log2(), 0); 307b087521eSChiichen self.read_at(range.lba_start, 1, &mut temp[..])?; 308b087521eSChiichen 309b087521eSChiichen // 把数据从临时buffer复制到目标buffer 310b087521eSChiichen buf_slice.copy_from_slice(&temp[range.begin..range.end]); 311b087521eSChiichen } 312b087521eSChiichen } 313b087521eSChiichen return Ok(len); 314b087521eSChiichen 315b087521eSChiichen // kdebug!( 316b087521eSChiichen // "ahci read at {lba_id_start}, count={count}, lock={:?}", 317b087521eSChiichen // self.0 318b087521eSChiichen // ); 319b087521eSChiichen } 320b087521eSChiichen } 321ae5ede03SLoGin 322ae5ede03SLoGin /// @brief 块设备框架函数集 323ae5ede03SLoGin pub struct BlockDeviceOps; 324ae5ede03SLoGin 325ae5ede03SLoGin impl BlockDeviceOps { 326ae5ede03SLoGin /// @brief: 主设备号转下标 327ae5ede03SLoGin /// @parameter: major: 主设备号 328ae5ede03SLoGin /// @return: 返回下标 329ae5ede03SLoGin #[allow(dead_code)] 330ae5ede03SLoGin fn major_to_index(major: usize) -> usize { 331ae5ede03SLoGin return major % DEV_MAJOR_HASH_SIZE; 332ae5ede03SLoGin } 333ae5ede03SLoGin 334ae5ede03SLoGin /// @brief: 动态获取主设备号 335ae5ede03SLoGin /// @parameter: None 336ae5ede03SLoGin /// @return: 如果成功,返回主设备号,否则,返回错误码 337ae5ede03SLoGin #[allow(dead_code)] 338ae5ede03SLoGin fn find_dynamic_major() -> Result<usize, SystemError> { 339ae5ede03SLoGin let blockdevs = BLOCKDEVS.lock(); 340ae5ede03SLoGin // 寻找主设备号为234~255的设备 341ae5ede03SLoGin for index in (DEV_MAJOR_DYN_END..DEV_MAJOR_HASH_SIZE).rev() { 342ae5ede03SLoGin if let Some(item) = blockdevs.get(index) { 343ae5ede03SLoGin if item.is_empty() { 344ae5ede03SLoGin return Ok(index); // 返回可用的主设备号 345ae5ede03SLoGin } 346ae5ede03SLoGin } 347ae5ede03SLoGin } 348ae5ede03SLoGin // 寻找主设备号在384~511的设备 349ae5ede03SLoGin for index in (DEV_MAJOR_DYN_EXT_END + 1..DEV_MAJOR_DYN_EXT_START + 1).rev() { 350ae5ede03SLoGin if let Some(blockdevss) = blockdevs.get(Self::major_to_index(index)) { 351ae5ede03SLoGin let mut flag = true; 352ae5ede03SLoGin for item in blockdevss { 353ae5ede03SLoGin if item.device_number().major() == index { 354ae5ede03SLoGin flag = false; 355ae5ede03SLoGin break; 356ae5ede03SLoGin } 357ae5ede03SLoGin } 358ae5ede03SLoGin if flag { 359ae5ede03SLoGin // 如果数组中不存在主设备号等于index的设备 360ae5ede03SLoGin return Ok(index); // 返回可用的主设备号 361ae5ede03SLoGin } 362ae5ede03SLoGin } 363ae5ede03SLoGin } 364ae5ede03SLoGin return Err(SystemError::EBUSY); 365ae5ede03SLoGin } 366ae5ede03SLoGin 367ae5ede03SLoGin /// @brief: 注册设备号,该函数需要指定主设备号 368ae5ede03SLoGin /// @parameter: from: 主设备号 369ae5ede03SLoGin /// count: 次设备号数量 370ae5ede03SLoGin /// name: 字符设备名 371ae5ede03SLoGin /// @return: 如果注册成功,返回设备号,否则,返回错误码 372ae5ede03SLoGin #[allow(dead_code)] 373ae5ede03SLoGin pub fn register_blockdev_region( 374ae5ede03SLoGin from: DeviceNumber, 375ae5ede03SLoGin count: usize, 376ae5ede03SLoGin name: &'static str, 377ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 378ae5ede03SLoGin Self::__register_blockdev_region(from, count, name) 379ae5ede03SLoGin } 380ae5ede03SLoGin 381ae5ede03SLoGin /// @brief: 注册设备号,该函数自动分配主设备号 382ae5ede03SLoGin /// @parameter: baseminor: 主设备号 383ae5ede03SLoGin /// count: 次设备号数量 384ae5ede03SLoGin /// name: 字符设备名 385ae5ede03SLoGin /// @return: 如果注册成功,返回,否则,返回false 386ae5ede03SLoGin #[allow(dead_code)] 387ae5ede03SLoGin pub fn alloc_blockdev_region( 388ae5ede03SLoGin baseminor: usize, 389ae5ede03SLoGin count: usize, 390ae5ede03SLoGin name: &'static str, 391ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 392ae5ede03SLoGin Self::__register_blockdev_region(mkdev(0, baseminor), count, name) 393ae5ede03SLoGin } 394ae5ede03SLoGin 395ae5ede03SLoGin /// @brief: 注册设备号 396ae5ede03SLoGin /// @parameter: device_number: 设备号,主设备号如果为0,则动态分配 397ae5ede03SLoGin /// minorct: 次设备号数量 398ae5ede03SLoGin /// name: 字符设备名 399ae5ede03SLoGin /// @return: 如果注册成功,返回设备号,否则,返回错误码 400ae5ede03SLoGin fn __register_blockdev_region( 401ae5ede03SLoGin device_number: DeviceNumber, 402ae5ede03SLoGin minorct: usize, 403ae5ede03SLoGin name: &'static str, 404ae5ede03SLoGin ) -> Result<DeviceNumber, SystemError> { 405ae5ede03SLoGin let mut major = device_number.major(); 406ae5ede03SLoGin let baseminor = device_number.minor(); 407ae5ede03SLoGin if major >= DEV_MAJOR_MAX { 408ae5ede03SLoGin kerror!( 409ae5ede03SLoGin "DEV {} major requested {} is greater than the maximum {}\n", 410ae5ede03SLoGin name, 411ae5ede03SLoGin major, 412ae5ede03SLoGin DEV_MAJOR_MAX - 1 413ae5ede03SLoGin ); 414ae5ede03SLoGin } 415ae5ede03SLoGin if minorct > MINOR_MASK + 1 - baseminor { 416ae5ede03SLoGin kerror!("DEV {} minor range requested ({}-{}) is out of range of maximum range ({}-{}) for a single major\n", 417ae5ede03SLoGin name, baseminor, baseminor + minorct - 1, 0, MINOR_MASK); 418ae5ede03SLoGin } 419ae5ede03SLoGin let blockdev = DeviceStruct::new(mkdev(major, baseminor), minorct, name); 420ae5ede03SLoGin if major == 0 { 421ae5ede03SLoGin // 如果主设备号为0,则自动分配主设备号 422ae5ede03SLoGin major = Self::find_dynamic_major().expect("Find synamic major error.\n"); 423ae5ede03SLoGin } 424ae5ede03SLoGin if let Some(items) = BLOCKDEVS.lock().get_mut(Self::major_to_index(major)) { 425ae5ede03SLoGin let mut insert_index: usize = 0; 426ae5ede03SLoGin for (index, item) in items.iter().enumerate() { 427ae5ede03SLoGin insert_index = index; 428ae5ede03SLoGin match item.device_number().major().cmp(&major) { 429ae5ede03SLoGin core::cmp::Ordering::Less => continue, 430ae5ede03SLoGin core::cmp::Ordering::Greater => { 431ae5ede03SLoGin break; // 大于则向后插入 432ae5ede03SLoGin } 433ae5ede03SLoGin core::cmp::Ordering::Equal => { 434ae5ede03SLoGin if item.device_number().minor() + item.minorct() <= baseminor { 435ae5ede03SLoGin continue; // 下一个主设备号大于或者次设备号大于被插入的次设备号最大值 436ae5ede03SLoGin } 437ae5ede03SLoGin if item.base_minor() >= baseminor + minorct { 438ae5ede03SLoGin break; // 在此处插入 439ae5ede03SLoGin } 440ae5ede03SLoGin return Err(SystemError::EBUSY); // 存在重合的次设备号 441ae5ede03SLoGin } 442ae5ede03SLoGin } 443ae5ede03SLoGin } 444ae5ede03SLoGin items.insert(insert_index, blockdev); 445ae5ede03SLoGin } 446ae5ede03SLoGin return Ok(mkdev(major, baseminor)); 447ae5ede03SLoGin } 448ae5ede03SLoGin 449ae5ede03SLoGin /// @brief: 注销设备号 450ae5ede03SLoGin /// @parameter: major: 主设备号,如果为0,动态分配 451ae5ede03SLoGin /// baseminor: 起始次设备号 452ae5ede03SLoGin /// minorct: 次设备号数量 453ae5ede03SLoGin /// @return: 如果注销成功,返回(),否则,返回错误码 454ae5ede03SLoGin fn __unregister_blockdev_region( 455ae5ede03SLoGin device_number: DeviceNumber, 456ae5ede03SLoGin minorct: usize, 457ae5ede03SLoGin ) -> Result<(), SystemError> { 458ae5ede03SLoGin if let Some(items) = BLOCKDEVS 459ae5ede03SLoGin .lock() 460ae5ede03SLoGin .get_mut(Self::major_to_index(device_number.major())) 461ae5ede03SLoGin { 462ae5ede03SLoGin for (index, item) in items.iter().enumerate() { 463ae5ede03SLoGin if item.device_number() == device_number && item.minorct() == minorct { 464ae5ede03SLoGin // 设备号和数量都相等 465ae5ede03SLoGin items.remove(index); 466ae5ede03SLoGin return Ok(()); 467ae5ede03SLoGin } 468ae5ede03SLoGin } 469ae5ede03SLoGin } 470ae5ede03SLoGin return Err(SystemError::EBUSY); 471ae5ede03SLoGin } 472ae5ede03SLoGin 473ae5ede03SLoGin /// @brief: 块设备注册 474ae5ede03SLoGin /// @parameter: cdev: 字符设备实例 475ae5ede03SLoGin /// dev_t: 字符设备号 476ae5ede03SLoGin /// range: 次设备号范围 477ae5ede03SLoGin /// @return: none 478ae5ede03SLoGin #[allow(dead_code)] 47906d5e247SLoGin pub fn bdev_add(_bdev: Arc<dyn BlockDevice>, id_table: IdTable) -> Result<(), DeviceError> { 480ae5ede03SLoGin if Into::<usize>::into(id_table.device_number()) == 0 { 481ae5ede03SLoGin kerror!("Device number can't be 0!\n"); 482ae5ede03SLoGin } 48306d5e247SLoGin todo!("bdev_add") 48406d5e247SLoGin // return device_manager().add_device(bdev.id_table(), bdev.device()); 485ae5ede03SLoGin } 486ae5ede03SLoGin 487ae5ede03SLoGin /// @brief: block设备注销 488ae5ede03SLoGin /// @parameter: dev_t: 字符设备号 489ae5ede03SLoGin /// range: 次设备号范围 490ae5ede03SLoGin /// @return: none 491ae5ede03SLoGin #[allow(dead_code)] 492ae5ede03SLoGin pub fn bdev_del(_devnum: DeviceNumber, _range: usize) { 493ae5ede03SLoGin unimplemented!(); 494ae5ede03SLoGin } 495ae5ede03SLoGin } 496