1b087521eSChiichen use crate::driver::base::block::block_device::BlockDevice; 202343d0bSLoGin use crate::driver::base::device::device_number::{DeviceNumber, Major}; 3004e86ffSlogin use crate::filesystem::devfs::{DevFS, DeviceINode}; 40d48c3c9Slogin use crate::filesystem::vfs::file::FileMode; 56b4e7a29SLoGin use crate::filesystem::vfs::syscall::ModeType; 6004e86ffSlogin use crate::filesystem::vfs::{ 702343d0bSLoGin core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 8004e86ffSlogin }; 9dfe53cf0SGnoCiYeH use crate::libs::spinlock::SpinLockGuard; 10*6fc066acSJomo use crate::{libs::spinlock::SpinLock, time::PosixTimeSpec}; 11004e86ffSlogin use alloc::{ 12004e86ffSlogin string::String, 13004e86ffSlogin sync::{Arc, Weak}, 14004e86ffSlogin vec::Vec, 15004e86ffSlogin }; 1691e9d4abSLoGin use system_error::SystemError; 17004e86ffSlogin 18004e86ffSlogin use super::ahcidisk::LockedAhciDisk; 19004e86ffSlogin 20004e86ffSlogin #[derive(Debug)] 21004e86ffSlogin pub struct AhciInode { 22004e86ffSlogin /// uuid 暂时不知道有什么用(x 23004e86ffSlogin // uuid: Uuid, 24004e86ffSlogin /// 指向自身的弱引用 25004e86ffSlogin self_ref: Weak<LockedAhciInode>, 26004e86ffSlogin /// 指向inode所在的文件系统对象的指针 27004e86ffSlogin fs: Weak<DevFS>, 28004e86ffSlogin /// INode 元数据 29004e86ffSlogin metadata: Metadata, 30004e86ffSlogin /// INode 对应的磁盘 31004e86ffSlogin disk: Arc<LockedAhciDisk>, 32004e86ffSlogin } 33004e86ffSlogin 34004e86ffSlogin #[derive(Debug)] 35004e86ffSlogin pub struct LockedAhciInode(pub SpinLock<AhciInode>); 36004e86ffSlogin 37004e86ffSlogin impl LockedAhciInode { 38004e86ffSlogin pub fn new(disk: Arc<LockedAhciDisk>) -> Arc<Self> { 39004e86ffSlogin let inode = AhciInode { 40004e86ffSlogin // uuid: Uuid::new_v5(), 41004e86ffSlogin self_ref: Weak::default(), 42004e86ffSlogin fs: Weak::default(), 43b5b571e0SLoGin disk, 44004e86ffSlogin metadata: Metadata { 45004e86ffSlogin dev_id: 1, 46004e86ffSlogin inode_id: generate_inode_id(), 47004e86ffSlogin size: 0, 48004e86ffSlogin blk_size: 0, 49004e86ffSlogin blocks: 0, 50*6fc066acSJomo atime: PosixTimeSpec::default(), 51*6fc066acSJomo mtime: PosixTimeSpec::default(), 52*6fc066acSJomo ctime: PosixTimeSpec::default(), 53004e86ffSlogin file_type: FileType::BlockDevice, // 文件夹,block设备,char设备 546b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 55004e86ffSlogin nlinks: 1, 56004e86ffSlogin uid: 0, 57004e86ffSlogin gid: 0, 5802343d0bSLoGin raw_dev: DeviceNumber::new(Major::HD_MAJOR, 0), 59004e86ffSlogin }, 60004e86ffSlogin }; 61004e86ffSlogin 62004e86ffSlogin let result = Arc::new(LockedAhciInode(SpinLock::new(inode))); 63004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 64004e86ffSlogin 65004e86ffSlogin return result; 66004e86ffSlogin } 67004e86ffSlogin } 68004e86ffSlogin 69004e86ffSlogin impl DeviceINode for LockedAhciInode { 70004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 71004e86ffSlogin self.0.lock().fs = fs; 72004e86ffSlogin } 73004e86ffSlogin } 74004e86ffSlogin 75004e86ffSlogin impl IndexNode for LockedAhciInode { 76004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 77004e86ffSlogin self 78004e86ffSlogin } 79004e86ffSlogin 80dfe53cf0SGnoCiYeH fn open( 81dfe53cf0SGnoCiYeH &self, 82dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 83dfe53cf0SGnoCiYeH _mode: &FileMode, 84dfe53cf0SGnoCiYeH ) -> Result<(), SystemError> { 8579a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 86004e86ffSlogin } 87004e86ffSlogin 88dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 8979a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 90004e86ffSlogin } 91004e86ffSlogin 92676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 93004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 94004e86ffSlogin } 95004e86ffSlogin 96004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 97004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 98004e86ffSlogin } 99004e86ffSlogin 100676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 10179a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 102004e86ffSlogin } 103004e86ffSlogin 104676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 105004e86ffSlogin let mut inode = self.0.lock(); 106004e86ffSlogin inode.metadata.atime = metadata.atime; 107004e86ffSlogin inode.metadata.mtime = metadata.mtime; 108004e86ffSlogin inode.metadata.ctime = metadata.ctime; 109004e86ffSlogin inode.metadata.mode = metadata.mode; 110004e86ffSlogin inode.metadata.uid = metadata.uid; 111004e86ffSlogin inode.metadata.gid = metadata.gid; 112004e86ffSlogin 113004e86ffSlogin return Ok(()); 114004e86ffSlogin } 115004e86ffSlogin 116004e86ffSlogin /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 117004e86ffSlogin fn read_at( 118004e86ffSlogin &self, 119004e86ffSlogin offset: usize, // lba地址 120004e86ffSlogin len: usize, 121004e86ffSlogin buf: &mut [u8], 122dfe53cf0SGnoCiYeH data: SpinLockGuard<FilePrivateData>, 123676b8ef6SMork ) -> Result<usize, SystemError> { 124004e86ffSlogin if buf.len() < len { 125676b8ef6SMork return Err(SystemError::EINVAL); 126004e86ffSlogin } 127004e86ffSlogin 128dfe53cf0SGnoCiYeH if let FilePrivateData::Unused = *data { 129b087521eSChiichen return self.0.lock().disk.read_at_bytes(offset, len, buf); 130004e86ffSlogin } 131004e86ffSlogin 132676b8ef6SMork return Err(SystemError::EINVAL); 133004e86ffSlogin } 134004e86ffSlogin 135004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 136004e86ffSlogin fn write_at( 137004e86ffSlogin &self, 138004e86ffSlogin offset: usize, // lba地址 139004e86ffSlogin len: usize, 140004e86ffSlogin buf: &[u8], 141dfe53cf0SGnoCiYeH data: SpinLockGuard<FilePrivateData>, 142676b8ef6SMork ) -> Result<usize, SystemError> { 143004e86ffSlogin if buf.len() < len { 144676b8ef6SMork return Err(SystemError::EINVAL); 145004e86ffSlogin } 146004e86ffSlogin 147dfe53cf0SGnoCiYeH if let FilePrivateData::Unused = *data { 148b087521eSChiichen return self.0.lock().disk.write_at_bytes(offset, len, buf); 149004e86ffSlogin } 150004e86ffSlogin 151676b8ef6SMork return Err(SystemError::EINVAL); 152004e86ffSlogin } 153004e86ffSlogin } 154