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