10d48c3c9Slogin use crate::filesystem::vfs::file::FileMode; 2004e86ffSlogin use crate::filesystem::vfs::make_rawdev; 3004e86ffSlogin use crate::filesystem::vfs::{ 4004e86ffSlogin core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, PollStatus, 5004e86ffSlogin }; 6004e86ffSlogin use crate::{ 7*676b8ef6SMork syscall::SystemError, 8004e86ffSlogin libs::spinlock::SpinLock, 9004e86ffSlogin time::TimeSpec, 10004e86ffSlogin }; 11004e86ffSlogin use alloc::{ 12004e86ffSlogin string::String, 13004e86ffSlogin sync::{Arc, Weak}, 14004e86ffSlogin vec::Vec, 15004e86ffSlogin }; 16004e86ffSlogin // use uuid::{uuid, Uuid}; 17004e86ffSlogin use super::{DevFS, DeviceINode}; 18004e86ffSlogin 19004e86ffSlogin #[derive(Debug)] 20004e86ffSlogin pub struct ZeroInode { 21004e86ffSlogin /// uuid 暂时不知道有什么用(x 22004e86ffSlogin // uuid: Uuid, 23004e86ffSlogin /// 指向自身的弱引用 24004e86ffSlogin self_ref: Weak<LockedZeroInode>, 25004e86ffSlogin /// 指向inode所在的文件系统对象的指针 26004e86ffSlogin fs: Weak<DevFS>, 27004e86ffSlogin /// INode 元数据 28004e86ffSlogin metadata: Metadata, 29004e86ffSlogin } 30004e86ffSlogin 31004e86ffSlogin #[derive(Debug)] 32004e86ffSlogin pub struct LockedZeroInode(SpinLock<ZeroInode>); 33004e86ffSlogin 34004e86ffSlogin impl LockedZeroInode { 35004e86ffSlogin pub fn new() -> Arc<Self> { 36004e86ffSlogin let inode = ZeroInode { 37004e86ffSlogin // uuid: Uuid::new_v5(), 38004e86ffSlogin self_ref: Weak::default(), 39004e86ffSlogin fs: Weak::default(), 40004e86ffSlogin metadata: Metadata { 41004e86ffSlogin dev_id: 1, 42004e86ffSlogin inode_id: generate_inode_id(), 43004e86ffSlogin size: 0, 44004e86ffSlogin blk_size: 0, 45004e86ffSlogin blocks: 0, 46004e86ffSlogin atime: TimeSpec::default(), 47004e86ffSlogin mtime: TimeSpec::default(), 48004e86ffSlogin ctime: TimeSpec::default(), 49004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 50004e86ffSlogin mode: 0o666, 51004e86ffSlogin nlinks: 1, 52004e86ffSlogin uid: 0, 53004e86ffSlogin gid: 0, 54004e86ffSlogin raw_dev: make_rawdev(1, 3), // 这里用来作为device number 55004e86ffSlogin }, 56004e86ffSlogin }; 57004e86ffSlogin 58004e86ffSlogin let result = Arc::new(LockedZeroInode(SpinLock::new(inode))); 59004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 60004e86ffSlogin 61004e86ffSlogin return result; 62004e86ffSlogin } 63004e86ffSlogin } 64004e86ffSlogin 65004e86ffSlogin impl DeviceINode for LockedZeroInode { 66004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 67004e86ffSlogin self.0.lock().fs = fs; 68004e86ffSlogin } 69004e86ffSlogin } 70004e86ffSlogin 71004e86ffSlogin impl IndexNode for LockedZeroInode { 72004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 73004e86ffSlogin self 74004e86ffSlogin } 75004e86ffSlogin 76*676b8ef6SMork fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 77*676b8ef6SMork Err(SystemError::ENOTSUP) 78004e86ffSlogin } 79004e86ffSlogin 80*676b8ef6SMork fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 81*676b8ef6SMork Err(SystemError::ENOTSUP) 82004e86ffSlogin } 83004e86ffSlogin 84*676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 85004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 86004e86ffSlogin } 87004e86ffSlogin 88004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 89004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 90004e86ffSlogin } 91004e86ffSlogin 92*676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 93*676b8ef6SMork Err(SystemError::ENOTSUP) 94004e86ffSlogin } 95004e86ffSlogin 96*676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 97004e86ffSlogin let mut inode = self.0.lock(); 98004e86ffSlogin inode.metadata.atime = metadata.atime; 99004e86ffSlogin inode.metadata.mtime = metadata.mtime; 100004e86ffSlogin inode.metadata.ctime = metadata.ctime; 101004e86ffSlogin inode.metadata.mode = metadata.mode; 102004e86ffSlogin inode.metadata.uid = metadata.uid; 103004e86ffSlogin inode.metadata.gid = metadata.gid; 104004e86ffSlogin 105004e86ffSlogin return Ok(()); 106004e86ffSlogin } 107004e86ffSlogin 108*676b8ef6SMork fn poll(&self) -> Result<PollStatus, SystemError> { 109004e86ffSlogin return Ok(PollStatus { 110004e86ffSlogin flags: PollStatus::READ_MASK | PollStatus::WRITE_MASK, 111004e86ffSlogin }); 112004e86ffSlogin } 113004e86ffSlogin 114004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 115004e86ffSlogin fn read_at( 116004e86ffSlogin &self, 117004e86ffSlogin _offset: usize, 118004e86ffSlogin len: usize, 119004e86ffSlogin buf: &mut [u8], 120004e86ffSlogin _data: &mut FilePrivateData, 121*676b8ef6SMork ) -> Result<usize, SystemError> { 122004e86ffSlogin if buf.len() < len { 123*676b8ef6SMork return Err(SystemError::EINVAL); 124004e86ffSlogin } 125004e86ffSlogin 126004e86ffSlogin for i in 0..len { 127004e86ffSlogin buf[i] = 0; 128004e86ffSlogin } 129004e86ffSlogin 130004e86ffSlogin return Ok(len); 131004e86ffSlogin } 132004e86ffSlogin 133004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 134004e86ffSlogin fn write_at( 135004e86ffSlogin &self, 136004e86ffSlogin _offset: usize, 137004e86ffSlogin len: usize, 138004e86ffSlogin buf: &[u8], 139004e86ffSlogin _data: &mut FilePrivateData, 140*676b8ef6SMork ) -> Result<usize, SystemError> { 141004e86ffSlogin if buf.len() < len { 142*676b8ef6SMork return Err(SystemError::EINVAL); 143004e86ffSlogin } 144004e86ffSlogin 145004e86ffSlogin Ok(len) 146004e86ffSlogin } 147004e86ffSlogin } 148