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, 873c607aaSYJwu2023 FileSystem, FileType, FsInfo, IndexNode, Metadata, PollStatus, 9004e86ffSlogin }; 10004e86ffSlogin use crate::{ 11*b087521eSChiichen kerror, kinfo, 12*b087521eSChiichen libs::{ 13*b087521eSChiichen once::Once, 14*b087521eSChiichen spinlock::{SpinLock, SpinLockGuard}, 15*b087521eSChiichen }, 1620e3152eSlogin syscall::SystemError, 1720e3152eSlogin time::TimeSpec, 18004e86ffSlogin }; 19004e86ffSlogin use alloc::{ 20004e86ffSlogin collections::BTreeMap, 21004e86ffSlogin string::{String, ToString}, 22004e86ffSlogin sync::{Arc, Weak}, 23004e86ffSlogin vec::Vec, 24004e86ffSlogin }; 25004e86ffSlogin 26004e86ffSlogin const DEVFS_MAX_NAMELEN: usize = 64; 27004e86ffSlogin 28004e86ffSlogin /// @brief dev文件系统 29004e86ffSlogin #[derive(Debug)] 30004e86ffSlogin pub struct DevFS { 31004e86ffSlogin // 文件系统根节点 32004e86ffSlogin root_inode: Arc<LockedDevFSInode>, 33004e86ffSlogin } 34004e86ffSlogin 35004e86ffSlogin impl FileSystem for DevFS { 36004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 37004e86ffSlogin self 38004e86ffSlogin } 39004e86ffSlogin 40004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 41004e86ffSlogin return self.root_inode.clone(); 42004e86ffSlogin } 43004e86ffSlogin 44004e86ffSlogin fn info(&self) -> super::vfs::FsInfo { 45004e86ffSlogin return FsInfo { 46004e86ffSlogin blk_dev_id: 0, 47004e86ffSlogin max_name_len: DEVFS_MAX_NAMELEN, 48004e86ffSlogin }; 49004e86ffSlogin } 50004e86ffSlogin } 51004e86ffSlogin 52004e86ffSlogin impl DevFS { 53004e86ffSlogin pub fn new() -> Arc<Self> { 54004e86ffSlogin // 初始化root inode 55004e86ffSlogin let root: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new( 56004e86ffSlogin // /dev 的权限设置为 读+执行,root 可以读写 57004e86ffSlogin // root 的 parent 是空指针 58004e86ffSlogin DevFSInode::new(FileType::Dir, 0o755 as u32, 0), 59004e86ffSlogin ))); 60004e86ffSlogin 61004e86ffSlogin let devfs: Arc<DevFS> = Arc::new(DevFS { root_inode: root }); 62004e86ffSlogin 63004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 64004e86ffSlogin let mut root_guard: SpinLockGuard<DevFSInode> = devfs.root_inode.0.lock(); 65004e86ffSlogin root_guard.parent = Arc::downgrade(&devfs.root_inode); 66004e86ffSlogin root_guard.self_ref = Arc::downgrade(&devfs.root_inode); 67004e86ffSlogin root_guard.fs = Arc::downgrade(&devfs); 68004e86ffSlogin // 释放锁 69004e86ffSlogin drop(root_guard); 70004e86ffSlogin 71004e86ffSlogin // 创建文件夹 72004e86ffSlogin let root: &Arc<LockedDevFSInode> = &devfs.root_inode; 73004e86ffSlogin root.add_dir("char") 74004e86ffSlogin .expect("DevFS: Failed to create /dev/char"); 75004e86ffSlogin 76004e86ffSlogin root.add_dir("block") 77004e86ffSlogin .expect("DevFS: Failed to create /dev/block"); 78004e86ffSlogin devfs.register_bultinin_device(); 79004e86ffSlogin 8033270d00Slogin // kdebug!("ls /dev: {:?}", root.list()); 81004e86ffSlogin return devfs; 82004e86ffSlogin } 83004e86ffSlogin 84004e86ffSlogin /// @brief 注册系统内部自带的设备 85004e86ffSlogin fn register_bultinin_device(&self) { 86004e86ffSlogin use null_dev::LockedNullInode; 87004e86ffSlogin use zero_dev::LockedZeroInode; 88004e86ffSlogin let dev_root: Arc<LockedDevFSInode> = self.root_inode.clone(); 89004e86ffSlogin dev_root 90004e86ffSlogin .add_dev("null", LockedNullInode::new()) 91004e86ffSlogin .expect("DevFS: Failed to register /dev/null"); 92004e86ffSlogin dev_root 93004e86ffSlogin .add_dev("zero", LockedZeroInode::new()) 94004e86ffSlogin .expect("DevFS: Failed to register /dev/zero"); 95004e86ffSlogin } 96004e86ffSlogin 97004e86ffSlogin /// @brief 在devfs内注册设备 98004e86ffSlogin /// 99004e86ffSlogin /// @param name 设备名称 100004e86ffSlogin /// @param device 设备节点的结构体 10120e3152eSlogin pub fn register_device<T: DeviceINode>( 10220e3152eSlogin &self, 10320e3152eSlogin name: &str, 10420e3152eSlogin device: Arc<T>, 10520e3152eSlogin ) -> Result<(), SystemError> { 106004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 10720e3152eSlogin let metadata = device.metadata()?; 10820e3152eSlogin match metadata.file_type { 109004e86ffSlogin // 字节设备挂载在 /dev/char 110004e86ffSlogin FileType::CharDevice => { 111004e86ffSlogin if let Err(_) = dev_root_inode.find("char") { 112004e86ffSlogin dev_root_inode.create("char", FileType::Dir, 0o755)?; 113004e86ffSlogin } 114004e86ffSlogin 115004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 116004e86ffSlogin let dev_char_inode: &LockedDevFSInode = any_char_inode 117004e86ffSlogin .as_any_ref() 118004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 119004e86ffSlogin .unwrap(); 12020e3152eSlogin // 在 /dev/char 下创建设备节点 121004e86ffSlogin dev_char_inode.add_dev(name, device.clone())?; 12220e3152eSlogin 12320e3152eSlogin // 特殊处理 tty 设备,挂载在 /dev 下 12420e3152eSlogin if name.starts_with("tty") && name.len() > 3 { 12520e3152eSlogin dev_root_inode.add_dev(name, device.clone())?; 12620e3152eSlogin } 127004e86ffSlogin device.set_fs(dev_char_inode.0.lock().fs.clone()); 128004e86ffSlogin } 129004e86ffSlogin FileType::BlockDevice => { 130004e86ffSlogin if let Err(_) = dev_root_inode.find("block") { 131004e86ffSlogin dev_root_inode.create("block", FileType::Dir, 0o755)?; 132004e86ffSlogin } 133004e86ffSlogin 134004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 135004e86ffSlogin let dev_block_inode: &LockedDevFSInode = any_block_inode 136004e86ffSlogin .as_any_ref() 137004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 138004e86ffSlogin .unwrap(); 139004e86ffSlogin 140004e86ffSlogin dev_block_inode.add_dev(name, device.clone())?; 141004e86ffSlogin device.set_fs(dev_block_inode.0.lock().fs.clone()); 142004e86ffSlogin } 143004e86ffSlogin _ => { 14479a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 145004e86ffSlogin } 146004e86ffSlogin } 147004e86ffSlogin 148004e86ffSlogin return Ok(()); 149004e86ffSlogin } 150004e86ffSlogin 151004e86ffSlogin /// @brief 卸载设备 15220e3152eSlogin pub fn unregister_device<T: DeviceINode>( 15320e3152eSlogin &self, 15420e3152eSlogin name: &str, 15520e3152eSlogin device: Arc<T>, 15620e3152eSlogin ) -> Result<(), SystemError> { 157004e86ffSlogin let dev_root_inode: Arc<LockedDevFSInode> = self.root_inode.clone(); 158004e86ffSlogin match device.metadata().unwrap().file_type { 159004e86ffSlogin // 字节设备挂载在 /dev/char 160004e86ffSlogin FileType::CharDevice => { 161004e86ffSlogin if let Err(_) = dev_root_inode.find("char") { 162676b8ef6SMork return Err(SystemError::ENOENT); 163004e86ffSlogin } 164004e86ffSlogin 165004e86ffSlogin let any_char_inode = dev_root_inode.find("char")?; 166004e86ffSlogin let dev_char_inode = any_char_inode 167004e86ffSlogin .as_any_ref() 168004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 169004e86ffSlogin .unwrap(); 170004e86ffSlogin // TODO: 调用设备的卸载接口(当引入卸载接口之后) 171004e86ffSlogin dev_char_inode.remove(name)?; 172004e86ffSlogin } 173004e86ffSlogin FileType::BlockDevice => { 174004e86ffSlogin if let Err(_) = dev_root_inode.find("block") { 175676b8ef6SMork return Err(SystemError::ENOENT); 176004e86ffSlogin } 177004e86ffSlogin 178004e86ffSlogin let any_block_inode = dev_root_inode.find("block")?; 179004e86ffSlogin let dev_block_inode = any_block_inode 180004e86ffSlogin .as_any_ref() 181004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 182004e86ffSlogin .unwrap(); 183004e86ffSlogin 184004e86ffSlogin dev_block_inode.remove(name)?; 185004e86ffSlogin } 186004e86ffSlogin _ => { 18779a452ceShoumkh return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 188004e86ffSlogin } 189004e86ffSlogin } 190004e86ffSlogin 191004e86ffSlogin return Ok(()); 192004e86ffSlogin } 193004e86ffSlogin } 194004e86ffSlogin 195004e86ffSlogin /// @brief dev文件i节点(锁) 196004e86ffSlogin #[derive(Debug)] 197004e86ffSlogin pub struct LockedDevFSInode(SpinLock<DevFSInode>); 198004e86ffSlogin 199004e86ffSlogin /// @brief dev文件i节点(无锁) 200004e86ffSlogin #[derive(Debug)] 201004e86ffSlogin pub struct DevFSInode { 202004e86ffSlogin /// 指向父Inode的弱引用 203004e86ffSlogin parent: Weak<LockedDevFSInode>, 204004e86ffSlogin /// 指向自身的弱引用 205004e86ffSlogin self_ref: Weak<LockedDevFSInode>, 206004e86ffSlogin /// 子Inode的B树 207004e86ffSlogin children: BTreeMap<String, Arc<dyn IndexNode>>, 208004e86ffSlogin /// 指向inode所在的文件系统对象的指针 209004e86ffSlogin fs: Weak<DevFS>, 210004e86ffSlogin /// INode 元数据 211004e86ffSlogin metadata: Metadata, 212004e86ffSlogin } 213004e86ffSlogin 214004e86ffSlogin impl DevFSInode { 215004e86ffSlogin pub fn new(dev_type_: FileType, mode_: u32, data_: usize) -> Self { 216004e86ffSlogin return Self::new_with_parent(Weak::default(), dev_type_, mode_, data_); 217004e86ffSlogin } 218004e86ffSlogin 219004e86ffSlogin pub fn new_with_parent( 220004e86ffSlogin parent: Weak<LockedDevFSInode>, 221004e86ffSlogin dev_type_: FileType, 222004e86ffSlogin mode_: u32, 223004e86ffSlogin data_: usize, 224004e86ffSlogin ) -> Self { 225004e86ffSlogin return DevFSInode { 226004e86ffSlogin parent: parent, 227004e86ffSlogin self_ref: Weak::default(), 228004e86ffSlogin children: BTreeMap::new(), 229004e86ffSlogin metadata: Metadata { 230004e86ffSlogin dev_id: 1, 231004e86ffSlogin inode_id: generate_inode_id(), 232004e86ffSlogin size: 0, 233004e86ffSlogin blk_size: 0, 234004e86ffSlogin blocks: 0, 235004e86ffSlogin atime: TimeSpec::default(), 236004e86ffSlogin mtime: TimeSpec::default(), 237004e86ffSlogin ctime: TimeSpec::default(), 238004e86ffSlogin file_type: dev_type_, // 文件夹 239004e86ffSlogin mode: mode_, 240004e86ffSlogin nlinks: 1, 241004e86ffSlogin uid: 0, 242004e86ffSlogin gid: 0, 243004e86ffSlogin raw_dev: data_, 244004e86ffSlogin }, 245004e86ffSlogin fs: Weak::default(), 246004e86ffSlogin }; 247004e86ffSlogin } 248004e86ffSlogin } 249004e86ffSlogin 250004e86ffSlogin impl LockedDevFSInode { 251676b8ef6SMork pub fn add_dir(&self, name: &str) -> Result<(), SystemError> { 252004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 253004e86ffSlogin 254004e86ffSlogin if guard.children.contains_key(name) { 255676b8ef6SMork return Err(SystemError::EEXIST); 256004e86ffSlogin } 257004e86ffSlogin 258004e86ffSlogin match self.do_create_with_data(guard, name, FileType::Dir, 0o755 as u32, 0) { 259004e86ffSlogin Ok(inode) => inode, 260004e86ffSlogin Err(err) => { 261004e86ffSlogin return Err(err); 262004e86ffSlogin } 263004e86ffSlogin }; 264004e86ffSlogin 265004e86ffSlogin return Ok(()); 266004e86ffSlogin } 267004e86ffSlogin 268676b8ef6SMork pub fn add_dev(&self, name: &str, dev: Arc<dyn IndexNode>) -> Result<(), SystemError> { 269004e86ffSlogin let mut this = self.0.lock(); 270004e86ffSlogin 271004e86ffSlogin if this.children.contains_key(name) { 272676b8ef6SMork return Err(SystemError::EEXIST); 273004e86ffSlogin } 274004e86ffSlogin 275004e86ffSlogin this.children.insert(name.to_string(), dev); 276004e86ffSlogin return Ok(()); 277004e86ffSlogin } 278004e86ffSlogin 279676b8ef6SMork pub fn remove(&self, name: &str) -> Result<(), SystemError> { 280004e86ffSlogin let x = self 281004e86ffSlogin .0 282004e86ffSlogin .lock() 283004e86ffSlogin .children 284004e86ffSlogin .remove(name) 285676b8ef6SMork .ok_or(SystemError::ENOENT)?; 286004e86ffSlogin 287004e86ffSlogin drop(x); 288004e86ffSlogin return Ok(()); 289004e86ffSlogin } 290004e86ffSlogin 29173c607aaSYJwu2023 fn do_create_with_data( 29273c607aaSYJwu2023 &self, 29373c607aaSYJwu2023 mut guard: SpinLockGuard<DevFSInode>, 29473c607aaSYJwu2023 _name: &str, 295004e86ffSlogin _file_type: FileType, 296004e86ffSlogin _mode: u32, 29773c607aaSYJwu2023 _data: usize, 298676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 299004e86ffSlogin if guard.metadata.file_type != FileType::Dir { 300676b8ef6SMork return Err(SystemError::ENOTDIR); 301004e86ffSlogin } 302004e86ffSlogin 303004e86ffSlogin // 如果有重名的,则返回 304004e86ffSlogin if guard.children.contains_key(_name) { 305676b8ef6SMork return Err(SystemError::EEXIST); 306004e86ffSlogin } 307004e86ffSlogin 308004e86ffSlogin // 创建inode 309004e86ffSlogin let result: Arc<LockedDevFSInode> = Arc::new(LockedDevFSInode(SpinLock::new(DevFSInode { 310004e86ffSlogin parent: guard.self_ref.clone(), 311004e86ffSlogin self_ref: Weak::default(), 312004e86ffSlogin children: BTreeMap::new(), 313004e86ffSlogin metadata: Metadata { 314004e86ffSlogin dev_id: 0, 315004e86ffSlogin inode_id: generate_inode_id(), 316004e86ffSlogin size: 0, 317004e86ffSlogin blk_size: 0, 318004e86ffSlogin blocks: 0, 319004e86ffSlogin atime: TimeSpec::default(), 320004e86ffSlogin mtime: TimeSpec::default(), 321004e86ffSlogin ctime: TimeSpec::default(), 322004e86ffSlogin file_type: _file_type, 323004e86ffSlogin mode: _mode, 324004e86ffSlogin nlinks: 1, 325004e86ffSlogin uid: 0, 326004e86ffSlogin gid: 0, 327004e86ffSlogin raw_dev: _data, 328004e86ffSlogin }, 329004e86ffSlogin fs: guard.fs.clone(), 330004e86ffSlogin }))); 331004e86ffSlogin 332004e86ffSlogin // 初始化inode的自引用的weak指针 333004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 334004e86ffSlogin 335004e86ffSlogin // 将子inode插入父inode的B树中 336004e86ffSlogin guard.children.insert(String::from(_name), result.clone()); 337004e86ffSlogin return Ok(result); 338004e86ffSlogin } 339004e86ffSlogin } 340004e86ffSlogin 341004e86ffSlogin impl IndexNode for LockedDevFSInode { 342004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 343004e86ffSlogin self 344004e86ffSlogin } 345004e86ffSlogin 34620e3152eSlogin fn open( 34720e3152eSlogin &self, 34820e3152eSlogin _data: &mut super::vfs::FilePrivateData, 34920e3152eSlogin _mode: &FileMode, 35020e3152eSlogin ) -> Result<(), SystemError> { 351004e86ffSlogin return Ok(()); 352004e86ffSlogin } 353004e86ffSlogin 354676b8ef6SMork fn close(&self, _data: &mut super::vfs::FilePrivateData) -> Result<(), SystemError> { 355004e86ffSlogin return Ok(()); 356004e86ffSlogin } 357004e86ffSlogin 358004e86ffSlogin fn create_with_data( 359004e86ffSlogin &self, 360004e86ffSlogin name: &str, 361004e86ffSlogin file_type: FileType, 362004e86ffSlogin mode: u32, 363004e86ffSlogin data: usize, 364676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 365004e86ffSlogin // 获取当前inode 366004e86ffSlogin let guard: SpinLockGuard<DevFSInode> = self.0.lock(); 367004e86ffSlogin // 如果当前inode不是文件夹,则返回 368004e86ffSlogin return self.do_create_with_data(guard, name, file_type, mode, data); 369004e86ffSlogin } 370004e86ffSlogin 371676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 372004e86ffSlogin let inode = self.0.lock(); 373004e86ffSlogin 374004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 375676b8ef6SMork return Err(SystemError::ENOTDIR); 376004e86ffSlogin } 377004e86ffSlogin 378004e86ffSlogin match name { 379004e86ffSlogin "" | "." => { 380676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 381004e86ffSlogin } 382004e86ffSlogin ".." => { 383676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 384004e86ffSlogin } 385004e86ffSlogin name => { 386004e86ffSlogin // 在子目录项中查找 387676b8ef6SMork return Ok(inode.children.get(name).ok_or(SystemError::ENOENT)?.clone()); 388004e86ffSlogin } 389004e86ffSlogin } 390004e86ffSlogin } 391004e86ffSlogin 392004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 393004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 394004e86ffSlogin } 395004e86ffSlogin 396676b8ef6SMork fn get_entry_name(&self, ino: super::vfs::InodeId) -> Result<String, SystemError> { 397004e86ffSlogin let inode: SpinLockGuard<DevFSInode> = self.0.lock(); 398004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 399676b8ef6SMork return Err(SystemError::ENOTDIR); 400004e86ffSlogin } 401004e86ffSlogin 402004e86ffSlogin match ino { 403004e86ffSlogin 0 => { 404004e86ffSlogin return Ok(String::from(".")); 405004e86ffSlogin } 406004e86ffSlogin 1 => { 407004e86ffSlogin return Ok(String::from("..")); 408004e86ffSlogin } 409004e86ffSlogin ino => { 410004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 411004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 412004e86ffSlogin let mut key: Vec<String> = inode 413004e86ffSlogin .children 414004e86ffSlogin .keys() 415004e86ffSlogin .filter(|k| inode.children.get(*k).unwrap().metadata().unwrap().inode_id == ino) 416004e86ffSlogin .cloned() 417004e86ffSlogin .collect(); 418004e86ffSlogin 419004e86ffSlogin match key.len() { 420676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 421004e86ffSlogin 1=>{return Ok(key.remove(0));} 422004e86ffSlogin _ => 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) 423004e86ffSlogin } 424004e86ffSlogin } 425004e86ffSlogin } 426004e86ffSlogin } 427004e86ffSlogin 428676b8ef6SMork fn ioctl(&self, _cmd: u32, _data: usize) -> Result<usize, SystemError> { 42979a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 430004e86ffSlogin } 431004e86ffSlogin 432676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 433004e86ffSlogin let info = self.metadata()?; 434004e86ffSlogin if info.file_type != FileType::Dir { 435676b8ef6SMork return Err(SystemError::ENOTDIR); 436004e86ffSlogin } 437004e86ffSlogin 438004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 439004e86ffSlogin keys.push(String::from(".")); 440004e86ffSlogin keys.push(String::from("..")); 441004e86ffSlogin keys.append(&mut self.0.lock().children.keys().cloned().collect()); 442004e86ffSlogin 443004e86ffSlogin return Ok(keys); 444004e86ffSlogin } 445004e86ffSlogin 446676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 447004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 448004e86ffSlogin } 449004e86ffSlogin 450676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 451004e86ffSlogin let mut inode = self.0.lock(); 452004e86ffSlogin inode.metadata.atime = metadata.atime; 453004e86ffSlogin inode.metadata.mtime = metadata.mtime; 454004e86ffSlogin inode.metadata.ctime = metadata.ctime; 455004e86ffSlogin inode.metadata.mode = metadata.mode; 456004e86ffSlogin inode.metadata.uid = metadata.uid; 457004e86ffSlogin inode.metadata.gid = metadata.gid; 458004e86ffSlogin 459004e86ffSlogin return Ok(()); 460004e86ffSlogin } 461004e86ffSlogin 462676b8ef6SMork fn poll(&self) -> Result<super::vfs::PollStatus, SystemError> { 463004e86ffSlogin // 加锁 464004e86ffSlogin let inode: SpinLockGuard<DevFSInode> = self.0.lock(); 465004e86ffSlogin 466004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 467004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 468676b8ef6SMork return Err(SystemError::EISDIR); 469004e86ffSlogin } 470004e86ffSlogin 471cde5492fSlogin return Ok(PollStatus::READ | PollStatus::WRITE); 472004e86ffSlogin } 473004e86ffSlogin 474004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 475004e86ffSlogin fn read_at( 476004e86ffSlogin &self, 477004e86ffSlogin _offset: usize, 478004e86ffSlogin _len: usize, 479004e86ffSlogin _buf: &mut [u8], 480004e86ffSlogin _data: &mut super::vfs::file::FilePrivateData, 481676b8ef6SMork ) -> Result<usize, SystemError> { 48220e3152eSlogin kerror!("DevFS: read_at is not supported!"); 48379a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 484004e86ffSlogin } 485004e86ffSlogin 486004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 487004e86ffSlogin fn write_at( 488004e86ffSlogin &self, 489004e86ffSlogin _offset: usize, 490004e86ffSlogin _len: usize, 491004e86ffSlogin _buf: &[u8], 492004e86ffSlogin _data: &mut super::vfs::file::FilePrivateData, 493676b8ef6SMork ) -> Result<usize, SystemError> { 49479a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 495004e86ffSlogin } 496004e86ffSlogin } 497004e86ffSlogin 498004e86ffSlogin /// @brief 所有的设备INode都需要额外实现这个trait 499004e86ffSlogin pub trait DeviceINode: IndexNode { 500004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>); 501004e86ffSlogin // TODO: 增加 unregister 方法 502004e86ffSlogin } 503004e86ffSlogin 504004e86ffSlogin /// @brief 获取devfs实例的强类型不可变引用 505004e86ffSlogin macro_rules! devfs_exact_ref { 506004e86ffSlogin () => {{ 507676b8ef6SMork let devfs_inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().find("dev"); 508004e86ffSlogin if let Err(e) = devfs_inode { 509676b8ef6SMork kerror!("failed to get DevFS ref. errcode = {:?}", e); 510676b8ef6SMork return Err(SystemError::ENOENT); 511004e86ffSlogin } 512004e86ffSlogin 513004e86ffSlogin let binding = devfs_inode.unwrap(); 514004e86ffSlogin let devfs_inode: &LockedDevFSInode = binding 515004e86ffSlogin .as_any_ref() 516004e86ffSlogin .downcast_ref::<LockedDevFSInode>() 517004e86ffSlogin .unwrap(); 518004e86ffSlogin let binding = devfs_inode.fs(); 519004e86ffSlogin binding 520004e86ffSlogin } 521004e86ffSlogin .as_any_ref() 522004e86ffSlogin .downcast_ref::<DevFS>() 523004e86ffSlogin .unwrap()}; 524004e86ffSlogin } 525004e86ffSlogin /// @brief devfs的设备注册函数 526676b8ef6SMork pub fn devfs_register<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 527004e86ffSlogin return devfs_exact_ref!().register_device(name, device); 528004e86ffSlogin } 529004e86ffSlogin 530004e86ffSlogin /// @brief devfs的设备卸载函数 531004e86ffSlogin #[allow(dead_code)] 532676b8ef6SMork pub fn devfs_unregister<T: DeviceINode>(name: &str, device: Arc<T>) -> Result<(), SystemError> { 533004e86ffSlogin return devfs_exact_ref!().unregister_device(name, device); 534004e86ffSlogin } 535*b087521eSChiichen 536*b087521eSChiichen pub fn devfs_init() -> Result<(), SystemError> { 537*b087521eSChiichen static INIT: Once = Once::new(); 538*b087521eSChiichen let mut result = None; 539*b087521eSChiichen INIT.call_once(|| { 540*b087521eSChiichen kinfo!("Initializing ProcFS..."); 541*b087521eSChiichen // 创建 devfs 实例 542*b087521eSChiichen let devfs: Arc<DevFS> = DevFS::new(); 543*b087521eSChiichen // devfs 挂载 544*b087521eSChiichen let _t = ROOT_INODE() 545*b087521eSChiichen .find("dev") 546*b087521eSChiichen .expect("Cannot find /dev") 547*b087521eSChiichen .mount(devfs) 548*b087521eSChiichen .expect("Failed to mount devfs"); 549*b087521eSChiichen kinfo!("DevFS mounted."); 550*b087521eSChiichen result = Some(Ok(())); 551*b087521eSChiichen }); 552*b087521eSChiichen 553*b087521eSChiichen return result.unwrap(); 554*b087521eSChiichen } 555