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