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 }; 978bf93f0SYJwu2023 use crate::{libs::spinlock::SpinLock, time::TimeSpec}; 10004e86ffSlogin use alloc::{ 11004e86ffSlogin string::String, 12004e86ffSlogin sync::{Arc, Weak}, 13004e86ffSlogin vec::Vec, 14004e86ffSlogin }; 1591e9d4abSLoGin use system_error::SystemError; 16004e86ffSlogin 17004e86ffSlogin use super::ahcidisk::LockedAhciDisk; 18004e86ffSlogin 19004e86ffSlogin #[derive(Debug)] 20004e86ffSlogin pub struct AhciInode { 21004e86ffSlogin /// uuid 暂时不知道有什么用(x 22004e86ffSlogin // uuid: Uuid, 23004e86ffSlogin /// 指向自身的弱引用 24004e86ffSlogin self_ref: Weak<LockedAhciInode>, 25004e86ffSlogin /// 指向inode所在的文件系统对象的指针 26004e86ffSlogin fs: Weak<DevFS>, 27004e86ffSlogin /// INode 元数据 28004e86ffSlogin metadata: Metadata, 29004e86ffSlogin /// INode 对应的磁盘 30004e86ffSlogin disk: Arc<LockedAhciDisk>, 31004e86ffSlogin } 32004e86ffSlogin 33004e86ffSlogin #[derive(Debug)] 34004e86ffSlogin pub struct LockedAhciInode(pub SpinLock<AhciInode>); 35004e86ffSlogin 36004e86ffSlogin impl LockedAhciInode { 37004e86ffSlogin pub fn new(disk: Arc<LockedAhciDisk>) -> Arc<Self> { 38004e86ffSlogin let inode = AhciInode { 39004e86ffSlogin // uuid: Uuid::new_v5(), 40004e86ffSlogin self_ref: Weak::default(), 41004e86ffSlogin fs: Weak::default(), 42*b5b571e0SLoGin disk, 43004e86ffSlogin metadata: Metadata { 44004e86ffSlogin dev_id: 1, 45004e86ffSlogin inode_id: generate_inode_id(), 46004e86ffSlogin size: 0, 47004e86ffSlogin blk_size: 0, 48004e86ffSlogin blocks: 0, 49004e86ffSlogin atime: TimeSpec::default(), 50004e86ffSlogin mtime: TimeSpec::default(), 51004e86ffSlogin ctime: TimeSpec::default(), 52004e86ffSlogin file_type: FileType::BlockDevice, // 文件夹,block设备,char设备 536b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o666), 54004e86ffSlogin nlinks: 1, 55004e86ffSlogin uid: 0, 56004e86ffSlogin gid: 0, 5702343d0bSLoGin raw_dev: DeviceNumber::new(Major::HD_MAJOR, 0), 58004e86ffSlogin }, 59004e86ffSlogin }; 60004e86ffSlogin 61004e86ffSlogin let result = Arc::new(LockedAhciInode(SpinLock::new(inode))); 62004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 63004e86ffSlogin 64004e86ffSlogin return result; 65004e86ffSlogin } 66004e86ffSlogin } 67004e86ffSlogin 68004e86ffSlogin impl DeviceINode for LockedAhciInode { 69004e86ffSlogin fn set_fs(&self, fs: Weak<DevFS>) { 70004e86ffSlogin self.0.lock().fs = fs; 71004e86ffSlogin } 72004e86ffSlogin } 73004e86ffSlogin 74004e86ffSlogin impl IndexNode for LockedAhciInode { 75004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 76004e86ffSlogin self 77004e86ffSlogin } 78004e86ffSlogin 79676b8ef6SMork fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), SystemError> { 8079a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 81004e86ffSlogin } 82004e86ffSlogin 83676b8ef6SMork fn close(&self, _data: &mut FilePrivateData) -> Result<(), SystemError> { 8479a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 85004e86ffSlogin } 86004e86ffSlogin 87676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 88004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 89004e86ffSlogin } 90004e86ffSlogin 91004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 92004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 93004e86ffSlogin } 94004e86ffSlogin 95676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 9679a452ceShoumkh Err(SystemError::EOPNOTSUPP_OR_ENOTSUP) 97004e86ffSlogin } 98004e86ffSlogin 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 /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 112004e86ffSlogin fn read_at( 113004e86ffSlogin &self, 114004e86ffSlogin offset: usize, // lba地址 115004e86ffSlogin len: usize, 116004e86ffSlogin buf: &mut [u8], 117004e86ffSlogin data: &mut FilePrivateData, 118676b8ef6SMork ) -> Result<usize, SystemError> { 119004e86ffSlogin if buf.len() < len { 120676b8ef6SMork return Err(SystemError::EINVAL); 121004e86ffSlogin } 122004e86ffSlogin 123004e86ffSlogin if let FilePrivateData::Unused = data { 124b087521eSChiichen return self.0.lock().disk.read_at_bytes(offset, len, buf); 125004e86ffSlogin } 126004e86ffSlogin 127676b8ef6SMork return Err(SystemError::EINVAL); 128004e86ffSlogin } 129004e86ffSlogin 130004e86ffSlogin /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 131004e86ffSlogin fn write_at( 132004e86ffSlogin &self, 133004e86ffSlogin offset: usize, // lba地址 134004e86ffSlogin len: usize, 135004e86ffSlogin buf: &[u8], 136004e86ffSlogin data: &mut FilePrivateData, 137676b8ef6SMork ) -> Result<usize, SystemError> { 138004e86ffSlogin if buf.len() < len { 139676b8ef6SMork return Err(SystemError::EINVAL); 140004e86ffSlogin } 141004e86ffSlogin 142004e86ffSlogin if let FilePrivateData::Unused = data { 143b087521eSChiichen return self.0.lock().disk.write_at_bytes(offset, len, buf); 144004e86ffSlogin } 145004e86ffSlogin 146676b8ef6SMork return Err(SystemError::EINVAL); 147004e86ffSlogin } 148004e86ffSlogin } 149