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, 9*1074eb34SSamuel Dai utils::DName, 10597ecc08STTaq FilePrivateData, FileSystem, FileType, FsInfo, IndexNode, Magic, Metadata, SuperBlock, 11004e86ffSlogin }; 12004e86ffSlogin use crate::{ 1302343d0bSLoGin driver::base::device::device_number::DeviceNumber, 14b087521eSChiichen kerror, kinfo, 15b087521eSChiichen libs::{ 16b087521eSChiichen once::Once, 17b087521eSChiichen spinlock::{SpinLock, SpinLockGuard}, 18b087521eSChiichen }, 196fc066acSJomo time::PosixTimeSpec, 20004e86ffSlogin }; 21004e86ffSlogin use alloc::{ 22004e86ffSlogin collections::BTreeMap, 23004e86ffSlogin string::{String, ToString}, 24004e86ffSlogin sync::{Arc, Weak}, 25004e86ffSlogin vec::Vec, 26004e86ffSlogin }; 2791e9d4abSLoGin use system_error::SystemError; 28004e86ffSlogin 29597ecc08STTaq const DEVFS_BLOCK_SIZE: u64 = 512; 30597ecc08STTaq const DEVFS_MAX_NAMELEN: usize = 255; 31004e86ffSlogin /// @brief dev文件系统 32004e86ffSlogin #[derive(Debug)] 33004e86ffSlogin pub struct DevFS { 34004e86ffSlogin // 文件系统根节点 35004e86ffSlogin root_inode: Arc<LockedDevFSInode>, 36597ecc08STTaq super_block: SuperBlock, 37004e86ffSlogin } 38004e86ffSlogin 39004e86ffSlogin impl FileSystem for DevFS { 40004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 41004e86ffSlogin self 42004e86ffSlogin } 43004e86ffSlogin 44004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 45004e86ffSlogin return self.root_inode.clone(); 46004e86ffSlogin } 47004e86ffSlogin 48004e86ffSlogin fn info(&self) -> super::vfs::FsInfo { 49004e86ffSlogin return FsInfo { 50004e86ffSlogin blk_dev_id: 0, 51004e86ffSlogin max_name_len: DEVFS_MAX_NAMELEN, 52004e86ffSlogin }; 53004e86ffSlogin } 541d37ca6dSDonkey Kane 551d37ca6dSDonkey Kane fn name(&self) -> &str { 561d37ca6dSDonkey Kane "devfs" 571d37ca6dSDonkey Kane } 58597ecc08STTaq 59597ecc08STTaq fn super_block(&self) -> SuperBlock { 60597ecc08STTaq self.super_block.clone() 61597ecc08STTaq } 62004e86ffSlogin } 63004e86ffSlogin 64004e86ffSlogin impl DevFS { 65004e86ffSlogin pub fn new() -> Arc<Self> { 66597ecc08STTaq let super_block = SuperBlock::new( 67597ecc08STTaq Magic::DEVFS_MAGIC, 68597ecc08STTaq DEVFS_BLOCK_SIZE, 69597ecc08STTaq DEVFS_MAX_NAMELEN as u64, 70597ecc08STTaq ); 71004e86ffSlogin // 初始化root inode 72004e86ffSlogin let root: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new( 73004e86ffSlogin // /dev 的权限设置为 读+执行,root 可以读写 74004e86ffSlogin // root 的 parent 是空指针 756b4e7a29SLoGin DevFSInode::new(FileType::Dir, ModeType::from_bits_truncate(0o755), 0), 76004e86ffSlogin ))); 77004e86ffSlogin 78597ecc08STTaq let devfs: Arc<DevFS> = Arc::new(DevFS { 79597ecc08STTaq root_inode: root, 80597ecc08STTaq super_block, 81597ecc08STTaq }); 82004e86ffSlogin 83004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 84004e86ffSlogin let mut root_guard: SpinLockGuard<DevFSInode> = devfs.root_inode.0.lock(); 85004e86ffSlogin root_guard.parent = Arc::downgrade(&devfs.root_inode); 86004e86ffSlogin root_guard.self_ref = Arc::downgrade(&devfs.root_inode); 87004e86ffSlogin root_guard.fs = Arc::downgrade(&devfs); 88004e86ffSlogin // 释放锁 89004e86ffSlogin drop(root_guard); 90004e86ffSlogin 91004e86ffSlogin // 创建文件夹 92004e86ffSlogin let root: &Arc<LockedDevFSInode> = &devfs.root_inode; 93004e86ffSlogin root.add_dir("char") 94004e86ffSlogin .expect("DevFS: Failed to create /dev/char"); 95004e86ffSlogin 96004e86ffSlogin root.add_dir("block") 97004e86ffSlogin .expect("DevFS: Failed to create /dev/block"); 98004e86ffSlogin devfs.register_bultinin_device(); 99004e86ffSlogin 10033270d00Slogin // kdebug!("ls /dev: {:?}", root.list()); 101004e86ffSlogin return devfs; 102004e86ffSlogin } 103004e86ffSlogin 104004e86ffSlogin /// @brief 注册系统内部自带的设备 105004e86ffSlogin fn register_bultinin_device(&self) { 106004e86ffSlogin use null_dev::LockedNullInode; 107004e86ffSlogin use zero_dev::LockedZeroInode; 108004e86ffSlogin let dev_root: Arc<LockedDevFSInode> = self.root_inode.clone(); 109004e86ffSlogin dev_root 110004e86ffSlogin .add_dev("null", LockedNullInode::new()) 111004e86ffSlogin .expect("DevFS: Failed to register /dev/null"); 112004e86ffSlogin dev_root 113004e86ffSlogin .add_dev("zero", LockedZeroInode::new()) 114004e86ffSlogin .expect("DevFS: Failed to register /dev/zero"); 115004e86ffSlogin } 116004e86ffSlogin 117004e86ffSlogin /// @brief 在devfs内注册设备 118004e86ffSlogin /// 119004e86ffSlogin /// @param name 设备名称 120004e86ffSlogin /// @param device 设备节点的结构体 12120e3152eSlogin pub fn register_device<T: DeviceINode>( 12220e3152eSlogin &self, 12320e3152eSlogin name: &str, 12420e3152eSlogin device: Arc<T>, 12520e3152eSlogin ) -> Result<(), SystemError> { 126004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 12720e3152eSlogin let metadata = device.metadata()?; 12820e3152eSlogin match metadata.file_type { 129004e86ffSlogin // 字节设备挂载在 /dev/char 130004e86ffSlogin FileType::CharDevice => { 131b5b571e0SLoGin if dev_root_inode.find("char").is_err() { 1326b4e7a29SLoGin dev_root_inode.create( 1336b4e7a29SLoGin "char", 1346b4e7a29SLoGin FileType::Dir, 1356b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 1366b4e7a29SLoGin )?; 137004e86ffSlogin } 138004e86ffSlogin 139004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 140004e86ffSlogin let dev_char_inode: &LockedDevFSInode = any_char_inode 141004e86ffSlogin .as_any_ref() 142004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 143004e86ffSlogin .unwrap(); 14420e3152eSlogin // 在 /dev/char 下创建设备节点 145004e86ffSlogin dev_char_inode.add_dev(name, device.clone())?; 14620e3152eSlogin 14720e3152eSlogin // 特殊处理 tty 设备,挂载在 /dev 下 14820e3152eSlogin if name.starts_with("tty") && name.len() > 3 { 14920e3152eSlogin dev_root_inode.add_dev(name, device.clone())?; 15020e3152eSlogin } 151dfe53cf0SGnoCiYeH // ptmx设备 152dfe53cf0SGnoCiYeH if name == "ptmx" { 153dfe53cf0SGnoCiYeH dev_root_inode.add_dev(name, device.clone())?; 154dfe53cf0SGnoCiYeH } 155004e86ffSlogin device.set_fs(dev_char_inode.0.lock().fs.clone()); 156004e86ffSlogin } 157004e86ffSlogin FileType::BlockDevice => { 158b5b571e0SLoGin if dev_root_inode.find("block").is_err() { 1596b4e7a29SLoGin dev_root_inode.create( 1606b4e7a29SLoGin "block", 1616b4e7a29SLoGin FileType::Dir, 1626b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 1636b4e7a29SLoGin )?; 164004e86ffSlogin } 165004e86ffSlogin 166004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 167004e86ffSlogin let dev_block_inode: &LockedDevFSInode = any_block_inode 168004e86ffSlogin .as_any_ref() 169004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 170004e86ffSlogin .unwrap(); 171004e86ffSlogin 172004e86ffSlogin dev_block_inode.add_dev(name, device.clone())?; 173004e86ffSlogin device.set_fs(dev_block_inode.0.lock().fs.clone()); 174004e86ffSlogin } 17540314b30SXiaoye Zheng FileType::KvmDevice => { 17640314b30SXiaoye Zheng dev_root_inode 17740314b30SXiaoye Zheng .add_dev(name, device.clone()) 17840314b30SXiaoye Zheng .expect("DevFS: Failed to register /dev/kvm"); 17940314b30SXiaoye Zheng } 18002343d0bSLoGin FileType::FramebufferDevice => { 18102343d0bSLoGin dev_root_inode 18202343d0bSLoGin .add_dev(name, device.clone()) 18302343d0bSLoGin .expect("DevFS: Failed to register /dev/fb"); 18402343d0bSLoGin } 185004e86ffSlogin _ => { 186*1074eb34SSamuel Dai return Err(SystemError::ENOSYS); 187004e86ffSlogin } 188004e86ffSlogin } 189004e86ffSlogin 190004e86ffSlogin return Ok(()); 191004e86ffSlogin } 192004e86ffSlogin 193004e86ffSlogin /// @brief 卸载设备 19420e3152eSlogin pub fn unregister_device<T: DeviceINode>( 19520e3152eSlogin &self, 19620e3152eSlogin name: &str, 19720e3152eSlogin device: Arc<T>, 19820e3152eSlogin ) -> Result<(), SystemError> { 199004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 200004e86ffSlogin match device.metadata().unwrap().file_type { 201004e86ffSlogin // 字节设备挂载在 /dev/char 202004e86ffSlogin FileType::CharDevice => { 203b5b571e0SLoGin if dev_root_inode.find("char").is_err() { 204676b8ef6SMork return Err(SystemError::ENOENT); 205004e86ffSlogin } 206004e86ffSlogin 207004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 208004e86ffSlogin let dev_char_inode = any_char_inode 209004e86ffSlogin .as_any_ref() 210004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 211004e86ffSlogin .unwrap(); 212004e86ffSlogin // TODO: 调用设备的卸载接口(当引入卸载接口之后) 213004e86ffSlogin dev_char_inode.remove(name)?; 214004e86ffSlogin } 215004e86ffSlogin FileType::BlockDevice => { 216b5b571e0SLoGin if dev_root_inode.find("block").is_err() { 217676b8ef6SMork return Err(SystemError::ENOENT); 218004e86ffSlogin } 219004e86ffSlogin 220004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 221004e86ffSlogin let dev_block_inode = any_block_inode 222004e86ffSlogin .as_any_ref() 223004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 224004e86ffSlogin .unwrap(); 225004e86ffSlogin 226004e86ffSlogin dev_block_inode.remove(name)?; 227004e86ffSlogin } 228004e86ffSlogin _ => { 229*1074eb34SSamuel Dai return Err(SystemError::ENOSYS); 230004e86ffSlogin } 231004e86ffSlogin } 232004e86ffSlogin 233004e86ffSlogin return Ok(()); 234004e86ffSlogin } 235004e86ffSlogin } 236004e86ffSlogin 237004e86ffSlogin /// @brief dev文件i节点(锁) 238004e86ffSlogin #[derive(Debug)] 239004e86ffSlogin pub struct LockedDevFSInode(SpinLock<DevFSInode>); 240004e86ffSlogin 241004e86ffSlogin /// @brief dev文件i节点(无锁) 242004e86ffSlogin #[derive(Debug)] 243004e86ffSlogin pub struct DevFSInode { 244004e86ffSlogin /// 指向父Inode的弱引用 245004e86ffSlogin parent: Weak<LockedDevFSInode>, 246004e86ffSlogin /// 指向自身的弱引用 247004e86ffSlogin self_ref: Weak<LockedDevFSInode>, 248004e86ffSlogin /// 子Inode的B树 249*1074eb34SSamuel Dai children: BTreeMap<DName, Arc<dyn IndexNode>>, 250004e86ffSlogin /// 指向inode所在的文件系统对象的指针 251004e86ffSlogin fs: Weak<DevFS>, 252004e86ffSlogin /// INode 元数据 253004e86ffSlogin metadata: Metadata, 254*1074eb34SSamuel Dai /// 目录名 255*1074eb34SSamuel Dai dname: DName, 256004e86ffSlogin } 257004e86ffSlogin 258004e86ffSlogin impl DevFSInode { 2596b4e7a29SLoGin pub fn new(dev_type_: FileType, mode: ModeType, data_: usize) -> Self { 2606b4e7a29SLoGin return Self::new_with_parent(Weak::default(), dev_type_, mode, data_); 261004e86ffSlogin } 262004e86ffSlogin 263004e86ffSlogin pub fn new_with_parent( 264004e86ffSlogin parent: Weak<LockedDevFSInode>, 265004e86ffSlogin dev_type_: FileType, 2666b4e7a29SLoGin mode: ModeType, 267004e86ffSlogin data_: usize, 268004e86ffSlogin ) -> Self { 269004e86ffSlogin return DevFSInode { 270b5b571e0SLoGin parent, 271004e86ffSlogin self_ref: Weak::default(), 272004e86ffSlogin children: BTreeMap::new(), 273004e86ffSlogin metadata: Metadata { 274004e86ffSlogin dev_id: 1, 275004e86ffSlogin inode_id: generate_inode_id(), 276004e86ffSlogin size: 0, 277004e86ffSlogin blk_size: 0, 278004e86ffSlogin blocks: 0, 2796fc066acSJomo atime: PosixTimeSpec::default(), 2806fc066acSJomo mtime: PosixTimeSpec::default(), 2816fc066acSJomo ctime: PosixTimeSpec::default(), 282004e86ffSlogin file_type: dev_type_, // 文件夹 2836b4e7a29SLoGin mode, 284004e86ffSlogin nlinks: 1, 285004e86ffSlogin uid: 0, 286004e86ffSlogin gid: 0, 28702343d0bSLoGin raw_dev: DeviceNumber::from(data_ as u32), 288004e86ffSlogin }, 289004e86ffSlogin fs: Weak::default(), 290*1074eb34SSamuel Dai dname: DName::default(), 291004e86ffSlogin }; 292004e86ffSlogin } 293004e86ffSlogin } 294004e86ffSlogin 295004e86ffSlogin impl LockedDevFSInode { 296676b8ef6SMork pub fn add_dir(&self, name: &str) -> Result<(), SystemError> { 297004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 298004e86ffSlogin 299*1074eb34SSamuel Dai if guard.children.contains_key(&DName::from(name)) { 300676b8ef6SMork return Err(SystemError::EEXIST); 301004e86ffSlogin } 302004e86ffSlogin 3036b4e7a29SLoGin match self.do_create_with_data( 3046b4e7a29SLoGin guard, 3056b4e7a29SLoGin name, 3066b4e7a29SLoGin FileType::Dir, 3076b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 3086b4e7a29SLoGin 0, 3096b4e7a29SLoGin ) { 310004e86ffSlogin Ok(inode) => inode, 311004e86ffSlogin Err(err) => { 312004e86ffSlogin return Err(err); 313004e86ffSlogin } 314004e86ffSlogin }; 315004e86ffSlogin 316004e86ffSlogin return Ok(()); 317004e86ffSlogin } 318004e86ffSlogin 319676b8ef6SMork pub fn add_dev(&self, name: &str, dev: Arc<dyn IndexNode>) -> Result<(), SystemError> { 320004e86ffSlogin let mut this = self.0.lock(); 321*1074eb34SSamuel Dai let name = DName::from(name); 322*1074eb34SSamuel Dai if this.children.contains_key(&name) { 323676b8ef6SMork return Err(SystemError::EEXIST); 324004e86ffSlogin } 325004e86ffSlogin 326*1074eb34SSamuel Dai this.children.insert(name, dev); 327004e86ffSlogin return Ok(()); 328004e86ffSlogin } 329004e86ffSlogin 330676b8ef6SMork pub fn remove(&self, name: &str) -> Result<(), SystemError> { 331004e86ffSlogin let x = self 332004e86ffSlogin .0 333004e86ffSlogin .lock() 334004e86ffSlogin .children 335*1074eb34SSamuel Dai .remove(&DName::from(name)) 336676b8ef6SMork .ok_or(SystemError::ENOENT)?; 337004e86ffSlogin 338004e86ffSlogin drop(x); 339004e86ffSlogin return Ok(()); 340004e86ffSlogin } 341004e86ffSlogin 34273c607aaSYJwu2023 fn do_create_with_data( 34373c607aaSYJwu2023 &self, 34473c607aaSYJwu2023 mut guard: SpinLockGuard<DevFSInode>, 3456b4e7a29SLoGin name: &str, 3466b4e7a29SLoGin file_type: FileType, 3476b4e7a29SLoGin mode: ModeType, 3486b4e7a29SLoGin data: usize, 349676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 350004e86ffSlogin if guard.metadata.file_type != FileType::Dir { 351676b8ef6SMork return Err(SystemError::ENOTDIR); 352004e86ffSlogin } 353*1074eb34SSamuel Dai let name = DName::from(name); 354004e86ffSlogin // 如果有重名的,则返回 355*1074eb34SSamuel Dai if guard.children.contains_key(&name) { 356676b8ef6SMork return Err(SystemError::EEXIST); 357004e86ffSlogin } 358004e86ffSlogin 359004e86ffSlogin // 创建inode 360004e86ffSlogin let result: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new(DevFSInode { 361004e86ffSlogin parent: guard.self_ref.clone(), 362004e86ffSlogin self_ref: Weak::default(), 363004e86ffSlogin children: BTreeMap::new(), 364004e86ffSlogin metadata: Metadata { 365004e86ffSlogin dev_id: 0, 366004e86ffSlogin inode_id: generate_inode_id(), 367004e86ffSlogin size: 0, 368004e86ffSlogin blk_size: 0, 369004e86ffSlogin blocks: 0, 3706fc066acSJomo atime: PosixTimeSpec::default(), 3716fc066acSJomo mtime: PosixTimeSpec::default(), 3726fc066acSJomo ctime: PosixTimeSpec::default(), 3736b4e7a29SLoGin file_type, 3746b4e7a29SLoGin mode, 375004e86ffSlogin nlinks: 1, 376004e86ffSlogin uid: 0, 377004e86ffSlogin gid: 0, 37802343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 379004e86ffSlogin }, 380004e86ffSlogin fs: guard.fs.clone(), 381*1074eb34SSamuel Dai dname: name.clone(), 382004e86ffSlogin }))); 383004e86ffSlogin 384004e86ffSlogin // 初始化inode的自引用的weak指针 385004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 386004e86ffSlogin 387004e86ffSlogin // 将子inode插入父inode的B树中 388*1074eb34SSamuel Dai guard.children.insert(name, result.clone()); 389004e86ffSlogin return Ok(result); 390004e86ffSlogin } 391004e86ffSlogin } 392004e86ffSlogin 393004e86ffSlogin impl IndexNode for LockedDevFSInode { 394004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 395004e86ffSlogin self 396004e86ffSlogin } 397004e86ffSlogin 39820e3152eSlogin fn open( 39920e3152eSlogin &self, 400dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 40120e3152eSlogin _mode: &FileMode, 40220e3152eSlogin ) -> Result<(), SystemError> { 403004e86ffSlogin return Ok(()); 404004e86ffSlogin } 405004e86ffSlogin 406dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 407004e86ffSlogin return Ok(()); 408004e86ffSlogin } 409004e86ffSlogin 410004e86ffSlogin fn create_with_data( 411004e86ffSlogin &self, 412004e86ffSlogin name: &str, 413004e86ffSlogin file_type: FileType, 4146b4e7a29SLoGin mode: ModeType, 415004e86ffSlogin data: usize, 416676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 417004e86ffSlogin // 获取当前inode 418004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 419004e86ffSlogin // 如果当前inode不是文件夹,则返回 420004e86ffSlogin return self.do_create_with_data(guard, name, file_type, mode, data); 421004e86ffSlogin } 422004e86ffSlogin 423676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 424004e86ffSlogin let inode = self.0.lock(); 425004e86ffSlogin 426004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 427676b8ef6SMork return Err(SystemError::ENOTDIR); 428004e86ffSlogin } 429004e86ffSlogin 430004e86ffSlogin match name { 431004e86ffSlogin "" | "." => { 432676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 433004e86ffSlogin } 434004e86ffSlogin ".." => { 435676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 436004e86ffSlogin } 437004e86ffSlogin name => { 438004e86ffSlogin // 在子目录项中查找 439*1074eb34SSamuel Dai return Ok(inode 440*1074eb34SSamuel Dai .children 441*1074eb34SSamuel Dai .get(&DName::from(name)) 442*1074eb34SSamuel Dai .ok_or(SystemError::ENOENT)? 443*1074eb34SSamuel Dai .clone()); 444004e86ffSlogin } 445004e86ffSlogin } 446004e86ffSlogin } 447004e86ffSlogin 448004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 449004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 450004e86ffSlogin } 451004e86ffSlogin 452676b8ef6SMork fn get_entry_name(&self, ino: super::vfs::InodeId) -> Result<String, SystemError> { 453004e86ffSlogin let inode: SpinLockGuard<DevFSInode> = self.0.lock(); 454004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 455676b8ef6SMork return Err(SystemError::ENOTDIR); 456004e86ffSlogin } 457004e86ffSlogin 4586b4e7a29SLoGin match ino.into() { 459004e86ffSlogin 0 => { 460004e86ffSlogin return Ok(String::from(".")); 461004e86ffSlogin } 462004e86ffSlogin 1 => { 463004e86ffSlogin return Ok(String::from("..")); 464004e86ffSlogin } 465004e86ffSlogin ino => { 466004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 467004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 468004e86ffSlogin let mut key: Vec<String> = inode 469004e86ffSlogin .children 470*1074eb34SSamuel Dai .iter() 471*1074eb34SSamuel Dai .filter_map(|(k, v)| { 472*1074eb34SSamuel Dai if v.metadata().unwrap().inode_id.into() == ino { 473*1074eb34SSamuel Dai Some(k.to_string()) 474*1074eb34SSamuel Dai } else { 475*1074eb34SSamuel Dai None 476*1074eb34SSamuel Dai } 4776b4e7a29SLoGin }) 478004e86ffSlogin .collect(); 479004e86ffSlogin 480004e86ffSlogin match key.len() { 481676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 482004e86ffSlogin 1=>{return Ok(key.remove(0));} 4836b4e7a29SLoGin _ => 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) 484004e86ffSlogin } 485004e86ffSlogin } 486004e86ffSlogin } 487004e86ffSlogin } 488004e86ffSlogin 48952da9a59SGnoCiYeH fn ioctl( 49052da9a59SGnoCiYeH &self, 49152da9a59SGnoCiYeH _cmd: u32, 49252da9a59SGnoCiYeH _data: usize, 49352da9a59SGnoCiYeH _private_data: &FilePrivateData, 49452da9a59SGnoCiYeH ) -> Result<usize, SystemError> { 495*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 496004e86ffSlogin } 497004e86ffSlogin 498676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 499004e86ffSlogin let info = self.metadata()?; 500004e86ffSlogin if info.file_type != FileType::Dir { 501676b8ef6SMork return Err(SystemError::ENOTDIR); 502004e86ffSlogin } 503004e86ffSlogin 504004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 505004e86ffSlogin keys.push(String::from(".")); 506004e86ffSlogin keys.push(String::from("..")); 507*1074eb34SSamuel Dai keys.append( 508*1074eb34SSamuel Dai &mut self 509*1074eb34SSamuel Dai .0 510*1074eb34SSamuel Dai .lock() 511*1074eb34SSamuel Dai .children 512*1074eb34SSamuel Dai .keys() 513*1074eb34SSamuel Dai .map(ToString::to_string) 514*1074eb34SSamuel Dai .collect(), 515*1074eb34SSamuel Dai ); 516004e86ffSlogin 517004e86ffSlogin return Ok(keys); 518004e86ffSlogin } 519004e86ffSlogin 520676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 521004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 522004e86ffSlogin } 523004e86ffSlogin 524676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 525004e86ffSlogin let mut inode = self.0.lock(); 526004e86ffSlogin inode.metadata.atime = metadata.atime; 527004e86ffSlogin inode.metadata.mtime = metadata.mtime; 528004e86ffSlogin inode.metadata.ctime = metadata.ctime; 529004e86ffSlogin inode.metadata.mode = metadata.mode; 530004e86ffSlogin inode.metadata.uid = metadata.uid; 531004e86ffSlogin inode.metadata.gid = metadata.gid; 532004e86ffSlogin 533004e86ffSlogin return Ok(()); 534004e86ffSlogin } 535004e86ffSlogin 536004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 537004e86ffSlogin fn read_at( 538004e86ffSlogin &self, 539004e86ffSlogin _offset: usize, 540004e86ffSlogin _len: usize, 541004e86ffSlogin _buf: &mut [u8], 542dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 543676b8ef6SMork ) -> Result<usize, SystemError> { 54420e3152eSlogin kerror!("DevFS: read_at is not supported!"); 545*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 546004e86ffSlogin } 547004e86ffSlogin 548004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 549004e86ffSlogin fn write_at( 550004e86ffSlogin &self, 551004e86ffSlogin _offset: usize, 552004e86ffSlogin _len: usize, 553004e86ffSlogin _buf: &[u8], 554dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 555676b8ef6SMork ) -> Result<usize, SystemError> { 556*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 557*1074eb34SSamuel Dai } 558*1074eb34SSamuel Dai 559*1074eb34SSamuel Dai fn parent(&self) -> Result<Arc<dyn IndexNode>, SystemError> { 560*1074eb34SSamuel Dai let me = self.0.lock(); 561*1074eb34SSamuel Dai Ok(me 562*1074eb34SSamuel Dai .parent 563*1074eb34SSamuel Dai .upgrade() 564*1074eb34SSamuel Dai .unwrap_or(me.self_ref.upgrade().unwrap())) 565*1074eb34SSamuel Dai } 566*1074eb34SSamuel Dai 567*1074eb34SSamuel Dai fn dname(&self) -> Result<DName, SystemError> { 568*1074eb34SSamuel Dai Ok(self.0.lock().dname.clone()) 569004e86ffSlogin } 570004e86ffSlogin } 571004e86ffSlogin 572004e86ffSlogin /// @brief 所有的设备INode都需要额外实现这个trait 573004e86ffSlogin pub trait DeviceINode: IndexNode { 574004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>); 575004e86ffSlogin // TODO: 增加 unregister 方法 576004e86ffSlogin } 577004e86ffSlogin 578004e86ffSlogin /// @brief 获取devfs实例的强类型不可变引用 579004e86ffSlogin macro_rules! devfs_exact_ref { 580004e86ffSlogin () => {{ 581676b8ef6SMork let devfs_inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().find("dev"); 582004e86ffSlogin if let Err(e) = devfs_inode { 583676b8ef6SMork kerror!("failed to get DevFS ref. errcode = {:?}", e); 584676b8ef6SMork return Err(SystemError::ENOENT); 585004e86ffSlogin } 586004e86ffSlogin 587004e86ffSlogin let binding = devfs_inode.unwrap(); 588004e86ffSlogin let devfs_inode: &LockedDevFSInode = binding 589004e86ffSlogin .as_any_ref() 590004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 591004e86ffSlogin .unwrap(); 592004e86ffSlogin let binding = devfs_inode.fs(); 593004e86ffSlogin binding 594004e86ffSlogin } 595004e86ffSlogin .as_any_ref() 596004e86ffSlogin .downcast_ref::<DevFS>() 597004e86ffSlogin .unwrap()}; 598004e86ffSlogin } 599004e86ffSlogin /// @brief devfs的设备注册函数 600676b8ef6SMork pub fn devfs_register<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 601004e86ffSlogin return devfs_exact_ref!().register_device(name, device); 602004e86ffSlogin } 603004e86ffSlogin 604004e86ffSlogin /// @brief devfs的设备卸载函数 605004e86ffSlogin #[allow(dead_code)] 606676b8ef6SMork pub fn devfs_unregister<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 607004e86ffSlogin return devfs_exact_ref!().unregister_device(name, device); 608004e86ffSlogin } 609b087521eSChiichen 610b087521eSChiichen pub fn devfs_init() -> Result<(), SystemError> { 611b087521eSChiichen static INIT: Once = Once::new(); 612b087521eSChiichen let mut result = None; 613b087521eSChiichen INIT.call_once(|| { 61434e6d6c8Syuyi2439 kinfo!("Initializing DevFS..."); 615b087521eSChiichen // 创建 devfs 实例 616b087521eSChiichen let devfs: Arc<DevFS> = DevFS::new(); 617b087521eSChiichen // devfs 挂载 618*1074eb34SSamuel Dai ROOT_INODE() 619*1074eb34SSamuel Dai .mkdir("dev", ModeType::from_bits_truncate(0o755)) 620*1074eb34SSamuel Dai .expect("Unabled to find /dev") 621b087521eSChiichen .mount(devfs) 622*1074eb34SSamuel Dai .expect("Failed to mount at /dev"); 623b087521eSChiichen kinfo!("DevFS mounted."); 624b087521eSChiichen result = Some(Ok(())); 625b087521eSChiichen }); 626b087521eSChiichen 627b087521eSChiichen return result.unwrap(); 628b087521eSChiichen } 629