10d48c3c9Slogin use crate::filesystem::vfs::file::FileMode; 2004e86ffSlogin use crate::filesystem::vfs::make_rawdev; 36b4e7a29SLoGin use crate::filesystem::vfs::syscall::ModeType; 4004e86ffSlogin use crate::filesystem::vfs::{ 540609970SGnoCiYeH core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 6004e86ffSlogin }; 7*91e9d4abSLoGin use crate::{libs::spinlock::SpinLock, time::TimeSpec}; 8004e86ffSlogin use alloc::{ 9004e86ffSlogin string::String, 10004e86ffSlogin sync::{Arc, Weak}, 11004e86ffSlogin vec::Vec, 12004e86ffSlogin }; 13*91e9d4abSLoGin use system_error::SystemError; 14004e86ffSlogin // use uuid::{uuid, Uuid}; 15004e86ffSlogin use super::{DevFS, DeviceINode}; 16004e86ffSlogin 17004e86ffSlogin #[derive(Debug)] 18004e86ffSlogin pub struct ZeroInode { 19004e86ffSlogin /// uuid 暂时不知道有什么用(x 20004e86ffSlogin // uuid: Uuid, 21004e86ffSlogin /// 指向自身的弱引用 22004e86ffSlogin self_ref: Weak<LockedZeroInode>, 23004e86ffSlogin /// 指向inode所在的文件系统对象的指针 24004e86ffSlogin fs: Weak<DevFS>, 25004e86ffSlogin /// INode 元数据 26004e86ffSlogin metadata: Metadata, 27004e86ffSlogin } 28004e86ffSlogin 29004e86ffSlogin #[derive(Debug)] 30004e86ffSlogin pub struct LockedZeroInode(SpinLock<ZeroInode>); 31004e86ffSlogin 32004e86ffSlogin impl LockedZeroInode { 33004e86ffSlogin pub fn new() -> Arc<Self> { 34004e86ffSlogin let inode = ZeroInode { 35004e86ffSlogin // uuid: Uuid::new_v5(), 36004e86ffSlogin self_ref: Weak::default(), 37004e86ffSlogin fs: Weak::default(), 38004e86ffSlogin metadata: Metadata { 39004e86ffSlogin dev_id: 1, 40004e86ffSlogin inode_id: generate_inode_id(), 41004e86ffSlogin size: 0, 42004e86ffSlogin blk_size: 0, 43004e86ffSlogin blocks: 0, 44004e86ffSlogin atime: TimeSpec::default(), 45004e86ffSlogin mtime: TimeSpec::default(), 46004e86ffSlogin ctime: TimeSpec::default(), 47004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 486b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 49004e86ffSlogin nlinks: 1, 50004e86ffSlogin uid: 0, 51004e86ffSlogin gid: 0, 52004e86ffSlogin raw_dev: make_rawdev(1, 3), // 这里用来作为device number 53004e86ffSlogin }, 54004e86ffSlogin }; 55004e86ffSlogin 56004e86ffSlogin let result = Arc::new(LockedZeroInode(SpinLock::new(inode))); 57004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 58004e86ffSlogin 59004e86ffSlogin return result; 60004e86ffSlogin } 61004e86ffSlogin } 62004e86ffSlogin 63004e86ffSlogin impl DeviceINode for LockedZeroInode { 64004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 65004e86ffSlogin self.0.lock().fs = fs; 66004e86ffSlogin } 67004e86ffSlogin } 68004e86ffSlogin 69004e86ffSlogin impl IndexNode for LockedZeroInode { 70004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 71004e86ffSlogin self 72004e86ffSlogin } 73004e86ffSlogin 74676b8ef6SMork fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 7506500303SLoGin return Ok(()); 76004e86ffSlogin } 77004e86ffSlogin 78676b8ef6SMork fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 7906500303SLoGin return Ok(()); 80004e86ffSlogin } 81004e86ffSlogin 82676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 83004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 84004e86ffSlogin } 85004e86ffSlogin 86004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 87004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 88004e86ffSlogin } 89004e86ffSlogin 90676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 9179a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 92004e86ffSlogin } 93004e86ffSlogin 94676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 95004e86ffSlogin let mut inode = self.0.lock(); 96004e86ffSlogin inode.metadata.atime = metadata.atime; 97004e86ffSlogin inode.metadata.mtime = metadata.mtime; 98004e86ffSlogin inode.metadata.ctime = metadata.ctime; 99004e86ffSlogin inode.metadata.mode = metadata.mode; 100004e86ffSlogin inode.metadata.uid = metadata.uid; 101004e86ffSlogin inode.metadata.gid = metadata.gid; 102004e86ffSlogin 103004e86ffSlogin return Ok(()); 104004e86ffSlogin } 105004e86ffSlogin 106004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 107004e86ffSlogin fn read_at( 108004e86ffSlogin &self, 109004e86ffSlogin _offset: usize, 110004e86ffSlogin len: usize, 111004e86ffSlogin buf: &mut [u8], 112004e86ffSlogin _data: &mut FilePrivateData, 113676b8ef6SMork ) -> Result<usize, SystemError> { 114004e86ffSlogin if buf.len() < len { 115676b8ef6SMork return Err(SystemError::EINVAL); 116004e86ffSlogin } 117004e86ffSlogin 118004e86ffSlogin for i in 0..len { 119004e86ffSlogin buf[i] = 0; 120004e86ffSlogin } 121004e86ffSlogin 122004e86ffSlogin return Ok(len); 123004e86ffSlogin } 124004e86ffSlogin 125004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 126004e86ffSlogin fn write_at( 127004e86ffSlogin &self, 128004e86ffSlogin _offset: usize, 129004e86ffSlogin len: usize, 130004e86ffSlogin buf: &[u8], 131004e86ffSlogin _data: &mut FilePrivateData, 132676b8ef6SMork ) -> Result<usize, SystemError> { 133004e86ffSlogin if buf.len() < len { 134676b8ef6SMork return Err(SystemError::EINVAL); 135004e86ffSlogin } 136004e86ffSlogin 137004e86ffSlogin Ok(len) 138004e86ffSlogin } 139004e86ffSlogin } 140