1004e86ffSlogin use core::any::Any; 22dbef785SGnoCiYeH use core::intrinsics::unlikely; 3004e86ffSlogin 41d37ca6dSDonkey Kane use crate::filesystem::vfs::FSMAKER; 5597ecc08STTaq use crate::libs::rwlock::RwLock; 61d37ca6dSDonkey Kane use crate::{ 71d37ca6dSDonkey Kane driver::base::device::device_number::DeviceNumber, 81d37ca6dSDonkey Kane filesystem::vfs::{core::generate_inode_id, FileType}, 91d37ca6dSDonkey Kane ipc::pipe::LockedPipeInode, 10924d64deSBrahmaMantra libs::casting::DowncastArc, 111d37ca6dSDonkey Kane libs::spinlock::{SpinLock, SpinLockGuard}, 126fc066acSJomo time::PosixTimeSpec, 131d37ca6dSDonkey Kane }; 14924d64deSBrahmaMantra 151074eb34SSamuel Dai use alloc::string::ToString; 16004e86ffSlogin use alloc::{ 17004e86ffSlogin collections::BTreeMap, 18004e86ffSlogin string::String, 19004e86ffSlogin sync::{Arc, Weak}, 20004e86ffSlogin vec::Vec, 21004e86ffSlogin }; 2291e9d4abSLoGin use system_error::SystemError; 23004e86ffSlogin 24004e86ffSlogin use super::vfs::{ 251074eb34SSamuel Dai file::FilePrivateData, syscall::ModeType, utils::DName, FileSystem, FileSystemMaker, FsInfo, 261074eb34SSamuel Dai IndexNode, InodeId, Metadata, SpecialNodeData, 27004e86ffSlogin }; 28*bd70d2d1SLoGin 29*bd70d2d1SLoGin use linkme::distributed_slice; 30*bd70d2d1SLoGin 31597ecc08STTaq use super::vfs::{Magic, SuperBlock}; 32004e86ffSlogin 33004e86ffSlogin /// RamFS的inode名称的最大长度 34004e86ffSlogin const RAMFS_MAX_NAMELEN: usize = 64; 35597ecc08STTaq const RAMFS_BLOCK_SIZE: u64 = 512; 36004e86ffSlogin /// @brief 内存文件系统的Inode结构体 37004e86ffSlogin #[derive(Debug)] 38004e86ffSlogin struct LockedRamFSInode(SpinLock<RamFSInode>); 39004e86ffSlogin 40004e86ffSlogin /// @brief 内存文件系统结构体 41004e86ffSlogin #[derive(Debug)] 42004e86ffSlogin pub struct RamFS { 43004e86ffSlogin /// RamFS的root inode 44004e86ffSlogin root_inode: Arc<LockedRamFSInode>, 45597ecc08STTaq super_block: RwLock<SuperBlock>, 46004e86ffSlogin } 47004e86ffSlogin 48004e86ffSlogin /// @brief 内存文件系统的Inode结构体(不包含锁) 49004e86ffSlogin #[derive(Debug)] 50004e86ffSlogin pub struct RamFSInode { 51004e86ffSlogin // parent变量目前只在find函数中使用到 52004e86ffSlogin // 所以只有当inode是文件夹的时候,parent才会生效 53004e86ffSlogin // 对于文件来说,parent就没什么作用了 54004e86ffSlogin // 关于parent的说明: 目录不允许有硬链接 55004e86ffSlogin /// 指向父Inode的弱引用 56004e86ffSlogin parent: Weak<LockedRamFSInode>, 57004e86ffSlogin /// 指向自身的弱引用 58004e86ffSlogin self_ref: Weak<LockedRamFSInode>, 59004e86ffSlogin /// 子Inode的B树 601074eb34SSamuel Dai children: BTreeMap<DName, Arc<LockedRamFSInode>>, 61004e86ffSlogin /// 当前inode的数据部分 62004e86ffSlogin data: Vec<u8>, 63004e86ffSlogin /// 当前inode的元数据 64004e86ffSlogin metadata: Metadata, 65004e86ffSlogin /// 指向inode所在的文件系统对象的指针 66004e86ffSlogin fs: Weak<RamFS>, 672dbef785SGnoCiYeH /// 指向特殊节点 682dbef785SGnoCiYeH special_node: Option<SpecialNodeData>, 691074eb34SSamuel Dai 701074eb34SSamuel Dai name: DName, 71004e86ffSlogin } 72004e86ffSlogin 73004e86ffSlogin impl FileSystem for RamFS { 74004e86ffSlogin fn root_inode(&self) -> Arc<dyn super::vfs::IndexNode> { 75004e86ffSlogin return self.root_inode.clone(); 76004e86ffSlogin } 77004e86ffSlogin 78004e86ffSlogin fn info(&self) -> FsInfo { 79004e86ffSlogin return FsInfo { 80004e86ffSlogin blk_dev_id: 0, 81004e86ffSlogin max_name_len: RAMFS_MAX_NAMELEN, 82004e86ffSlogin }; 83004e86ffSlogin } 84004e86ffSlogin 85004e86ffSlogin /// @brief 本函数用于实现动态转换。 86004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 87004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 88004e86ffSlogin self 89004e86ffSlogin } 901d37ca6dSDonkey Kane 911d37ca6dSDonkey Kane fn name(&self) -> &str { 921d37ca6dSDonkey Kane "ramfs" 931d37ca6dSDonkey Kane } 94597ecc08STTaq 95597ecc08STTaq fn super_block(&self) -> SuperBlock { 96597ecc08STTaq self.super_block.read().clone() 97597ecc08STTaq } 98004e86ffSlogin } 99004e86ffSlogin 100004e86ffSlogin impl RamFS { 101004e86ffSlogin pub fn new() -> Arc<Self> { 102597ecc08STTaq let super_block = SuperBlock::new( 103597ecc08STTaq Magic::RAMFS_MAGIC, 104597ecc08STTaq RAMFS_BLOCK_SIZE, 105597ecc08STTaq RAMFS_MAX_NAMELEN as u64, 106597ecc08STTaq ); 107004e86ffSlogin // 初始化root inode 108004e86ffSlogin let root: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 109004e86ffSlogin parent: Weak::default(), 110004e86ffSlogin self_ref: Weak::default(), 111004e86ffSlogin children: BTreeMap::new(), 112004e86ffSlogin data: Vec::new(), 113004e86ffSlogin metadata: Metadata { 114004e86ffSlogin dev_id: 0, 115004e86ffSlogin inode_id: generate_inode_id(), 116004e86ffSlogin size: 0, 117004e86ffSlogin blk_size: 0, 118004e86ffSlogin blocks: 0, 1196fc066acSJomo atime: PosixTimeSpec::default(), 1206fc066acSJomo mtime: PosixTimeSpec::default(), 1216fc066acSJomo ctime: PosixTimeSpec::default(), 122004e86ffSlogin file_type: FileType::Dir, 1236b4e7a29SLoGin mode: ModeType::from_bits_truncate(0o777), 124004e86ffSlogin nlinks: 1, 125004e86ffSlogin uid: 0, 126004e86ffSlogin gid: 0, 12702343d0bSLoGin raw_dev: DeviceNumber::default(), 128004e86ffSlogin }, 129004e86ffSlogin fs: Weak::default(), 1302dbef785SGnoCiYeH special_node: None, 1311074eb34SSamuel Dai name: Default::default(), 132004e86ffSlogin }))); 133004e86ffSlogin 134597ecc08STTaq let result: Arc<RamFS> = Arc::new(RamFS { 135597ecc08STTaq root_inode: root, 136597ecc08STTaq super_block: RwLock::new(super_block), 137597ecc08STTaq }); 138004e86ffSlogin 139004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 140004e86ffSlogin let mut root_guard: SpinLockGuard<RamFSInode> = result.root_inode.0.lock(); 141004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 142004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 143004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 144004e86ffSlogin // 释放锁 145004e86ffSlogin drop(root_guard); 146004e86ffSlogin 147004e86ffSlogin return result; 148004e86ffSlogin } 1491d37ca6dSDonkey Kane 1501d37ca6dSDonkey Kane pub fn make_ramfs() -> Result<Arc<dyn FileSystem + 'static>, SystemError> { 1511d37ca6dSDonkey Kane let fs = RamFS::new(); 1521d37ca6dSDonkey Kane return Ok(fs); 153004e86ffSlogin } 1541d37ca6dSDonkey Kane } 1551d37ca6dSDonkey Kane #[distributed_slice(FSMAKER)] 1561d37ca6dSDonkey Kane static RAMFSMAKER: FileSystemMaker = FileSystemMaker::new( 1571d37ca6dSDonkey Kane "ramfs", 1581d37ca6dSDonkey Kane &(RamFS::make_ramfs as fn() -> Result<Arc<dyn FileSystem + 'static>, SystemError>), 1591d37ca6dSDonkey Kane ); 160004e86ffSlogin 161004e86ffSlogin impl IndexNode for LockedRamFSInode { 162a3ef8f8aSGnoCiYeH fn truncate(&self, len: usize) -> Result<(), SystemError> { 163a3ef8f8aSGnoCiYeH let mut inode = self.0.lock(); 164a3ef8f8aSGnoCiYeH 165a3ef8f8aSGnoCiYeH //如果是文件夹,则报错 166a3ef8f8aSGnoCiYeH if inode.metadata.file_type == FileType::Dir { 167a3ef8f8aSGnoCiYeH return Err(SystemError::EINVAL); 168a3ef8f8aSGnoCiYeH } 169a3ef8f8aSGnoCiYeH 170a3ef8f8aSGnoCiYeH //当前文件长度大于_len才进行截断,否则不操作 171a3ef8f8aSGnoCiYeH if inode.data.len() > len { 172a3ef8f8aSGnoCiYeH inode.data.resize(len, 0); 173a3ef8f8aSGnoCiYeH } 174a3ef8f8aSGnoCiYeH return Ok(()); 175a3ef8f8aSGnoCiYeH } 176a3ef8f8aSGnoCiYeH 177dfe53cf0SGnoCiYeH fn close(&self, _data: SpinLockGuard<FilePrivateData>) -> Result<(), SystemError> { 178a3ef8f8aSGnoCiYeH return Ok(()); 179a3ef8f8aSGnoCiYeH } 180a3ef8f8aSGnoCiYeH 181a3ef8f8aSGnoCiYeH fn open( 182a3ef8f8aSGnoCiYeH &self, 183dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 184a3ef8f8aSGnoCiYeH _mode: &super::vfs::file::FileMode, 185a3ef8f8aSGnoCiYeH ) -> Result<(), SystemError> { 186a3ef8f8aSGnoCiYeH return Ok(()); 187a3ef8f8aSGnoCiYeH } 188a3ef8f8aSGnoCiYeH 189004e86ffSlogin fn read_at( 190004e86ffSlogin &self, 191004e86ffSlogin offset: usize, 192004e86ffSlogin len: usize, 193004e86ffSlogin buf: &mut [u8], 194dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 195676b8ef6SMork ) -> Result<usize, SystemError> { 196004e86ffSlogin if buf.len() < len { 197676b8ef6SMork return Err(SystemError::EINVAL); 198004e86ffSlogin } 199004e86ffSlogin // 加锁 200004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 201004e86ffSlogin 202004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 203004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 204676b8ef6SMork return Err(SystemError::EISDIR); 205004e86ffSlogin } 206004e86ffSlogin 207004e86ffSlogin let start = inode.data.len().min(offset); 208004e86ffSlogin let end = inode.data.len().min(offset + len); 209004e86ffSlogin 210004e86ffSlogin // buffer空间不足 211004e86ffSlogin if buf.len() < (end - start) { 212676b8ef6SMork return Err(SystemError::ENOBUFS); 213004e86ffSlogin } 214004e86ffSlogin 215004e86ffSlogin // 拷贝数据 216004e86ffSlogin let src = &inode.data[start..end]; 217004e86ffSlogin buf[0..src.len()].copy_from_slice(src); 218004e86ffSlogin return Ok(src.len()); 219004e86ffSlogin } 220004e86ffSlogin 221004e86ffSlogin fn write_at( 222004e86ffSlogin &self, 223004e86ffSlogin offset: usize, 224004e86ffSlogin len: usize, 225004e86ffSlogin buf: &[u8], 226dfe53cf0SGnoCiYeH _data: SpinLockGuard<FilePrivateData>, 227676b8ef6SMork ) -> Result<usize, SystemError> { 228004e86ffSlogin if buf.len() < len { 229676b8ef6SMork return Err(SystemError::EINVAL); 230004e86ffSlogin } 231004e86ffSlogin 232004e86ffSlogin // 加锁 233004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 234004e86ffSlogin 235004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 236004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 237676b8ef6SMork return Err(SystemError::EISDIR); 238004e86ffSlogin } 239004e86ffSlogin 240004e86ffSlogin let data: &mut Vec<u8> = &mut inode.data; 241004e86ffSlogin 242004e86ffSlogin // 如果文件大小比原来的大,那就resize这个数组 243004e86ffSlogin if offset + len > data.len() { 244004e86ffSlogin data.resize(offset + len, 0); 245004e86ffSlogin } 246004e86ffSlogin 247004e86ffSlogin let target = &mut data[offset..offset + len]; 248004e86ffSlogin target.copy_from_slice(&buf[0..len]); 249004e86ffSlogin return Ok(len); 250004e86ffSlogin } 251004e86ffSlogin 252004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 253004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 254004e86ffSlogin } 255004e86ffSlogin 256004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 257004e86ffSlogin self 258004e86ffSlogin } 259004e86ffSlogin 260676b8ef6SMork fn metadata(&self) -> Result<Metadata, SystemError> { 261004e86ffSlogin let inode = self.0.lock(); 262004e86ffSlogin let mut metadata = inode.metadata.clone(); 263004e86ffSlogin metadata.size = inode.data.len() as i64; 264004e86ffSlogin 265004e86ffSlogin return Ok(metadata); 266004e86ffSlogin } 267004e86ffSlogin 268676b8ef6SMork fn set_metadata(&self, metadata: &Metadata) -> Result<(), SystemError> { 269004e86ffSlogin let mut inode = self.0.lock(); 270004e86ffSlogin inode.metadata.atime = metadata.atime; 271004e86ffSlogin inode.metadata.mtime = metadata.mtime; 272004e86ffSlogin inode.metadata.ctime = metadata.ctime; 273004e86ffSlogin inode.metadata.mode = metadata.mode; 274004e86ffSlogin inode.metadata.uid = metadata.uid; 275004e86ffSlogin inode.metadata.gid = metadata.gid; 276004e86ffSlogin 277004e86ffSlogin return Ok(()); 278004e86ffSlogin } 279004e86ffSlogin 280676b8ef6SMork fn resize(&self, len: usize) -> Result<(), SystemError> { 281004e86ffSlogin let mut inode = self.0.lock(); 282004e86ffSlogin if inode.metadata.file_type == FileType::File { 283004e86ffSlogin inode.data.resize(len, 0); 284004e86ffSlogin return Ok(()); 285004e86ffSlogin } else { 286676b8ef6SMork return Err(SystemError::EINVAL); 287004e86ffSlogin } 288004e86ffSlogin } 289004e86ffSlogin 290004e86ffSlogin fn create_with_data( 291004e86ffSlogin &self, 292004e86ffSlogin name: &str, 293004e86ffSlogin file_type: FileType, 2946b4e7a29SLoGin mode: ModeType, 295004e86ffSlogin data: usize, 296676b8ef6SMork ) -> Result<Arc<dyn IndexNode>, SystemError> { 2971074eb34SSamuel Dai let name = DName::from(name); 298004e86ffSlogin // 获取当前inode 299004e86ffSlogin let mut inode = self.0.lock(); 300004e86ffSlogin // 如果当前inode不是文件夹,则返回 301004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 302676b8ef6SMork return Err(SystemError::ENOTDIR); 303004e86ffSlogin } 304004e86ffSlogin // 如果有重名的,则返回 3051074eb34SSamuel Dai if inode.children.contains_key(&name) { 306676b8ef6SMork return Err(SystemError::EEXIST); 307004e86ffSlogin } 308004e86ffSlogin 309004e86ffSlogin // 创建inode 310004e86ffSlogin let result: Arc<LockedRamFSInode> = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 311004e86ffSlogin parent: inode.self_ref.clone(), 312004e86ffSlogin self_ref: Weak::default(), 313004e86ffSlogin children: BTreeMap::new(), 314004e86ffSlogin data: Vec::new(), 315004e86ffSlogin metadata: Metadata { 316004e86ffSlogin dev_id: 0, 317004e86ffSlogin inode_id: generate_inode_id(), 318004e86ffSlogin size: 0, 319004e86ffSlogin blk_size: 0, 320004e86ffSlogin blocks: 0, 3216fc066acSJomo atime: PosixTimeSpec::default(), 3226fc066acSJomo mtime: PosixTimeSpec::default(), 3236fc066acSJomo ctime: PosixTimeSpec::default(), 324b5b571e0SLoGin file_type, 325b5b571e0SLoGin mode, 326004e86ffSlogin nlinks: 1, 327004e86ffSlogin uid: 0, 328004e86ffSlogin gid: 0, 32902343d0bSLoGin raw_dev: DeviceNumber::from(data as u32), 330004e86ffSlogin }, 331004e86ffSlogin fs: inode.fs.clone(), 3322dbef785SGnoCiYeH special_node: None, 3331074eb34SSamuel Dai name: name.clone(), 334004e86ffSlogin }))); 335004e86ffSlogin 336004e86ffSlogin // 初始化inode的自引用的weak指针 337004e86ffSlogin result.0.lock().self_ref = Arc::downgrade(&result); 338004e86ffSlogin 339004e86ffSlogin // 将子inode插入父inode的B树中 3401074eb34SSamuel Dai inode.children.insert(name, result.clone()); 341004e86ffSlogin 342004e86ffSlogin return Ok(result); 343004e86ffSlogin } 344004e86ffSlogin 345676b8ef6SMork fn link(&self, name: &str, other: &Arc<dyn IndexNode>) -> Result<(), SystemError> { 346004e86ffSlogin let other: &LockedRamFSInode = other 347004e86ffSlogin .downcast_ref::<LockedRamFSInode>() 348676b8ef6SMork .ok_or(SystemError::EPERM)?; 3491074eb34SSamuel Dai let name = DName::from(name); 350004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 351004e86ffSlogin let mut other_locked: SpinLockGuard<RamFSInode> = other.0.lock(); 352004e86ffSlogin 353004e86ffSlogin // 如果当前inode不是文件夹,那么报错 354004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 355676b8ef6SMork return Err(SystemError::ENOTDIR); 356004e86ffSlogin } 357004e86ffSlogin 358004e86ffSlogin // 如果另一个inode是文件夹,那么也报错 359004e86ffSlogin if other_locked.metadata.file_type == FileType::Dir { 360676b8ef6SMork return Err(SystemError::EISDIR); 361004e86ffSlogin } 362004e86ffSlogin 363004e86ffSlogin // 如果当前文件夹下已经有同名文件,也报错。 3641074eb34SSamuel Dai if inode.children.contains_key(&name) { 365676b8ef6SMork return Err(SystemError::EEXIST); 366004e86ffSlogin } 367004e86ffSlogin 368004e86ffSlogin inode 369004e86ffSlogin .children 3701074eb34SSamuel Dai .insert(name, other_locked.self_ref.upgrade().unwrap()); 371004e86ffSlogin 372004e86ffSlogin // 增加硬链接计数 373004e86ffSlogin other_locked.metadata.nlinks += 1; 374004e86ffSlogin return Ok(()); 375004e86ffSlogin } 376004e86ffSlogin 377676b8ef6SMork fn unlink(&self, name: &str) -> Result<(), SystemError> { 378004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 379004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 380004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 381676b8ef6SMork return Err(SystemError::ENOTDIR); 382004e86ffSlogin } 383004e86ffSlogin // 不允许删除当前文件夹,也不允许删除上一个目录 384004e86ffSlogin if name == "." || name == ".." { 385676b8ef6SMork return Err(SystemError::ENOTEMPTY); 386004e86ffSlogin } 387004e86ffSlogin 3881074eb34SSamuel Dai let name = DName::from(name); 389004e86ffSlogin // 获得要删除的文件的inode 3901074eb34SSamuel Dai let to_delete = inode.children.get(&name).ok_or(SystemError::ENOENT)?; 391004e86ffSlogin if to_delete.0.lock().metadata.file_type == FileType::Dir { 392676b8ef6SMork return Err(SystemError::EPERM); 393004e86ffSlogin } 394004e86ffSlogin // 减少硬链接计数 395004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 396004e86ffSlogin // 在当前目录中删除这个子目录项 3971074eb34SSamuel Dai inode.children.remove(&name); 398004e86ffSlogin return Ok(()); 399004e86ffSlogin } 400004e86ffSlogin 401676b8ef6SMork fn rmdir(&self, name: &str) -> Result<(), SystemError> { 4021074eb34SSamuel Dai let name = DName::from(name); 403004e86ffSlogin let mut inode: SpinLockGuard<RamFSInode> = self.0.lock(); 404004e86ffSlogin // 如果当前inode不是目录,那么也没有子目录/文件的概念了,因此要求当前inode的类型是目录 405004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 406676b8ef6SMork return Err(SystemError::ENOTDIR); 407004e86ffSlogin } 408004e86ffSlogin // 获得要删除的文件夹的inode 4091074eb34SSamuel Dai let to_delete = inode.children.get(&name).ok_or(SystemError::ENOENT)?; 410004e86ffSlogin if to_delete.0.lock().metadata.file_type != FileType::Dir { 411676b8ef6SMork return Err(SystemError::ENOTDIR); 412004e86ffSlogin } 413004e86ffSlogin 414004e86ffSlogin to_delete.0.lock().metadata.nlinks -= 1; 415004e86ffSlogin // 在当前目录中删除这个子目录项 4161074eb34SSamuel Dai inode.children.remove(&name); 417004e86ffSlogin return Ok(()); 418004e86ffSlogin } 419004e86ffSlogin 4209e481b3bSTTaq fn move_to( 421004e86ffSlogin &self, 422004e86ffSlogin old_name: &str, 423004e86ffSlogin target: &Arc<dyn IndexNode>, 424004e86ffSlogin new_name: &str, 425676b8ef6SMork ) -> Result<(), SystemError> { 4261074eb34SSamuel Dai let inode_to_move = self 4271074eb34SSamuel Dai .find(old_name)? 4281074eb34SSamuel Dai .downcast_arc::<LockedRamFSInode>() 4291074eb34SSamuel Dai .ok_or(SystemError::EINVAL)?; 4301074eb34SSamuel Dai 4311074eb34SSamuel Dai let new_name = DName::from(new_name); 4321074eb34SSamuel Dai 4331074eb34SSamuel Dai inode_to_move.0.lock().name = new_name.clone(); 4341074eb34SSamuel Dai 4351074eb34SSamuel Dai let target_id = target.metadata()?.inode_id; 4361074eb34SSamuel Dai 4371074eb34SSamuel Dai let mut self_inode = self.0.lock(); 4381074eb34SSamuel Dai // 判断是否在同一目录下, 是则进行重命名 4391074eb34SSamuel Dai if target_id == self_inode.metadata.inode_id { 4401074eb34SSamuel Dai self_inode.children.remove(&DName::from(old_name)); 4411074eb34SSamuel Dai self_inode.children.insert(new_name, inode_to_move); 4421074eb34SSamuel Dai return Ok(()); 4431074eb34SSamuel Dai } 4441074eb34SSamuel Dai drop(self_inode); 4451074eb34SSamuel Dai 446924d64deSBrahmaMantra // 修改其对父节点的引用 4471074eb34SSamuel Dai inode_to_move.0.lock().parent = Arc::downgrade( 448924d64deSBrahmaMantra &target 449924d64deSBrahmaMantra .clone() 450924d64deSBrahmaMantra .downcast_arc::<LockedRamFSInode>() 4511074eb34SSamuel Dai .ok_or(SystemError::EINVAL)?, 452924d64deSBrahmaMantra ); 453004e86ffSlogin 454004e86ffSlogin // 在新的目录下创建一个硬链接 4551074eb34SSamuel Dai target.link(new_name.as_ref(), &(inode_to_move as Arc<dyn IndexNode>))?; 456924d64deSBrahmaMantra 457004e86ffSlogin // 取消现有的目录下的这个硬链接 458924d64deSBrahmaMantra if let Err(e) = self.unlink(old_name) { 459924d64deSBrahmaMantra // 当操作失败时回退操作 4601074eb34SSamuel Dai target.unlink(new_name.as_ref())?; 461924d64deSBrahmaMantra return Err(e); 462004e86ffSlogin } 463924d64deSBrahmaMantra 464004e86ffSlogin return Ok(()); 465004e86ffSlogin } 466004e86ffSlogin 467676b8ef6SMork fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> { 468004e86ffSlogin let inode = self.0.lock(); 469004e86ffSlogin 470004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 471676b8ef6SMork return Err(SystemError::ENOTDIR); 472004e86ffSlogin } 473004e86ffSlogin 474004e86ffSlogin match name { 475004e86ffSlogin "" | "." => { 476676b8ef6SMork return Ok(inode.self_ref.upgrade().ok_or(SystemError::ENOENT)?); 477004e86ffSlogin } 478004e86ffSlogin 479004e86ffSlogin ".." => { 480676b8ef6SMork return Ok(inode.parent.upgrade().ok_or(SystemError::ENOENT)?); 481004e86ffSlogin } 482004e86ffSlogin name => { 483004e86ffSlogin // 在子目录项中查找 4841074eb34SSamuel Dai let name = DName::from(name); 4851074eb34SSamuel Dai return Ok(inode 4861074eb34SSamuel Dai .children 4871074eb34SSamuel Dai .get(&name) 4881074eb34SSamuel Dai .ok_or(SystemError::ENOENT)? 4891074eb34SSamuel Dai .clone()); 490004e86ffSlogin } 491004e86ffSlogin } 492004e86ffSlogin } 493004e86ffSlogin 494676b8ef6SMork fn get_entry_name(&self, ino: InodeId) -> Result<String, SystemError> { 495004e86ffSlogin let inode: SpinLockGuard<RamFSInode> = self.0.lock(); 496004e86ffSlogin if inode.metadata.file_type != FileType::Dir { 497676b8ef6SMork return Err(SystemError::ENOTDIR); 498004e86ffSlogin } 499004e86ffSlogin 5006b4e7a29SLoGin match ino.into() { 501004e86ffSlogin 0 => { 502004e86ffSlogin return Ok(String::from(".")); 503004e86ffSlogin } 504004e86ffSlogin 1 => { 505004e86ffSlogin return Ok(String::from("..")); 506004e86ffSlogin } 507004e86ffSlogin ino => { 508004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 509004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 510004e86ffSlogin let mut key: Vec<String> = inode 511004e86ffSlogin .children 5121074eb34SSamuel Dai .iter() 5131074eb34SSamuel Dai .filter_map(|(k, v)| { 5141074eb34SSamuel Dai if v.0.lock().metadata.inode_id.into() == ino { 5151074eb34SSamuel Dai Some(k.to_string()) 5161074eb34SSamuel Dai } else { 5171074eb34SSamuel Dai None 5181074eb34SSamuel Dai } 5196b4e7a29SLoGin }) 520004e86ffSlogin .collect(); 521004e86ffSlogin 522004e86ffSlogin match key.len() { 523676b8ef6SMork 0=>{return Err(SystemError::ENOENT);} 524004e86ffSlogin 1=>{return Ok(key.remove(0));} 5256b4e7a29SLoGin _ => panic!("Ramfs get_entry_name: key.len()={key_len}>1, current inode_id={inode_id:?}, to find={to_find:?}", key_len=key.len(), inode_id = inode.metadata.inode_id, to_find=ino) 526004e86ffSlogin } 527004e86ffSlogin } 528004e86ffSlogin } 529004e86ffSlogin } 530004e86ffSlogin 531676b8ef6SMork fn list(&self) -> Result<Vec<String>, SystemError> { 532004e86ffSlogin let info = self.metadata()?; 533004e86ffSlogin if info.file_type != FileType::Dir { 534676b8ef6SMork return Err(SystemError::ENOTDIR); 535004e86ffSlogin } 536004e86ffSlogin 537004e86ffSlogin let mut keys: Vec<String> = Vec::new(); 538004e86ffSlogin keys.push(String::from(".")); 539004e86ffSlogin keys.push(String::from("..")); 5401074eb34SSamuel Dai keys.append( 5411074eb34SSamuel Dai &mut self 5421074eb34SSamuel Dai .0 5431074eb34SSamuel Dai .lock() 5441074eb34SSamuel Dai .children 5451074eb34SSamuel Dai .keys() 5461074eb34SSamuel Dai .map(|k| k.to_string()) 5471074eb34SSamuel Dai .collect(), 5481074eb34SSamuel Dai ); 549004e86ffSlogin 550004e86ffSlogin return Ok(keys); 551004e86ffSlogin } 5522dbef785SGnoCiYeH 5532dbef785SGnoCiYeH fn mknod( 5542dbef785SGnoCiYeH &self, 5552dbef785SGnoCiYeH filename: &str, 5562dbef785SGnoCiYeH mode: ModeType, 557c566df45SLoGin _dev_t: DeviceNumber, 5582dbef785SGnoCiYeH ) -> Result<Arc<dyn IndexNode>, SystemError> { 5592dbef785SGnoCiYeH let mut inode = self.0.lock(); 5602dbef785SGnoCiYeH if inode.metadata.file_type != FileType::Dir { 5612dbef785SGnoCiYeH return Err(SystemError::ENOTDIR); 5622dbef785SGnoCiYeH } 5632dbef785SGnoCiYeH 5642dbef785SGnoCiYeH // 判断需要创建的类型 5652dbef785SGnoCiYeH if unlikely(mode.contains(ModeType::S_IFREG)) { 5662dbef785SGnoCiYeH // 普通文件 567b5b571e0SLoGin return self.create(filename, FileType::File, mode); 5682dbef785SGnoCiYeH } 5692dbef785SGnoCiYeH 5701074eb34SSamuel Dai let filename = DName::from(filename); 5711074eb34SSamuel Dai 5722dbef785SGnoCiYeH let nod = Arc::new(LockedRamFSInode(SpinLock::new(RamFSInode { 5732dbef785SGnoCiYeH parent: inode.self_ref.clone(), 5742dbef785SGnoCiYeH self_ref: Weak::default(), 5752dbef785SGnoCiYeH children: BTreeMap::new(), 5762dbef785SGnoCiYeH data: Vec::new(), 5772dbef785SGnoCiYeH metadata: Metadata { 5782dbef785SGnoCiYeH dev_id: 0, 5792dbef785SGnoCiYeH inode_id: generate_inode_id(), 5802dbef785SGnoCiYeH size: 0, 5812dbef785SGnoCiYeH blk_size: 0, 5822dbef785SGnoCiYeH blocks: 0, 5836fc066acSJomo atime: PosixTimeSpec::default(), 5846fc066acSJomo mtime: PosixTimeSpec::default(), 5856fc066acSJomo ctime: PosixTimeSpec::default(), 5862dbef785SGnoCiYeH file_type: FileType::Pipe, 587b5b571e0SLoGin mode, 5882dbef785SGnoCiYeH nlinks: 1, 5892dbef785SGnoCiYeH uid: 0, 5902dbef785SGnoCiYeH gid: 0, 59102343d0bSLoGin raw_dev: DeviceNumber::default(), 5922dbef785SGnoCiYeH }, 5932dbef785SGnoCiYeH fs: inode.fs.clone(), 5942dbef785SGnoCiYeH special_node: None, 5951074eb34SSamuel Dai name: filename.clone(), 5962dbef785SGnoCiYeH }))); 5972dbef785SGnoCiYeH 5982dbef785SGnoCiYeH nod.0.lock().self_ref = Arc::downgrade(&nod); 5992dbef785SGnoCiYeH 6002dbef785SGnoCiYeH if mode.contains(ModeType::S_IFIFO) { 6012dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::Pipe; 6022dbef785SGnoCiYeH // 创建pipe文件 6032dbef785SGnoCiYeH let pipe_inode = LockedPipeInode::new(); 6042dbef785SGnoCiYeH // 设置special_node 6052dbef785SGnoCiYeH nod.0.lock().special_node = Some(SpecialNodeData::Pipe(pipe_inode)); 6062dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFBLK) { 6072dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::BlockDevice; 6082dbef785SGnoCiYeH unimplemented!() 6092dbef785SGnoCiYeH } else if mode.contains(ModeType::S_IFCHR) { 6102dbef785SGnoCiYeH nod.0.lock().metadata.file_type = FileType::CharDevice; 6112dbef785SGnoCiYeH unimplemented!() 6122dbef785SGnoCiYeH } 6132dbef785SGnoCiYeH 6141074eb34SSamuel Dai inode.children.insert(filename, nod.clone()); 6152dbef785SGnoCiYeH Ok(nod) 6162dbef785SGnoCiYeH } 6172dbef785SGnoCiYeH 6182dbef785SGnoCiYeH fn special_node(&self) -> Option<super::vfs::SpecialNodeData> { 6192dbef785SGnoCiYeH return self.0.lock().special_node.clone(); 6202dbef785SGnoCiYeH } 6219e481b3bSTTaq 6221074eb34SSamuel Dai fn dname(&self) -> Result<DName, SystemError> { 6231074eb34SSamuel Dai Ok(self.0.lock().name.clone()) 6249e481b3bSTTaq } 6259e481b3bSTTaq 6261074eb34SSamuel Dai fn parent(&self) -> Result<Arc<dyn IndexNode>, SystemError> { 6271074eb34SSamuel Dai self.0 6281074eb34SSamuel Dai .lock() 6291074eb34SSamuel Dai .parent 6301074eb34SSamuel Dai .upgrade() 6311074eb34SSamuel Dai .map(|item| item as Arc<dyn IndexNode>) 6321074eb34SSamuel Dai .ok_or(SystemError::EINVAL) 6339e481b3bSTTaq } 634004e86ffSlogin } 635