10d48c3c9Slogin use crate::filesystem::vfs::file::FileMode; 2004e86ffSlogin use crate::filesystem::vfs::make_rawdev; 3*6b4e7a29SLoGin use crate::filesystem::vfs::syscall::ModeType; 4004e86ffSlogin use crate::filesystem::vfs::{ 5004e86ffSlogin core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, PollStatus, 6004e86ffSlogin }; 778bf93f0SYJwu2023 use crate::{libs::spinlock::SpinLock, syscall::SystemError, time::TimeSpec}; 8004e86ffSlogin use alloc::{ 9004e86ffSlogin string::String, 10004e86ffSlogin sync::{Arc, Weak}, 11004e86ffSlogin vec::Vec, 12004e86ffSlogin }; 13004e86ffSlogin // use uuid::{uuid, Uuid}; 14004e86ffSlogin use super::{DevFS, DeviceINode}; 15004e86ffSlogin 16004e86ffSlogin #[derive(Debug)] 17004e86ffSlogin pub struct NullInode { 18004e86ffSlogin /// uuid 暂时不知道有什么用(x 19004e86ffSlogin // uuid: Uuid, 20004e86ffSlogin /// 指向自身的弱引用 21004e86ffSlogin self_ref: Weak<LockedNullInode>, 22004e86ffSlogin /// 指向inode所在的文件系统对象的指针 23004e86ffSlogin fs: Weak<DevFS>, 24004e86ffSlogin /// INode 元数据 25004e86ffSlogin metadata: Metadata, 26004e86ffSlogin } 27004e86ffSlogin 28004e86ffSlogin #[derive(Debug)] 29004e86ffSlogin pub struct LockedNullInode(SpinLock<NullInode>); 30004e86ffSlogin 31004e86ffSlogin impl LockedNullInode { 32004e86ffSlogin pub fn new() -> Arc<Self> { 33004e86ffSlogin let inode = NullInode { 34004e86ffSlogin // uuid: Uuid::new_v5(), 35004e86ffSlogin self_ref: Weak::default(), 36004e86ffSlogin fs: Weak::default(), 37004e86ffSlogin metadata: Metadata { 38004e86ffSlogin dev_id: 1, 39004e86ffSlogin inode_id: generate_inode_id(), 40004e86ffSlogin size: 0, 41004e86ffSlogin blk_size: 0, 42004e86ffSlogin blocks: 0, 43004e86ffSlogin atime: TimeSpec::default(), 44004e86ffSlogin mtime: TimeSpec::default(), 45004e86ffSlogin ctime: TimeSpec::default(), 46004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 47*6b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 48004e86ffSlogin nlinks: 1, 49004e86ffSlogin uid: 0, 50004e86ffSlogin gid: 0, 51004e86ffSlogin raw_dev: make_rawdev(1, 3), // 这里用来作为device number 52004e86ffSlogin }, 53004e86ffSlogin }; 54004e86ffSlogin 55004e86ffSlogin let result = Arc::new(LockedNullInode(SpinLock::new(inode))); 56004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 57004e86ffSlogin 58004e86ffSlogin return result; 59004e86ffSlogin } 60004e86ffSlogin } 61004e86ffSlogin 62004e86ffSlogin impl DeviceINode for LockedNullInode { 63004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 64004e86ffSlogin self.0.lock().fs = fs; 65004e86ffSlogin } 66004e86ffSlogin } 67004e86ffSlogin 68004e86ffSlogin impl IndexNode for LockedNullInode { 69004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 70004e86ffSlogin self 71004e86ffSlogin } 72004e86ffSlogin 73676b8ef6SMork fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 7406500303SLoGin return Ok(()); 75004e86ffSlogin } 76004e86ffSlogin 77676b8ef6SMork fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 7806500303SLoGin return Ok(()); 79004e86ffSlogin } 80004e86ffSlogin 81676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 82004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 83004e86ffSlogin } 84004e86ffSlogin 85004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 86004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 87004e86ffSlogin } 88004e86ffSlogin 89676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 9079a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 91004e86ffSlogin } 92004e86ffSlogin 93676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 94004e86ffSlogin let mut inode = self.0.lock(); 95004e86ffSlogin inode.metadata.atime = metadata.atime; 96004e86ffSlogin inode.metadata.mtime = metadata.mtime; 97004e86ffSlogin inode.metadata.ctime = metadata.ctime; 98004e86ffSlogin inode.metadata.mode = metadata.mode; 99004e86ffSlogin inode.metadata.uid = metadata.uid; 100004e86ffSlogin inode.metadata.gid = metadata.gid; 101004e86ffSlogin 102004e86ffSlogin return Ok(()); 103004e86ffSlogin } 104004e86ffSlogin 105676b8ef6SMork fn poll(&self) -> Result<PollStatus, SystemError> { 106cde5492fSlogin return Ok(PollStatus::READ | PollStatus::WRITE); 107004e86ffSlogin } 108004e86ffSlogin 109004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 110004e86ffSlogin fn read_at( 111004e86ffSlogin &self, 112004e86ffSlogin _offset: usize, 11306500303SLoGin _len: usize, 11406500303SLoGin _buf: &mut [u8], 115004e86ffSlogin _data: &mut FilePrivateData, 116676b8ef6SMork ) -> Result<usize, SystemError> { 11706500303SLoGin return Ok(0); 118004e86ffSlogin } 119004e86ffSlogin 120004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 121004e86ffSlogin fn write_at( 122004e86ffSlogin &self, 123004e86ffSlogin _offset: usize, 124004e86ffSlogin len: usize, 125004e86ffSlogin buf: &[u8], 126004e86ffSlogin _data: &mut FilePrivateData, 127676b8ef6SMork ) -> Result<usize, SystemError> { 128004e86ffSlogin if buf.len() < len { 129676b8ef6SMork return Err(SystemError::EINVAL); 130004e86ffSlogin } 131004e86ffSlogin 132004e86ffSlogin Ok(len) 133004e86ffSlogin } 134004e86ffSlogin } 135