1 use core::{hint::spin_loop, sync::atomic::Ordering}; 2 3 use alloc::{format, string::ToString, sync::Arc}; 4 5 use crate::{ 6 driver::{ 7 base::block::disk_info::Partition, 8 disk::ahci::{self}, 9 }, 10 filesystem::{ 11 devfs::devfs_init, 12 fat::fs::FATFileSystem, 13 procfs::procfs_init, 14 ramfs::RamFS, 15 sysfs::sysfs_init, 16 vfs::{mount::MountFS, syscall::ModeType, AtomicInodeId, FileSystem, FileType}, 17 }, 18 include::bindings::bindings::PAGE_4K_SIZE, 19 kdebug, kerror, kinfo, 20 syscall::SystemError, 21 }; 22 23 use super::{file::FileMode, utils::rsplit_path, IndexNode, InodeId}; 24 25 /// @brief 原子地生成新的Inode号。 26 /// 请注意,所有的inode号都需要通过该函数来生成.全局的inode号,除了以下两个特殊的以外,都是唯一的 27 /// 特殊的两个inode号: 28 /// [0]: 对应'.'目录项 29 /// [1]: 对应'..'目录项 30 pub fn generate_inode_id() -> InodeId { 31 static INO: AtomicInodeId = AtomicInodeId::new(InodeId::new(1)); 32 return INO.fetch_add(InodeId::new(1), Ordering::SeqCst); 33 } 34 35 static mut __ROOT_INODE: Option<Arc<dyn IndexNode>> = None; 36 37 /// @brief 获取全局的根节点 38 #[inline(always)] 39 #[allow(non_snake_case)] 40 pub fn ROOT_INODE() -> Arc<dyn IndexNode> { 41 unsafe { 42 return __ROOT_INODE.as_ref().unwrap().clone(); 43 } 44 } 45 46 #[no_mangle] 47 pub extern "C" fn vfs_init() -> i32 { 48 // 使用Ramfs作为默认的根文件系统 49 let ramfs = RamFS::new(); 50 let mount_fs = MountFS::new(ramfs, None); 51 let root_inode = mount_fs.root_inode(); 52 53 unsafe { 54 __ROOT_INODE = Some(root_inode.clone()); 55 } 56 57 // 创建文件夹 58 root_inode 59 .create("proc", FileType::Dir, ModeType::from_bits_truncate(0o755)) 60 .expect("Failed to create /proc"); 61 root_inode 62 .create("dev", FileType::Dir, ModeType::from_bits_truncate(0o755)) 63 .expect("Failed to create /dev"); 64 root_inode 65 .create("sys", FileType::Dir, ModeType::from_bits_truncate(0o755)) 66 .expect("Failed to create /sys"); 67 kdebug!("dir in root:{:?}", root_inode.list()); 68 69 procfs_init().expect("Failed to initialize procfs"); 70 71 devfs_init().expect("Failed to initialize devfs"); 72 73 sysfs_init().expect("Failed to initialize sysfs"); 74 75 let root_entries = ROOT_INODE().list().expect("VFS init failed"); 76 if root_entries.len() > 0 { 77 kinfo!("Successfully initialized VFS!"); 78 } 79 return 0; 80 } 81 82 /// @brief 真正执行伪文件系统迁移的过程 83 /// 84 /// @param mountpoint_name 在根目录下的挂载点的名称 85 /// @param inode 原本的挂载点的inode 86 fn do_migrate( 87 new_root_inode: Arc<dyn IndexNode>, 88 mountpoint_name: &str, 89 fs: &MountFS, 90 ) -> Result<(), SystemError> { 91 let r = new_root_inode.find(mountpoint_name); 92 let mountpoint = if r.is_err() { 93 new_root_inode 94 .create( 95 mountpoint_name, 96 FileType::Dir, 97 ModeType::from_bits_truncate(0o755), 98 ) 99 .expect(format!("Failed to create '/{mountpoint_name}' in migrating").as_str()) 100 } else { 101 r.unwrap() 102 }; 103 // 迁移挂载点 104 mountpoint 105 .mount(fs.inner_filesystem()) 106 .expect(format!("Failed to migrate {mountpoint_name} ").as_str()); 107 return Ok(()); 108 } 109 110 /// @brief 迁移伪文件系统的inode 111 /// 请注意,为了避免删掉了伪文件系统内的信息,因此没有在原root inode那里调用unlink. 112 fn migrate_virtual_filesystem(new_fs: Arc<dyn FileSystem>) -> Result<(), SystemError> { 113 kinfo!("VFS: Migrating filesystems..."); 114 115 // ==== 在这里获取要被迁移的文件系统的inode === 116 let binding = ROOT_INODE().find("proc").expect("ProcFS not mounted!").fs(); 117 let proc: &MountFS = binding.as_any_ref().downcast_ref::<MountFS>().unwrap(); 118 let binding = ROOT_INODE().find("dev").expect("DevFS not mounted!").fs(); 119 let dev: &MountFS = binding.as_any_ref().downcast_ref::<MountFS>().unwrap(); 120 let binding = ROOT_INODE().find("sys").expect("SysFs not mounted!").fs(); 121 let sys: &MountFS = binding.as_any_ref().downcast_ref::<MountFS>().unwrap(); 122 123 let new_fs = MountFS::new(new_fs, None); 124 // 获取新的根文件系统的根节点的引用 125 let new_root_inode = new_fs.root_inode(); 126 127 // 把上述文件系统,迁移到新的文件系统下 128 do_migrate(new_root_inode.clone(), "proc", proc)?; 129 do_migrate(new_root_inode.clone(), "dev", dev)?; 130 do_migrate(new_root_inode.clone(), "sys", sys)?; 131 unsafe { 132 // drop旧的Root inode 133 let old_root_inode = __ROOT_INODE.take().unwrap(); 134 drop(old_root_inode); 135 136 // 设置全局的新的ROOT Inode 137 __ROOT_INODE = Some(new_root_inode); 138 } 139 140 kinfo!("VFS: Migrate filesystems done!"); 141 142 return Ok(()); 143 } 144 145 pub fn mount_root_fs() -> Result<(), SystemError> { 146 kinfo!("Try to mount FAT32 as root fs..."); 147 let partiton: Arc<Partition> = ahci::get_disks_by_name("ahci_disk_0".to_string()) 148 .unwrap() 149 .0 150 .lock() 151 .partitions[0] 152 .clone(); 153 154 let fatfs: Result<Arc<FATFileSystem>, SystemError> = FATFileSystem::new(partiton); 155 if fatfs.is_err() { 156 kerror!( 157 "Failed to initialize fatfs, code={:?}", 158 fatfs.as_ref().err() 159 ); 160 loop { 161 spin_loop(); 162 } 163 } 164 let fatfs: Arc<FATFileSystem> = fatfs.unwrap(); 165 let r = migrate_virtual_filesystem(fatfs); 166 if r.is_err() { 167 kerror!("Failed to migrate virtual filesystem to FAT32!"); 168 loop { 169 spin_loop(); 170 } 171 } 172 kinfo!("Successfully migrate rootfs to FAT32!"); 173 174 return Ok(()); 175 } 176 177 /// @brief 创建文件/文件夹 178 pub fn do_mkdir(path: &str, _mode: FileMode) -> Result<u64, SystemError> { 179 // 文件名过长 180 if path.len() > PAGE_4K_SIZE as usize { 181 return Err(SystemError::ENAMETOOLONG); 182 } 183 184 let inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().lookup(path); 185 186 if inode.is_err() { 187 let errno = inode.unwrap_err(); 188 // 文件不存在,且需要创建 189 if errno == SystemError::ENOENT { 190 let (filename, parent_path) = rsplit_path(path); 191 // 查找父目录 192 let parent_inode: Arc<dyn IndexNode> = 193 ROOT_INODE().lookup(parent_path.unwrap_or("/"))?; 194 // 创建文件夹 195 let _create_inode: Arc<dyn IndexNode> = parent_inode.create( 196 filename, 197 FileType::Dir, 198 ModeType::from_bits_truncate(0o755), 199 )?; 200 } else { 201 // 不需要创建文件,因此返回错误码 202 return Err(errno); 203 } 204 } 205 206 return Ok(0); 207 } 208 209 /// @brief 删除文件夹 210 pub fn do_remove_dir(path: &str) -> Result<u64, SystemError> { 211 // 文件名过长 212 if path.len() > PAGE_4K_SIZE as usize { 213 return Err(SystemError::ENAMETOOLONG); 214 } 215 216 let inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().lookup(path); 217 218 if inode.is_err() { 219 let errno = inode.unwrap_err(); 220 // 文件不存在 221 if errno == SystemError::ENOENT { 222 return Err(SystemError::ENOENT); 223 } 224 } 225 226 let (filename, parent_path) = rsplit_path(path); 227 // 查找父目录 228 let parent_inode: Arc<dyn IndexNode> = ROOT_INODE().lookup(parent_path.unwrap_or("/"))?; 229 230 if parent_inode.metadata()?.file_type != FileType::Dir { 231 return Err(SystemError::ENOTDIR); 232 } 233 234 let target_inode: Arc<dyn IndexNode> = parent_inode.find(filename)?; 235 if target_inode.metadata()?.file_type != FileType::Dir { 236 return Err(SystemError::ENOTDIR); 237 } 238 239 // 删除文件夹 240 parent_inode.rmdir(filename)?; 241 242 return Ok(0); 243 } 244 245 /// @brief 删除文件 246 pub fn do_unlink_at(path: &str, _mode: FileMode) -> Result<u64, SystemError> { 247 // 文件名过长 248 if path.len() > PAGE_4K_SIZE as usize { 249 return Err(SystemError::ENAMETOOLONG); 250 } 251 252 let inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().lookup(path); 253 254 if inode.is_err() { 255 let errno = inode.clone().unwrap_err(); 256 // 文件不存在,且需要创建 257 if errno == SystemError::ENOENT { 258 return Err(SystemError::ENOENT); 259 } 260 } 261 // 禁止在目录上unlink 262 if inode.unwrap().metadata()?.file_type == FileType::Dir { 263 return Err(SystemError::EPERM); 264 } 265 266 let (filename, parent_path) = rsplit_path(path); 267 // 查找父目录 268 let parent_inode: Arc<dyn IndexNode> = ROOT_INODE().lookup(parent_path.unwrap_or("/"))?; 269 270 if parent_inode.metadata()?.file_type != FileType::Dir { 271 return Err(SystemError::ENOTDIR); 272 } 273 274 // 删除文件 275 parent_inode.unlink(filename)?; 276 277 return Ok(0); 278 } 279