102343d0bSLoGin use crate::driver::base::device::device_number::DeviceNumber; 20d48c3c9Slogin use crate::filesystem::vfs::file::FileMode; 36b4e7a29SLoGin use crate::filesystem::vfs::syscall::ModeType; 4004e86ffSlogin use crate::filesystem::vfs::{ 540609970SGnoCiYeH core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 6004e86ffSlogin }; 7dfe53cf0SGnoCiYeH use crate::libs::spinlock::SpinLockGuard; 86fc066acSJomo use crate::{libs::spinlock::SpinLock, time::PosixTimeSpec}; 9004e86ffSlogin use alloc::{ 10004e86ffSlogin string::String, 11004e86ffSlogin sync::{Arc, Weak}, 12004e86ffSlogin vec::Vec, 13004e86ffSlogin }; 1491e9d4abSLoGin use system_error::SystemError; 15004e86ffSlogin // use uuid::{uuid, Uuid}; 16004e86ffSlogin use super::{DevFS, DeviceINode}; 17004e86ffSlogin 18004e86ffSlogin #[derive(Debug)] 19004e86ffSlogin pub struct NullInode { 20004e86ffSlogin /// uuid 暂时不知道有什么用(x 21004e86ffSlogin // uuid: Uuid, 22004e86ffSlogin /// 指向自身的弱引用 23004e86ffSlogin self_ref: Weak<LockedNullInode>, 24004e86ffSlogin /// 指向inode所在的文件系统对象的指针 25004e86ffSlogin fs: Weak<DevFS>, 26004e86ffSlogin /// INode 元数据 27004e86ffSlogin metadata: Metadata, 28004e86ffSlogin } 29004e86ffSlogin 30004e86ffSlogin #[derive(Debug)] 31004e86ffSlogin pub struct LockedNullInode(SpinLock<NullInode>); 32004e86ffSlogin 33004e86ffSlogin impl LockedNullInode { new() -> Arc<Self>34004e86ffSlogin pub fn new() -> Arc<Self> { 35004e86ffSlogin let inode = NullInode { 36004e86ffSlogin // uuid: Uuid::new_v5(), 37004e86ffSlogin self_ref: Weak::default(), 38004e86ffSlogin fs: Weak::default(), 39004e86ffSlogin metadata: Metadata { 40004e86ffSlogin dev_id: 1, 41004e86ffSlogin inode_id: generate_inode_id(), 42004e86ffSlogin size: 0, 43004e86ffSlogin blk_size: 0, 44004e86ffSlogin blocks: 0, 456fc066acSJomo atime: PosixTimeSpec::default(), 466fc066acSJomo mtime: PosixTimeSpec::default(), 476fc066acSJomo ctime: PosixTimeSpec::default(), 48004e86ffSlogin file_type: FileType::CharDevice, // 文件夹,block设备,char设备 496b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 50004e86ffSlogin nlinks: 1, 51004e86ffSlogin uid: 0, 52004e86ffSlogin gid: 0, 5302343d0bSLoGin raw_dev: DeviceNumber::default(), // 这里用来作为device number 54004e86ffSlogin }, 55004e86ffSlogin }; 56004e86ffSlogin 57004e86ffSlogin let result = Arc::new(LockedNullInode(SpinLock::new(inode))); 58004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 59004e86ffSlogin 60004e86ffSlogin return result; 61004e86ffSlogin } 62004e86ffSlogin } 63004e86ffSlogin 64004e86ffSlogin impl DeviceINode for LockedNullInode { set_fs(&self, fs: Weak<DevFS>)65004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 66004e86ffSlogin self.0.lock().fs = fs; 67004e86ffSlogin } 68004e86ffSlogin } 69004e86ffSlogin 70004e86ffSlogin impl IndexNode for LockedNullInode { as_any_ref(&self) -> &dyn core::any::Any71004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 72004e86ffSlogin self 73004e86ffSlogin } 74004e86ffSlogin open( &self, _data: SpinLockGuard<FilePrivateData>, _mode: &FileMode, ) -> Result<(), SystemError>75dfe53cf0SGnoCiYeH fn open( 76dfe53cf0SGnoCiYeH &self, 77dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 78dfe53cf0SGnoCiYeH _mode: &FileMode, 79dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 8006500303SLoGin return Ok(()); 81004e86ffSlogin } 82004e86ffSlogin close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError>83dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 8406500303SLoGin return Ok(()); 85004e86ffSlogin } 86004e86ffSlogin metadata(&self) -> Result<Metadata, SystemError>87676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 88004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 89004e86ffSlogin } 90004e86ffSlogin fs(&self) -> Arc<dyn FileSystem>91004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 92004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 93004e86ffSlogin } 94004e86ffSlogin list(&self) -> Result<Vec<String>, SystemError>95676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 96*1074eb34SSamuel Dai Err(SystemError::ENOSYS) 97004e86ffSlogin } 98004e86ffSlogin set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError>99676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 100004e86ffSlogin let mut inode = self.0.lock(); 101004e86ffSlogin inode.metadata.atime = metadata.atime; 102004e86ffSlogin inode.metadata.mtime = metadata.mtime; 103004e86ffSlogin inode.metadata.ctime = metadata.ctime; 104004e86ffSlogin inode.metadata.mode = metadata.mode; 105004e86ffSlogin inode.metadata.uid = metadata.uid; 106004e86ffSlogin inode.metadata.gid = metadata.gid; 107004e86ffSlogin 108004e86ffSlogin return Ok(()); 109004e86ffSlogin } 110004e86ffSlogin 111004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 read_at( &self, _offset: usize, _len: usize, _buf: &mut [u8], _data: SpinLockGuard<FilePrivateData>, ) -> Result<usize, SystemError>112004e86ffSlogin fn read_at( 113004e86ffSlogin &self, 114004e86ffSlogin _offset: usize, 11506500303SLoGin _len: usize, 11606500303SLoGin _buf: &mut [u8], 117dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 118676b8ef6SMork ) -> Result<usize, SystemError> { 11906500303SLoGin return Ok(0); 120004e86ffSlogin } 121004e86ffSlogin 122004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 write_at( &self, _offset: usize, len: usize, buf: &[u8], _data: SpinLockGuard<FilePrivateData>, ) -> Result<usize, SystemError>123004e86ffSlogin fn write_at( 124004e86ffSlogin &self, 125004e86ffSlogin _offset: usize, 126004e86ffSlogin len: usize, 127004e86ffSlogin buf: &[u8], 128dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 129676b8ef6SMork ) -> Result<usize, SystemError> { 130004e86ffSlogin if buf.len() < len { 131676b8ef6SMork return Err(SystemError::EINVAL); 132004e86ffSlogin } 133004e86ffSlogin 134004e86ffSlogin Ok(len) 135004e86ffSlogin } 136004e86ffSlogin } 137