1004e86ffSlogin /// 导出devfs的模块 2004e86ffSlogin pub mod null_dev; 3004e86ffSlogin pub mod zero_dev; 406b09f34Skong 5004e86ffSlogin use super::vfs::{ 6004e86ffSlogin core::{generate_inode_id, ROOT_INODE}, 773c607aaSYJwu2023 file::FileMode, 86b4e7a29SLoGin syscall::ModeType, 9597ecc08STTaq FilePrivateData, FileSystem, FileType, FsInfo, IndexNode, Magic, Metadata, SuperBlock, 10004e86ffSlogin }; 11004e86ffSlogin use crate::{ 1202343d0bSLoGin driver::base::device::device_number::DeviceNumber, 13b087521eSChiichen kerror, kinfo, 14b087521eSChiichen libs::{ 15b087521eSChiichen once::Once, 16b087521eSChiichen spinlock::{SpinLock, SpinLockGuard}, 17b087521eSChiichen }, 1820e3152eSlogin time::TimeSpec, 19004e86ffSlogin }; 20004e86ffSlogin use alloc::{ 21004e86ffSlogin collections::BTreeMap, 22004e86ffSlogin string::{String, ToString}, 23004e86ffSlogin sync::{Arc, Weak}, 24004e86ffSlogin vec::Vec, 25004e86ffSlogin }; 2691e9d4abSLoGin use system_error::SystemError; 27004e86ffSlogin 28597ecc08STTaq const DEVFS_BLOCK_SIZE: u64 = 512; 29597ecc08STTaq const DEVFS_MAX_NAMELEN: usize = 255; 30004e86ffSlogin /// @brief dev文件系统 31004e86ffSlogin #[derive(Debug)] 32004e86ffSlogin pub struct DevFS { 33004e86ffSlogin // 文件系统根节点 34004e86ffSlogin root_inode: Arc<LockedDevFSInode>, 35597ecc08STTaq super_block: SuperBlock, 36004e86ffSlogin } 37004e86ffSlogin 38004e86ffSlogin impl FileSystem for DevFS { 39004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 40004e86ffSlogin self 41004e86ffSlogin } 42004e86ffSlogin 43004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 44004e86ffSlogin return self.root_inode.clone(); 45004e86ffSlogin } 46004e86ffSlogin 47004e86ffSlogin fn info(&self) -> super::vfs::FsInfo { 48004e86ffSlogin return FsInfo { 49004e86ffSlogin blk_dev_id: 0, 50004e86ffSlogin max_name_len: DEVFS_MAX_NAMELEN, 51004e86ffSlogin }; 52004e86ffSlogin } 531d37ca6dSDonkey Kane 541d37ca6dSDonkey Kane fn name(&self) -> &str { 551d37ca6dSDonkey Kane "devfs" 561d37ca6dSDonkey Kane } 57597ecc08STTaq 58597ecc08STTaq fn super_block(&self) -> SuperBlock { 59597ecc08STTaq self.super_block.clone() 60597ecc08STTaq } 61004e86ffSlogin } 62004e86ffSlogin 63004e86ffSlogin impl DevFS { 64004e86ffSlogin pub fn new() -> Arc<Self> { 65597ecc08STTaq let super_block = SuperBlock::new( 66597ecc08STTaq Magic::DEVFS_MAGIC, 67597ecc08STTaq DEVFS_BLOCK_SIZE, 68597ecc08STTaq DEVFS_MAX_NAMELEN as u64, 69597ecc08STTaq ); 70004e86ffSlogin // 初始化root inode 71004e86ffSlogin let root: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new( 72004e86ffSlogin // /dev 的权限设置为 读+执行,root 可以读写 73004e86ffSlogin // root 的 parent 是空指针 746b4e7a29SLoGin DevFSInode::new(FileType::Dir, ModeType::from_bits_truncate(0o755), 0), 75004e86ffSlogin ))); 76004e86ffSlogin 77597ecc08STTaq let devfs: Arc<DevFS> = Arc::new(DevFS { 78597ecc08STTaq root_inode: root, 79597ecc08STTaq super_block, 80597ecc08STTaq }); 81004e86ffSlogin 82004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 83004e86ffSlogin let mut root_guard: SpinLockGuard<DevFSInode> = devfs.root_inode.0.lock(); 84004e86ffSlogin root_guard.parent = Arc::downgrade(&devfs.root_inode); 85004e86ffSlogin root_guard.self_ref = Arc::downgrade(&devfs.root_inode); 86004e86ffSlogin root_guard.fs = Arc::downgrade(&devfs); 87004e86ffSlogin // 释放锁 88004e86ffSlogin drop(root_guard); 89004e86ffSlogin 90004e86ffSlogin // 创建文件夹 91004e86ffSlogin let root: &Arc<LockedDevFSInode> = &devfs.root_inode; 92004e86ffSlogin root.add_dir("char") 93004e86ffSlogin .expect("DevFS: Failed to create /dev/char"); 94004e86ffSlogin 95004e86ffSlogin root.add_dir("block") 96004e86ffSlogin .expect("DevFS: Failed to create /dev/block"); 97004e86ffSlogin devfs.register_bultinin_device(); 98004e86ffSlogin 9933270d00Slogin // kdebug!("ls /dev: {:?}", root.list()); 100004e86ffSlogin return devfs; 101004e86ffSlogin } 102004e86ffSlogin 103004e86ffSlogin /// @brief 注册系统内部自带的设备 104004e86ffSlogin fn register_bultinin_device(&self) { 105004e86ffSlogin use null_dev::LockedNullInode; 106004e86ffSlogin use zero_dev::LockedZeroInode; 107004e86ffSlogin let dev_root: Arc<LockedDevFSInode> = self.root_inode.clone(); 108004e86ffSlogin dev_root 109004e86ffSlogin .add_dev("null", LockedNullInode::new()) 110004e86ffSlogin .expect("DevFS: Failed to register /dev/null"); 111004e86ffSlogin dev_root 112004e86ffSlogin .add_dev("zero", LockedZeroInode::new()) 113004e86ffSlogin .expect("DevFS: Failed to register /dev/zero"); 114004e86ffSlogin } 115004e86ffSlogin 116004e86ffSlogin /// @brief 在devfs内注册设备 117004e86ffSlogin /// 118004e86ffSlogin /// @param name 设备名称 119004e86ffSlogin /// @param device 设备节点的结构体 12020e3152eSlogin pub fn register_device<T: DeviceINode>( 12120e3152eSlogin &self, 12220e3152eSlogin name: &str, 12320e3152eSlogin device: Arc<T>, 12420e3152eSlogin ) -> Result<(), SystemError> { 125004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 12620e3152eSlogin let metadata = device.metadata()?; 12720e3152eSlogin match metadata.file_type { 128004e86ffSlogin // 字节设备挂载在 /dev/char 129004e86ffSlogin FileType::CharDevice => { 130b5b571e0SLoGin if dev_root_inode.find("char").is_err() { 1316b4e7a29SLoGin dev_root_inode.create( 1326b4e7a29SLoGin "char", 1336b4e7a29SLoGin FileType::Dir, 1346b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 1356b4e7a29SLoGin )?; 136004e86ffSlogin } 137004e86ffSlogin 138004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 139004e86ffSlogin let dev_char_inode: &LockedDevFSInode = any_char_inode 140004e86ffSlogin .as_any_ref() 141004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 142004e86ffSlogin .unwrap(); 14320e3152eSlogin // 在 /dev/char 下创建设备节点 144004e86ffSlogin dev_char_inode.add_dev(name, device.clone())?; 14520e3152eSlogin 14620e3152eSlogin // 特殊处理 tty 设备,挂载在 /dev 下 14720e3152eSlogin if name.starts_with("tty") && name.len() > 3 { 14820e3152eSlogin dev_root_inode.add_dev(name, device.clone())?; 14920e3152eSlogin } 150*dfe53cf0SGnoCiYeH // ptmx设备 151*dfe53cf0SGnoCiYeH if name == "ptmx" { 152*dfe53cf0SGnoCiYeH dev_root_inode.add_dev(name, device.clone())?; 153*dfe53cf0SGnoCiYeH } 154004e86ffSlogin device.set_fs(dev_char_inode.0.lock().fs.clone()); 155004e86ffSlogin } 156004e86ffSlogin FileType::BlockDevice => { 157b5b571e0SLoGin if dev_root_inode.find("block").is_err() { 1586b4e7a29SLoGin dev_root_inode.create( 1596b4e7a29SLoGin "block", 1606b4e7a29SLoGin FileType::Dir, 1616b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 1626b4e7a29SLoGin )?; 163004e86ffSlogin } 164004e86ffSlogin 165004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 166004e86ffSlogin let dev_block_inode: &LockedDevFSInode = any_block_inode 167004e86ffSlogin .as_any_ref() 168004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 169004e86ffSlogin .unwrap(); 170004e86ffSlogin 171004e86ffSlogin dev_block_inode.add_dev(name, device.clone())?; 172004e86ffSlogin device.set_fs(dev_block_inode.0.lock().fs.clone()); 173004e86ffSlogin } 17440314b30SXiaoye Zheng FileType::KvmDevice => { 17540314b30SXiaoye Zheng dev_root_inode 17640314b30SXiaoye Zheng .add_dev(name, device.clone()) 17740314b30SXiaoye Zheng .expect("DevFS: Failed to register /dev/kvm"); 17840314b30SXiaoye Zheng } 17902343d0bSLoGin FileType::FramebufferDevice => { 18002343d0bSLoGin dev_root_inode 18102343d0bSLoGin .add_dev(name, device.clone()) 18202343d0bSLoGin .expect("DevFS: Failed to register /dev/fb"); 18302343d0bSLoGin } 184004e86ffSlogin _ => { 18579a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 186004e86ffSlogin } 187004e86ffSlogin } 188004e86ffSlogin 189004e86ffSlogin return Ok(()); 190004e86ffSlogin } 191004e86ffSlogin 192004e86ffSlogin /// @brief 卸载设备 19320e3152eSlogin pub fn unregister_device<T: DeviceINode>( 19420e3152eSlogin &self, 19520e3152eSlogin name: &str, 19620e3152eSlogin device: Arc<T>, 19720e3152eSlogin ) -> Result<(), SystemError> { 198004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 199004e86ffSlogin match device.metadata().unwrap().file_type { 200004e86ffSlogin // 字节设备挂载在 /dev/char 201004e86ffSlogin FileType::CharDevice => { 202b5b571e0SLoGin if dev_root_inode.find("char").is_err() { 203676b8ef6SMork return Err(SystemError::ENOENT); 204004e86ffSlogin } 205004e86ffSlogin 206004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 207004e86ffSlogin let dev_char_inode = any_char_inode 208004e86ffSlogin .as_any_ref() 209004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 210004e86ffSlogin .unwrap(); 211004e86ffSlogin // TODO: 调用设备的卸载接口(当引入卸载接口之后) 212004e86ffSlogin dev_char_inode.remove(name)?; 213004e86ffSlogin } 214004e86ffSlogin FileType::BlockDevice => { 215b5b571e0SLoGin if dev_root_inode.find("block").is_err() { 216676b8ef6SMork return Err(SystemError::ENOENT); 217004e86ffSlogin } 218004e86ffSlogin 219004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 220004e86ffSlogin let dev_block_inode = any_block_inode 221004e86ffSlogin .as_any_ref() 222004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 223004e86ffSlogin .unwrap(); 224004e86ffSlogin 225004e86ffSlogin dev_block_inode.remove(name)?; 226004e86ffSlogin } 227004e86ffSlogin _ => { 22879a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 229004e86ffSlogin } 230004e86ffSlogin } 231004e86ffSlogin 232004e86ffSlogin return Ok(()); 233004e86ffSlogin } 234004e86ffSlogin } 235004e86ffSlogin 236004e86ffSlogin /// @brief dev文件i节点(锁) 237004e86ffSlogin #[derive(Debug)] 238004e86ffSlogin pub struct LockedDevFSInode(SpinLock<DevFSInode>); 239004e86ffSlogin 240004e86ffSlogin /// @brief dev文件i节点(无锁) 241004e86ffSlogin #[derive(Debug)] 242004e86ffSlogin pub struct DevFSInode { 243004e86ffSlogin /// 指向父Inode的弱引用 244004e86ffSlogin parent: Weak<LockedDevFSInode>, 245004e86ffSlogin /// 指向自身的弱引用 246004e86ffSlogin self_ref: Weak<LockedDevFSInode>, 247004e86ffSlogin /// 子Inode的B树 248004e86ffSlogin children: BTreeMap<String, Arc<dyn IndexNode>>, 249004e86ffSlogin /// 指向inode所在的文件系统对象的指针 250004e86ffSlogin fs: Weak<DevFS>, 251004e86ffSlogin /// INode 元数据 252004e86ffSlogin metadata: Metadata, 253004e86ffSlogin } 254004e86ffSlogin 255004e86ffSlogin impl DevFSInode { 2566b4e7a29SLoGin pub fn new(dev_type_: FileType, mode: ModeType, data_: usize) -> Self { 2576b4e7a29SLoGin return Self::new_with_parent(Weak::default(), dev_type_, mode, data_); 258004e86ffSlogin } 259004e86ffSlogin 260004e86ffSlogin pub fn new_with_parent( 261004e86ffSlogin parent: Weak<LockedDevFSInode>, 262004e86ffSlogin dev_type_: FileType, 2636b4e7a29SLoGin mode: ModeType, 264004e86ffSlogin data_: usize, 265004e86ffSlogin ) -> Self { 266004e86ffSlogin return DevFSInode { 267b5b571e0SLoGin parent, 268004e86ffSlogin self_ref: Weak::default(), 269004e86ffSlogin children: BTreeMap::new(), 270004e86ffSlogin metadata: Metadata { 271004e86ffSlogin dev_id: 1, 272004e86ffSlogin inode_id: generate_inode_id(), 273004e86ffSlogin size: 0, 274004e86ffSlogin blk_size: 0, 275004e86ffSlogin blocks: 0, 276004e86ffSlogin atime: TimeSpec::default(), 277004e86ffSlogin mtime: TimeSpec::default(), 278004e86ffSlogin ctime: TimeSpec::default(), 279004e86ffSlogin file_type: dev_type_, // 文件夹 2806b4e7a29SLoGin mode, 281004e86ffSlogin nlinks: 1, 282004e86ffSlogin uid: 0, 283004e86ffSlogin gid: 0, 28402343d0bSLoGin raw_dev: DeviceNumber::from(data_ as u32), 285004e86ffSlogin }, 286004e86ffSlogin fs: Weak::default(), 287004e86ffSlogin }; 288004e86ffSlogin } 289004e86ffSlogin } 290004e86ffSlogin 291004e86ffSlogin impl LockedDevFSInode { 292676b8ef6SMork pub fn add_dir(&self, name: &str) -> Result<(), SystemError> { 293004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 294004e86ffSlogin 295004e86ffSlogin if guard.children.contains_key(name) { 296676b8ef6SMork return Err(SystemError::EEXIST); 297004e86ffSlogin } 298004e86ffSlogin 2996b4e7a29SLoGin match self.do_create_with_data( 3006b4e7a29SLoGin guard, 3016b4e7a29SLoGin name, 3026b4e7a29SLoGin FileType::Dir, 3036b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 3046b4e7a29SLoGin 0, 3056b4e7a29SLoGin ) { 306004e86ffSlogin Ok(inode) => inode, 307004e86ffSlogin Err(err) => { 308004e86ffSlogin return Err(err); 309004e86ffSlogin } 310004e86ffSlogin }; 311004e86ffSlogin 312004e86ffSlogin return Ok(()); 313004e86ffSlogin } 314004e86ffSlogin 315676b8ef6SMork pub fn add_dev(&self, name: &str, dev: Arc<dyn IndexNode>) -> Result<(), SystemError> { 316004e86ffSlogin let mut this = self.0.lock(); 317004e86ffSlogin 318004e86ffSlogin if this.children.contains_key(name) { 319676b8ef6SMork return Err(SystemError::EEXIST); 320004e86ffSlogin } 321004e86ffSlogin 322004e86ffSlogin this.children.insert(name.to_string(), dev); 323004e86ffSlogin return Ok(()); 324004e86ffSlogin } 325004e86ffSlogin 326676b8ef6SMork pub fn remove(&self, name: &str) -> Result<(), SystemError> { 327004e86ffSlogin let x = self 328004e86ffSlogin .0 329004e86ffSlogin .lock() 330004e86ffSlogin .children 331004e86ffSlogin .remove(name) 332676b8ef6SMork .ok_or(SystemError::ENOENT)?; 333004e86ffSlogin 334004e86ffSlogin drop(x); 335004e86ffSlogin return Ok(()); 336004e86ffSlogin } 337004e86ffSlogin 33873c607aaSYJwu2023 fn do_create_with_data( 33973c607aaSYJwu2023 &self, 34073c607aaSYJwu2023 mut guard: SpinLockGuard<DevFSInode>, 3416b4e7a29SLoGin name: &str, 3426b4e7a29SLoGin file_type: FileType, 3436b4e7a29SLoGin mode: ModeType, 3446b4e7a29SLoGin data: usize, 345676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 346004e86ffSlogin if guard.metadata.file_type != FileType::Dir { 347676b8ef6SMork return Err(SystemError::ENOTDIR); 348004e86ffSlogin } 349004e86ffSlogin 350004e86ffSlogin // 如果有重名的,则返回 3516b4e7a29SLoGin if guard.children.contains_key(name) { 352676b8ef6SMork return Err(SystemError::EEXIST); 353004e86ffSlogin } 354004e86ffSlogin 355004e86ffSlogin // 创建inode 356004e86ffSlogin let result: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new(DevFSInode { 357004e86ffSlogin parent: guard.self_ref.clone(), 358004e86ffSlogin self_ref: Weak::default(), 359004e86ffSlogin children: BTreeMap::new(), 360004e86ffSlogin metadata: Metadata { 361004e86ffSlogin dev_id: 0, 362004e86ffSlogin inode_id: generate_inode_id(), 363004e86ffSlogin size: 0, 364004e86ffSlogin blk_size: 0, 365004e86ffSlogin blocks: 0, 366004e86ffSlogin atime: TimeSpec::default(), 367004e86ffSlogin mtime: TimeSpec::default(), 368004e86ffSlogin ctime: TimeSpec::default(), 3696b4e7a29SLoGin file_type, 3706b4e7a29SLoGin mode, 371004e86ffSlogin nlinks: 1, 372004e86ffSlogin uid: 0, 373004e86ffSlogin gid: 0, 37402343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 375004e86ffSlogin }, 376004e86ffSlogin fs: guard.fs.clone(), 377004e86ffSlogin }))); 378004e86ffSlogin 379004e86ffSlogin // 初始化inode的自引用的weak指针 380004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 381004e86ffSlogin 382004e86ffSlogin // 将子inode插入父inode的B树中 3836b4e7a29SLoGin guard.children.insert(String::from(name), result.clone()); 384004e86ffSlogin return Ok(result); 385004e86ffSlogin } 386004e86ffSlogin } 387004e86ffSlogin 388004e86ffSlogin impl IndexNode for LockedDevFSInode { 389004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 390004e86ffSlogin self 391004e86ffSlogin } 392004e86ffSlogin 39320e3152eSlogin fn open( 39420e3152eSlogin &self, 395*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 39620e3152eSlogin _mode: &FileMode, 39720e3152eSlogin ) -> Result<(), SystemError> { 398004e86ffSlogin return Ok(()); 399004e86ffSlogin } 400004e86ffSlogin 401*dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 402004e86ffSlogin return Ok(()); 403004e86ffSlogin } 404004e86ffSlogin 405004e86ffSlogin fn create_with_data( 406004e86ffSlogin &self, 407004e86ffSlogin name: &str, 408004e86ffSlogin file_type: FileType, 4096b4e7a29SLoGin mode: ModeType, 410004e86ffSlogin data: usize, 411676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 412004e86ffSlogin // 获取当前inode 413004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 414004e86ffSlogin // 如果当前inode不是文件夹,则返回 415004e86ffSlogin return self.do_create_with_data(guard, name, file_type, mode, data); 416004e86ffSlogin } 417004e86ffSlogin 418676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 419004e86ffSlogin let inode = self.0.lock(); 420004e86ffSlogin 421004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 422676b8ef6SMork return Err(SystemError::ENOTDIR); 423004e86ffSlogin } 424004e86ffSlogin 425004e86ffSlogin match name { 426004e86ffSlogin "" | "." => { 427676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 428004e86ffSlogin } 429004e86ffSlogin ".." => { 430676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 431004e86ffSlogin } 432004e86ffSlogin name => { 433004e86ffSlogin // 在子目录项中查找 434676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 435004e86ffSlogin } 436004e86ffSlogin } 437004e86ffSlogin } 438004e86ffSlogin 439004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 440004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 441004e86ffSlogin } 442004e86ffSlogin 443676b8ef6SMork fn get_entry_name(&self, ino: super::vfs::InodeId) -> Result<String, SystemError> { 444004e86ffSlogin let inode: SpinLockGuard<DevFSInode> = self.0.lock(); 445004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 446676b8ef6SMork return Err(SystemError::ENOTDIR); 447004e86ffSlogin } 448004e86ffSlogin 4496b4e7a29SLoGin match ino.into() { 450004e86ffSlogin 0 => { 451004e86ffSlogin return Ok(String::from(".")); 452004e86ffSlogin } 453004e86ffSlogin 1 => { 454004e86ffSlogin return Ok(String::from("..")); 455004e86ffSlogin } 456004e86ffSlogin ino => { 457004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 458004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 459004e86ffSlogin let mut key: Vec<String> = inode 460004e86ffSlogin .children 461004e86ffSlogin .keys() 4626b4e7a29SLoGin .filter(|k| { 4636b4e7a29SLoGin inode 4646b4e7a29SLoGin .children 4656b4e7a29SLoGin .get(*k) 4666b4e7a29SLoGin .unwrap() 4676b4e7a29SLoGin .metadata() 4686b4e7a29SLoGin .unwrap() 4696b4e7a29SLoGin .inode_id 4706b4e7a29SLoGin .into() 4716b4e7a29SLoGin == ino 4726b4e7a29SLoGin }) 473004e86ffSlogin .cloned() 474004e86ffSlogin .collect(); 475004e86ffSlogin 476004e86ffSlogin match key.len() { 477676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 478004e86ffSlogin 1=>{return Ok(key.remove(0));} 4796b4e7a29SLoGin _ => panic!("Devfs get_entry_name: key.len()={key_len}>1, current inode_id={inode_id:?}, to find={to_find:?}", key_len=key.len(), inode_id = inode.metadata.inode_id, to_find=ino) 480004e86ffSlogin } 481004e86ffSlogin } 482004e86ffSlogin } 483004e86ffSlogin } 484004e86ffSlogin 48552da9a59SGnoCiYeH fn ioctl( 48652da9a59SGnoCiYeH &self, 48752da9a59SGnoCiYeH _cmd: u32, 48852da9a59SGnoCiYeH _data: usize, 48952da9a59SGnoCiYeH _private_data: &FilePrivateData, 49052da9a59SGnoCiYeH ) -> Result<usize, SystemError> { 49179a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 492004e86ffSlogin } 493004e86ffSlogin 494676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 495004e86ffSlogin let info = self.metadata()?; 496004e86ffSlogin if info.file_type != FileType::Dir { 497676b8ef6SMork return Err(SystemError::ENOTDIR); 498004e86ffSlogin } 499004e86ffSlogin 500004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 501004e86ffSlogin keys.push(String::from(".")); 502004e86ffSlogin keys.push(String::from("..")); 503004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 504004e86ffSlogin 505004e86ffSlogin return Ok(keys); 506004e86ffSlogin } 507004e86ffSlogin 508676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 509004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 510004e86ffSlogin } 511004e86ffSlogin 512676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 513004e86ffSlogin let mut inode = self.0.lock(); 514004e86ffSlogin inode.metadata.atime = metadata.atime; 515004e86ffSlogin inode.metadata.mtime = metadata.mtime; 516004e86ffSlogin inode.metadata.ctime = metadata.ctime; 517004e86ffSlogin inode.metadata.mode = metadata.mode; 518004e86ffSlogin inode.metadata.uid = metadata.uid; 519004e86ffSlogin inode.metadata.gid = metadata.gid; 520004e86ffSlogin 521004e86ffSlogin return Ok(()); 522004e86ffSlogin } 523004e86ffSlogin 524004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 525004e86ffSlogin fn read_at( 526004e86ffSlogin &self, 527004e86ffSlogin _offset: usize, 528004e86ffSlogin _len: usize, 529004e86ffSlogin _buf: &mut [u8], 530*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 531676b8ef6SMork ) -> Result<usize, SystemError> { 53220e3152eSlogin kerror!("DevFS: read_at is not supported!"); 53379a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 534004e86ffSlogin } 535004e86ffSlogin 536004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 537004e86ffSlogin fn write_at( 538004e86ffSlogin &self, 539004e86ffSlogin _offset: usize, 540004e86ffSlogin _len: usize, 541004e86ffSlogin _buf: &[u8], 542*dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 543676b8ef6SMork ) -> Result<usize, SystemError> { 54479a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 545004e86ffSlogin } 546004e86ffSlogin } 547004e86ffSlogin 548004e86ffSlogin /// @brief 所有的设备INode都需要额外实现这个trait 549004e86ffSlogin pub trait DeviceINode: IndexNode { 550004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>); 551004e86ffSlogin // TODO: 增加 unregister 方法 552004e86ffSlogin } 553004e86ffSlogin 554004e86ffSlogin /// @brief 获取devfs实例的强类型不可变引用 555004e86ffSlogin macro_rules! devfs_exact_ref { 556004e86ffSlogin () => {{ 557676b8ef6SMork let devfs_inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().find("dev"); 558004e86ffSlogin if let Err(e) = devfs_inode { 559676b8ef6SMork kerror!("failed to get DevFS ref. errcode = {:?}", e); 560676b8ef6SMork return Err(SystemError::ENOENT); 561004e86ffSlogin } 562004e86ffSlogin 563004e86ffSlogin let binding = devfs_inode.unwrap(); 564004e86ffSlogin let devfs_inode: &LockedDevFSInode = binding 565004e86ffSlogin .as_any_ref() 566004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 567004e86ffSlogin .unwrap(); 568004e86ffSlogin let binding = devfs_inode.fs(); 569004e86ffSlogin binding 570004e86ffSlogin } 571004e86ffSlogin .as_any_ref() 572004e86ffSlogin .downcast_ref::<DevFS>() 573004e86ffSlogin .unwrap()}; 574004e86ffSlogin } 575004e86ffSlogin /// @brief devfs的设备注册函数 576676b8ef6SMork pub fn devfs_register<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 577004e86ffSlogin return devfs_exact_ref!().register_device(name, device); 578004e86ffSlogin } 579004e86ffSlogin 580004e86ffSlogin /// @brief devfs的设备卸载函数 581004e86ffSlogin #[allow(dead_code)] 582676b8ef6SMork pub fn devfs_unregister<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 583004e86ffSlogin return devfs_exact_ref!().unregister_device(name, device); 584004e86ffSlogin } 585b087521eSChiichen 586b087521eSChiichen pub fn devfs_init() -> Result<(), SystemError> { 587b087521eSChiichen static INIT: Once = Once::new(); 588b087521eSChiichen let mut result = None; 589b087521eSChiichen INIT.call_once(|| { 59034e6d6c8Syuyi2439 kinfo!("Initializing DevFS..."); 591b087521eSChiichen // 创建 devfs 实例 592b087521eSChiichen let devfs: Arc<DevFS> = DevFS::new(); 593b087521eSChiichen // devfs 挂载 594b087521eSChiichen let _t = ROOT_INODE() 595b087521eSChiichen .find("dev") 596b087521eSChiichen .expect("Cannot find /dev") 597b087521eSChiichen .mount(devfs) 598b087521eSChiichen .expect("Failed to mount devfs"); 599b087521eSChiichen kinfo!("DevFS mounted."); 600b087521eSChiichen result = Some(Ok(())); 601b087521eSChiichen }); 602b087521eSChiichen 603b087521eSChiichen return result.unwrap(); 604b087521eSChiichen } 605