1 use crate::driver::base::block::block_device::BlockDevice; 2 use crate::driver::base::device::device_number::{DeviceNumber, Major}; 3 use crate::filesystem::devfs::{DevFS, DeviceINode}; 4 use crate::filesystem::vfs::file::FileMode; 5 use crate::filesystem::vfs::syscall::ModeType; 6 use crate::filesystem::vfs::{ 7 core::generate_inode_id, FilePrivateData, FileSystem, FileType, IndexNode, Metadata, 8 }; 9 use crate::{libs::spinlock::SpinLock, time::TimeSpec}; 10 use alloc::{ 11 string::String, 12 sync::{Arc, Weak}, 13 vec::Vec, 14 }; 15 use system_error::SystemError; 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: DeviceNumber::new(Major::HD_MAJOR, 0), 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 /// 读设备 - 应该调用设备的函数读写,而不是通过文件系统读写 112 fn read_at( 113 &self, 114 offset: usize, // lba地址 115 len: usize, 116 buf: &mut [u8], 117 data: &mut FilePrivateData, 118 ) -> Result<usize, SystemError> { 119 if buf.len() < len { 120 return Err(SystemError::EINVAL); 121 } 122 123 if let FilePrivateData::Unused = data { 124 return self.0.lock().disk.read_at_bytes(offset, len, buf); 125 } 126 127 return Err(SystemError::EINVAL); 128 } 129 130 /// 写设备 - 应该调用设备的函数读写,而不是通过文件系统读写 131 fn write_at( 132 &self, 133 offset: usize, // lba地址 134 len: usize, 135 buf: &[u8], 136 data: &mut FilePrivateData, 137 ) -> Result<usize, SystemError> { 138 if buf.len() < len { 139 return Err(SystemError::EINVAL); 140 } 141 142 if let FilePrivateData::Unused = data { 143 return self.0.lock().disk.write_at_bytes(offset, len, buf); 144 } 145 146 return Err(SystemError::EINVAL); 147 } 148 } 149