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