1 use crate::filesystem::vfs::file::FileMode; 2 use crate::filesystem::vfs::make_rawdev; 3 use crate::filesystem::vfs::syscall::ModeType; 4 use crate::filesystem::vfs::{ 5 core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 6 }; 7 use crate::{libs::spinlock::SpinLock, time::TimeSpec}; 8 use alloc::{ 9 string::String, 10 sync::{Arc, Weak}, 11 vec::Vec, 12 }; 13 use system_error::SystemError; 14 // use uuid::{uuid, Uuid}; 15 use super::{DevFS, DeviceINode}; 16 17 #[derive(Debug)] 18 pub struct ZeroInode { 19 /// uuid 暂时不知道有什么用(x 20 // uuid: Uuid, 21 /// 指向自身的弱引用 22 self_ref: Weak<LockedZeroInode>, 23 /// 指向inode所在的文件系统对象的指针 24 fs: Weak<DevFS>, 25 /// INode 元数据 26 metadata: Metadata, 27 } 28 29 #[derive(Debug)] 30 pub struct LockedZeroInode(SpinLock<ZeroInode>); 31 32 impl LockedZeroInode { 33 pub fn new() -> Arc<Self> { 34 let inode = ZeroInode { 35 // uuid: Uuid::new_v5(), 36 self_ref: Weak::default(), 37 fs: Weak::default(), 38 metadata: Metadata { 39 dev_id: 1, 40 inode_id: generate_inode_id(), 41 size: 0, 42 blk_size: 0, 43 blocks: 0, 44 atime: TimeSpec::default(), 45 mtime: TimeSpec::default(), 46 ctime: TimeSpec::default(), 47 file_type: FileType::CharDevice, // 文件夹,block设备,char设备 48 mode: ModeType::from_bits_truncate(0o666), 49 nlinks: 1, 50 uid: 0, 51 gid: 0, 52 raw_dev: make_rawdev(1, 3), // 这里用来作为device number 53 }, 54 }; 55 56 let result = Arc::new(LockedZeroInode(SpinLock::new(inode))); 57 result.0.lock().self_ref = Arc::downgrade(&result); 58 59 return result; 60 } 61 } 62 63 impl DeviceINode for LockedZeroInode { 64 fn set_fs(&self, fs: Weak<DevFS>) { 65 self.0.lock().fs = fs; 66 } 67 } 68 69 impl IndexNode for LockedZeroInode { 70 fn as_any_ref(&self) -> &dyn core::any::Any { 71 self 72 } 73 74 fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 75 return Ok(()); 76 } 77 78 fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 79 return Ok(()); 80 } 81 82 fn metadata(&self) -> Result<Metadata, SystemError> { 83 return Ok(self.0.lock().metadata.clone()); 84 } 85 86 fn fs(&self) -> Arc<dyn FileSystem> { 87 return self.0.lock().fs.upgrade().unwrap(); 88 } 89 90 fn list(&self) -> Result<Vec<String>, SystemError> { 91 Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 92 } 93 94 fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 95 let mut inode = self.0.lock(); 96 inode.metadata.atime = metadata.atime; 97 inode.metadata.mtime = metadata.mtime; 98 inode.metadata.ctime = metadata.ctime; 99 inode.metadata.mode = metadata.mode; 100 inode.metadata.uid = metadata.uid; 101 inode.metadata.gid = metadata.gid; 102 103 return Ok(()); 104 } 105 106 /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 107 fn read_at( 108 &self, 109 _offset: usize, 110 len: usize, 111 buf: &mut [u8], 112 _data: &mut FilePrivateData, 113 ) -> Result<usize, SystemError> { 114 if buf.len() < len { 115 return Err(SystemError::EINVAL); 116 } 117 118 for i in 0..len { 119 buf[i] = 0; 120 } 121 122 return Ok(len); 123 } 124 125 /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 126 fn write_at( 127 &self, 128 _offset: usize, 129 len: usize, 130 buf: &[u8], 131 _data: &mut FilePrivateData, 132 ) -> Result<usize, SystemError> { 133 if buf.len() < len { 134 return Err(SystemError::EINVAL); 135 } 136 137 Ok(len) 138 } 139 } 140