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