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