1004e86ffSlogin #![allow(dead_code)] 2004e86ffSlogin use core::{any::Any, fmt::Debug}; 3004e86ffSlogin 4004e86ffSlogin use alloc::{ 5004e86ffSlogin collections::BTreeMap, 6004e86ffSlogin string::String, 7004e86ffSlogin sync::{Arc, Weak}, 8004e86ffSlogin vec::Vec, 9004e86ffSlogin }; 10004e86ffSlogin 11004e86ffSlogin use crate::{ 12004e86ffSlogin filesystem::vfs::{ 13*73c607aaSYJwu2023 core::generate_inode_id, 14*73c607aaSYJwu2023 file::{FileMode, FilePrivateData}, 15*73c607aaSYJwu2023 FileSystem, FileType, IndexNode, InodeId, Metadata, PollStatus, 16004e86ffSlogin }, 17004e86ffSlogin include::bindings::bindings::{ 18004e86ffSlogin EFAULT, EINVAL, EISDIR, ENOENT, ENOSPC, ENOTDIR, ENOTEMPTY, ENOTSUP, EPERM, EROFS, 19004e86ffSlogin }, 20004e86ffSlogin io::{device::LBA_SIZE, disk_info::Partition, SeekFrom}, 21*73c607aaSYJwu2023 kerror, 22004e86ffSlogin libs::{ 23004e86ffSlogin spinlock::{SpinLock, SpinLockGuard}, 24004e86ffSlogin vec_cursor::VecCursor, 25004e86ffSlogin }, 26004e86ffSlogin time::TimeSpec, 27004e86ffSlogin }; 28004e86ffSlogin 29004e86ffSlogin use super::{ 30004e86ffSlogin bpb::{BiosParameterBlock, FATType}, 31004e86ffSlogin entry::{FATDir, FATDirEntry, FATDirIter, FATEntry}, 32004e86ffSlogin utils::RESERVED_CLUSTERS, 33004e86ffSlogin }; 34004e86ffSlogin 35004e86ffSlogin /// FAT32文件系统的最大的文件大小 36004e86ffSlogin pub const MAX_FILE_SIZE: u64 = 0xffff_ffff; 37004e86ffSlogin 38004e86ffSlogin /// @brief 表示当前簇和上一个簇的关系的结构体 39004e86ffSlogin /// 定义这样一个结构体的原因是,FAT文件系统的文件中,前后两个簇具有关联关系。 40004e86ffSlogin #[derive(Debug, Clone, Copy, Default)] 41004e86ffSlogin pub struct Cluster { 42004e86ffSlogin pub cluster_num: u64, 43004e86ffSlogin pub parent_cluster: u64, 44004e86ffSlogin } 45004e86ffSlogin 46004e86ffSlogin impl PartialOrd for Cluster { 47004e86ffSlogin /// @brief 根据当前簇号比较大小 48004e86ffSlogin fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { 49004e86ffSlogin return self.cluster_num.partial_cmp(&other.cluster_num); 50004e86ffSlogin } 51004e86ffSlogin } 52004e86ffSlogin 53004e86ffSlogin impl PartialEq for Cluster { 54004e86ffSlogin /// @brief 根据当前簇号比较是否相等 55004e86ffSlogin fn eq(&self, other: &Self) -> bool { 56004e86ffSlogin self.cluster_num == other.cluster_num 57004e86ffSlogin } 58004e86ffSlogin } 59004e86ffSlogin 60004e86ffSlogin impl Eq for Cluster {} 61004e86ffSlogin 62004e86ffSlogin #[derive(Debug)] 63004e86ffSlogin pub struct FATFileSystem { 64004e86ffSlogin /// 当前文件系统所在的分区 65004e86ffSlogin pub partition: Arc<Partition>, 66004e86ffSlogin /// 当前文件系统的BOPB 67004e86ffSlogin pub bpb: BiosParameterBlock, 68004e86ffSlogin /// 当前文件系统的第一个数据扇区(相对分区开始位置) 69004e86ffSlogin pub first_data_sector: u64, 70004e86ffSlogin /// 文件系统信息结构体 71004e86ffSlogin pub fs_info: Arc<LockedFATFsInfo>, 72004e86ffSlogin /// 文件系统的根inode 73004e86ffSlogin root_inode: Arc<LockedFATInode>, 74004e86ffSlogin } 75004e86ffSlogin 76004e86ffSlogin /// FAT文件系统的Inode 77004e86ffSlogin #[derive(Debug)] 78004e86ffSlogin pub struct LockedFATInode(SpinLock<FATInode>); 79004e86ffSlogin 80004e86ffSlogin #[derive(Debug)] 81004e86ffSlogin pub struct LockedFATFsInfo(SpinLock<FATFsInfo>); 82004e86ffSlogin 83004e86ffSlogin impl LockedFATFsInfo { 84004e86ffSlogin #[inline] 85004e86ffSlogin pub fn new(fs_info: FATFsInfo) -> Self { 86004e86ffSlogin return Self(SpinLock::new(fs_info)); 87004e86ffSlogin } 88004e86ffSlogin } 89004e86ffSlogin 90004e86ffSlogin #[derive(Debug)] 91004e86ffSlogin pub struct FATInode { 92004e86ffSlogin /// 指向父Inode的弱引用 93004e86ffSlogin parent: Weak<LockedFATInode>, 94004e86ffSlogin /// 指向自身的弱引用 95004e86ffSlogin self_ref: Weak<LockedFATInode>, 96004e86ffSlogin /// 子Inode的B树. 该数据结构用作缓存区。其中,它的key表示inode的名称。 97004e86ffSlogin /// 请注意,由于FAT的查询过程对大小写不敏感,因此我们选择让key全部是大写的,方便统一操作。 98004e86ffSlogin children: BTreeMap<String, Arc<LockedFATInode>>, 99004e86ffSlogin /// 当前inode的元数据 100004e86ffSlogin metadata: Metadata, 101004e86ffSlogin /// 指向inode所在的文件系统对象的指针 102004e86ffSlogin fs: Weak<FATFileSystem>, 103004e86ffSlogin 104004e86ffSlogin /// 根据不同的Inode类型,创建不同的私有字段 105004e86ffSlogin inode_type: FATDirEntry, 106004e86ffSlogin } 107004e86ffSlogin 108004e86ffSlogin impl FATInode { 109004e86ffSlogin /// @brief 更新当前inode的元数据 110004e86ffSlogin pub fn update_metadata(&mut self) { 111004e86ffSlogin // todo: 更新文件的访问时间等信息 112004e86ffSlogin match &self.inode_type { 113004e86ffSlogin FATDirEntry::File(f) | FATDirEntry::VolId(f) => { 114004e86ffSlogin self.metadata.size = f.size() as i64; 115004e86ffSlogin } 116004e86ffSlogin FATDirEntry::Dir(d) => { 117004e86ffSlogin self.metadata.size = d.size(&self.fs.upgrade().unwrap().clone()) as i64; 118004e86ffSlogin } 119004e86ffSlogin FATDirEntry::UnInit => { 120004e86ffSlogin kerror!("update_metadata: Uninitialized FATDirEntry: {:?}", self); 121004e86ffSlogin return; 122004e86ffSlogin } 123004e86ffSlogin }; 124004e86ffSlogin } 125004e86ffSlogin 126004e86ffSlogin fn find(&mut self, name: &str) -> Result<Arc<LockedFATInode>, i32> { 127004e86ffSlogin match &self.inode_type { 128004e86ffSlogin FATDirEntry::Dir(d) => { 129004e86ffSlogin // 尝试在缓存区查找 130004e86ffSlogin if let Some(entry) = self.children.get(&name.to_uppercase()) { 131004e86ffSlogin return Ok(entry.clone()); 132004e86ffSlogin } 133004e86ffSlogin // 在缓存区找不到 134004e86ffSlogin // 在磁盘查找 135004e86ffSlogin let fat_entry: FATDirEntry = 136004e86ffSlogin d.find_entry(name, None, None, self.fs.upgrade().unwrap())?; 137004e86ffSlogin // kdebug!("find entry from disk ok, entry={fat_entry:?}"); 138004e86ffSlogin // 创建新的inode 139004e86ffSlogin let entry_inode: Arc<LockedFATInode> = LockedFATInode::new( 140004e86ffSlogin self.fs.upgrade().unwrap(), 141004e86ffSlogin self.self_ref.clone(), 142004e86ffSlogin fat_entry, 143004e86ffSlogin ); 144004e86ffSlogin // 加入缓存区, 由于FAT文件系统的大小写不敏感问题,因此存入缓存区的key应当是全大写的 145004e86ffSlogin self.children 146004e86ffSlogin .insert(name.to_uppercase(), entry_inode.clone()); 147004e86ffSlogin return Ok(entry_inode); 148004e86ffSlogin } 149004e86ffSlogin FATDirEntry::UnInit => { 150004e86ffSlogin panic!( 151004e86ffSlogin "Uninitialized FAT Inode, fs = {:?}, inode={self:?}", 152004e86ffSlogin self.fs 153004e86ffSlogin ) 154004e86ffSlogin } 155004e86ffSlogin _ => { 156004e86ffSlogin return Err(-(ENOTDIR as i32)); 157004e86ffSlogin } 158004e86ffSlogin } 159004e86ffSlogin } 160004e86ffSlogin } 161004e86ffSlogin 162004e86ffSlogin impl LockedFATInode { 163004e86ffSlogin pub fn new( 164004e86ffSlogin fs: Arc<FATFileSystem>, 165004e86ffSlogin parent: Weak<LockedFATInode>, 166004e86ffSlogin inode_type: FATDirEntry, 167004e86ffSlogin ) -> Arc<LockedFATInode> { 168004e86ffSlogin let file_type = if let FATDirEntry::Dir(_) = inode_type { 169004e86ffSlogin FileType::Dir 170004e86ffSlogin } else { 171004e86ffSlogin FileType::File 172004e86ffSlogin }; 173004e86ffSlogin 174004e86ffSlogin let inode: Arc<LockedFATInode> = Arc::new(LockedFATInode(SpinLock::new(FATInode { 175004e86ffSlogin parent: parent, 176004e86ffSlogin self_ref: Weak::default(), 177004e86ffSlogin children: BTreeMap::new(), 178004e86ffSlogin fs: Arc::downgrade(&fs), 179004e86ffSlogin inode_type: inode_type, 180004e86ffSlogin metadata: Metadata { 181004e86ffSlogin dev_id: 0, 182004e86ffSlogin inode_id: generate_inode_id(), 183004e86ffSlogin size: 0, 184004e86ffSlogin blk_size: fs.bpb.bytes_per_sector as usize, 185004e86ffSlogin blocks: if let FATType::FAT32(_) = fs.bpb.fat_type { 186004e86ffSlogin fs.bpb.total_sectors_32 as usize 187004e86ffSlogin } else { 188004e86ffSlogin fs.bpb.total_sectors_16 as usize 189004e86ffSlogin }, 190004e86ffSlogin atime: TimeSpec::default(), 191004e86ffSlogin mtime: TimeSpec::default(), 192004e86ffSlogin ctime: TimeSpec::default(), 193004e86ffSlogin file_type: file_type, 194004e86ffSlogin mode: 0o777, 195004e86ffSlogin nlinks: 1, 196004e86ffSlogin uid: 0, 197004e86ffSlogin gid: 0, 198004e86ffSlogin raw_dev: 0, 199004e86ffSlogin }, 200004e86ffSlogin }))); 201004e86ffSlogin 202004e86ffSlogin inode.0.lock().self_ref = Arc::downgrade(&inode); 203004e86ffSlogin 204004e86ffSlogin inode.0.lock().update_metadata(); 205004e86ffSlogin 206004e86ffSlogin return inode; 207004e86ffSlogin } 208004e86ffSlogin } 209004e86ffSlogin 210004e86ffSlogin /// FsInfo结构体(内存中的一份拷贝,当卸载卷或者sync的时候,把它写入磁盘) 211004e86ffSlogin #[derive(Debug)] 212004e86ffSlogin pub struct FATFsInfo { 213004e86ffSlogin /// Lead Signature - must equal 0x41615252 214004e86ffSlogin lead_sig: u32, 215004e86ffSlogin /// Value must equal 0x61417272 216004e86ffSlogin struc_sig: u32, 217004e86ffSlogin /// 空闲簇数目 218004e86ffSlogin free_count: u32, 219004e86ffSlogin /// 第一个空闲簇的位置(不一定准确,仅供加速查找) 220004e86ffSlogin next_free: u32, 221004e86ffSlogin /// 0xAA550000 222004e86ffSlogin trail_sig: u32, 223004e86ffSlogin /// Dirty flag to flush to disk 224004e86ffSlogin dirty: bool, 225004e86ffSlogin /// FsInfo Structure 在磁盘上的字节偏移量 226004e86ffSlogin /// Not present for FAT12 and FAT16 227004e86ffSlogin offset: Option<u64>, 228004e86ffSlogin } 229004e86ffSlogin 230004e86ffSlogin impl FileSystem for FATFileSystem { 231004e86ffSlogin fn root_inode(&self) -> Arc<dyn crate::filesystem::vfs::IndexNode> { 232004e86ffSlogin return self.root_inode.clone(); 233004e86ffSlogin } 234004e86ffSlogin 235004e86ffSlogin fn info(&self) -> crate::filesystem::vfs::FsInfo { 236004e86ffSlogin todo!() 237004e86ffSlogin } 238004e86ffSlogin 239004e86ffSlogin /// @brief 本函数用于实现动态转换。 240004e86ffSlogin /// 具体的文件系统在实现本函数时,最简单的方式就是:直接返回self 241004e86ffSlogin fn as_any_ref(&self) -> &dyn Any { 242004e86ffSlogin self 243004e86ffSlogin } 244004e86ffSlogin } 245004e86ffSlogin 246004e86ffSlogin impl FATFileSystem { 247004e86ffSlogin pub fn new(partition: Arc<Partition>) -> Result<Arc<FATFileSystem>, i32> { 248004e86ffSlogin let bpb = BiosParameterBlock::new(partition.clone())?; 249004e86ffSlogin 250004e86ffSlogin // 从磁盘上读取FAT32文件系统的FsInfo结构体 251004e86ffSlogin let fs_info: FATFsInfo = match bpb.fat_type { 252004e86ffSlogin FATType::FAT32(bpb32) => { 253004e86ffSlogin let fs_info_in_disk_bytes_offset = partition.lba_start * LBA_SIZE as u64 254004e86ffSlogin + bpb32.fs_info as u64 * bpb.bytes_per_sector as u64; 255004e86ffSlogin FATFsInfo::new( 256004e86ffSlogin partition.clone(), 257004e86ffSlogin fs_info_in_disk_bytes_offset, 258004e86ffSlogin bpb.bytes_per_sector as usize, 259004e86ffSlogin )? 260004e86ffSlogin } 261004e86ffSlogin _ => FATFsInfo::default(), 262004e86ffSlogin }; 263004e86ffSlogin 264004e86ffSlogin // 根目录项占用的扇区数(向上取整) 265004e86ffSlogin let root_dir_sectors: u64 = ((bpb.root_entries_cnt as u64 * 32) 266004e86ffSlogin + (bpb.bytes_per_sector as u64 - 1)) 267004e86ffSlogin / (bpb.bytes_per_sector as u64); 268004e86ffSlogin 269004e86ffSlogin // FAT表大小(单位:扇区) 270004e86ffSlogin let fat_size = if bpb.fat_size_16 != 0 { 271004e86ffSlogin bpb.fat_size_16 as u64 272004e86ffSlogin } else { 273004e86ffSlogin match bpb.fat_type { 274004e86ffSlogin FATType::FAT32(x) => x.fat_size_32 as u64, 275004e86ffSlogin _ => { 276004e86ffSlogin kerror!("FAT12 and FAT16 volumes should have non-zero BPB_FATSz16"); 277004e86ffSlogin return Err(-(EINVAL as i32)); 278004e86ffSlogin } 279004e86ffSlogin } 280004e86ffSlogin }; 281004e86ffSlogin 282004e86ffSlogin let first_data_sector = 283004e86ffSlogin bpb.rsvd_sec_cnt as u64 + (bpb.num_fats as u64 * fat_size) + root_dir_sectors; 284004e86ffSlogin 285004e86ffSlogin // 创建文件系统的根节点 286004e86ffSlogin let root_inode: Arc<LockedFATInode> = Arc::new(LockedFATInode(SpinLock::new(FATInode { 287004e86ffSlogin parent: Weak::default(), 288004e86ffSlogin self_ref: Weak::default(), 289004e86ffSlogin children: BTreeMap::new(), 290004e86ffSlogin fs: Weak::default(), 291004e86ffSlogin inode_type: FATDirEntry::UnInit, 292004e86ffSlogin metadata: Metadata { 293004e86ffSlogin dev_id: 0, 294004e86ffSlogin inode_id: generate_inode_id(), 295004e86ffSlogin size: 0, 296004e86ffSlogin blk_size: bpb.bytes_per_sector as usize, 297004e86ffSlogin blocks: if let FATType::FAT32(_) = bpb.fat_type { 298004e86ffSlogin bpb.total_sectors_32 as usize 299004e86ffSlogin } else { 300004e86ffSlogin bpb.total_sectors_16 as usize 301004e86ffSlogin }, 302004e86ffSlogin atime: TimeSpec::default(), 303004e86ffSlogin mtime: TimeSpec::default(), 304004e86ffSlogin ctime: TimeSpec::default(), 305004e86ffSlogin file_type: FileType::Dir, 306004e86ffSlogin mode: 0o777, 307004e86ffSlogin nlinks: 1, 308004e86ffSlogin uid: 0, 309004e86ffSlogin gid: 0, 310004e86ffSlogin raw_dev: 0, 311004e86ffSlogin }, 312004e86ffSlogin }))); 313004e86ffSlogin 314004e86ffSlogin let result: Arc<FATFileSystem> = Arc::new(FATFileSystem { 315004e86ffSlogin partition: partition, 316004e86ffSlogin bpb, 317004e86ffSlogin first_data_sector, 318004e86ffSlogin fs_info: Arc::new(LockedFATFsInfo::new(fs_info)), 319004e86ffSlogin root_inode: root_inode, 320004e86ffSlogin }); 321004e86ffSlogin 322004e86ffSlogin // 对root inode加锁,并继续完成初始化工作 323004e86ffSlogin let mut root_guard: SpinLockGuard<FATInode> = result.root_inode.0.lock(); 324004e86ffSlogin root_guard.inode_type = FATDirEntry::Dir(result.root_dir()); 325004e86ffSlogin root_guard.parent = Arc::downgrade(&result.root_inode); 326004e86ffSlogin root_guard.self_ref = Arc::downgrade(&result.root_inode); 327004e86ffSlogin root_guard.fs = Arc::downgrade(&result); 328004e86ffSlogin // 释放锁 329004e86ffSlogin drop(root_guard); 330004e86ffSlogin 331004e86ffSlogin return Ok(result); 332004e86ffSlogin } 333004e86ffSlogin 334004e86ffSlogin /// @brief 计算每个簇有多少个字节 335004e86ffSlogin #[inline] 336004e86ffSlogin pub fn bytes_per_cluster(&self) -> u64 { 337004e86ffSlogin return (self.bpb.bytes_per_sector as u64) * (self.bpb.sector_per_cluster as u64); 338004e86ffSlogin } 339004e86ffSlogin 340004e86ffSlogin /// @brief 读取当前簇在FAT表中存储的信息 341004e86ffSlogin /// 342004e86ffSlogin /// @param cluster 当前簇 343004e86ffSlogin /// 344004e86ffSlogin /// @return Ok(FATEntry) 当前簇在FAT表中,存储的信息。(详情见FATEntry的注释) 345004e86ffSlogin /// @return Err(i32) 错误码 346004e86ffSlogin pub fn get_fat_entry(&self, cluster: Cluster) -> Result<FATEntry, i32> { 347004e86ffSlogin let current_cluster = cluster.cluster_num; 348004e86ffSlogin 349004e86ffSlogin let fat_type: FATType = self.bpb.fat_type; 350004e86ffSlogin // 获取FAT表的起始扇区(相对分区起始扇区的偏移量) 351004e86ffSlogin let fat_start_sector = self.fat_start_sector(); 352004e86ffSlogin let bytes_per_sec = self.bpb.bytes_per_sector as u64; 353004e86ffSlogin 354004e86ffSlogin // cluster对应的FAT表项在分区内的字节偏移量 355004e86ffSlogin let fat_bytes_offset = 356004e86ffSlogin fat_type.get_fat_bytes_offset(cluster, fat_start_sector, bytes_per_sec); 357004e86ffSlogin 358004e86ffSlogin // FAT表项所在的LBA地址 359004e86ffSlogin // let fat_ent_lba = self.get_lba_from_offset(self.bytes_to_sector(fat_bytes_offset)); 360004e86ffSlogin let fat_ent_lba = self.partition.lba_start + fat_bytes_offset / LBA_SIZE as u64; 361004e86ffSlogin 362004e86ffSlogin // FAT表项在逻辑块内的字节偏移量 363004e86ffSlogin let blk_offset = self.get_in_block_offset(fat_bytes_offset); 364004e86ffSlogin 365004e86ffSlogin let mut v = Vec::<u8>::new(); 366004e86ffSlogin v.resize(self.bpb.bytes_per_sector as usize, 0); 367004e86ffSlogin self.partition 368004e86ffSlogin .disk() 369004e86ffSlogin .read_at(fat_ent_lba as usize, 1 * self.lba_per_sector(), &mut v)?; 370004e86ffSlogin 371004e86ffSlogin let mut cursor = VecCursor::new(v); 372004e86ffSlogin cursor.seek(SeekFrom::SeekSet(blk_offset as i64))?; 373004e86ffSlogin 374004e86ffSlogin let res: FATEntry = match self.bpb.fat_type { 375004e86ffSlogin FATType::FAT12(_) => { 376004e86ffSlogin let mut entry = cursor.read_u16()?; 377004e86ffSlogin // 由于FAT12文件系统的FAT表,每个entry占用1.5字节,因此奇数的簇需要取高12位的值。 378004e86ffSlogin if (current_cluster & 1) > 0 { 379004e86ffSlogin entry >>= 4; 380004e86ffSlogin } else { 381004e86ffSlogin entry &= 0x0fff; 382004e86ffSlogin } 383004e86ffSlogin 384004e86ffSlogin if entry == 0 { 385004e86ffSlogin FATEntry::Unused 386004e86ffSlogin } else if entry == 0x0ff7 { 387004e86ffSlogin FATEntry::Bad 388004e86ffSlogin } else if entry >= 0x0ff8 { 389004e86ffSlogin FATEntry::EndOfChain 390004e86ffSlogin } else { 391004e86ffSlogin FATEntry::Next(Cluster { 392004e86ffSlogin cluster_num: entry as u64, 393004e86ffSlogin parent_cluster: current_cluster, 394004e86ffSlogin }) 395004e86ffSlogin } 396004e86ffSlogin } 397004e86ffSlogin FATType::FAT16(_) => { 398004e86ffSlogin let entry = cursor.read_u16()?; 399004e86ffSlogin 400004e86ffSlogin if entry == 0 { 401004e86ffSlogin FATEntry::Unused 402004e86ffSlogin } else if entry == 0xfff7 { 403004e86ffSlogin FATEntry::Bad 404004e86ffSlogin } else if entry >= 0xfff8 { 405004e86ffSlogin FATEntry::EndOfChain 406004e86ffSlogin } else { 407004e86ffSlogin FATEntry::Next(Cluster { 408004e86ffSlogin cluster_num: entry as u64, 409004e86ffSlogin parent_cluster: current_cluster, 410004e86ffSlogin }) 411004e86ffSlogin } 412004e86ffSlogin } 413004e86ffSlogin FATType::FAT32(_) => { 414004e86ffSlogin let entry = cursor.read_u32()? & 0x0fffffff; 415004e86ffSlogin 416004e86ffSlogin match entry { 417004e86ffSlogin _n if (current_cluster >= 0x0ffffff7 && current_cluster <= 0x0fffffff) => { 418004e86ffSlogin // 当前簇号不是一个能被获得的簇(可能是文件系统出错了) 419004e86ffSlogin kerror!("FAT32 get fat entry: current cluster number [{}] is not an allocatable cluster number.", current_cluster); 420004e86ffSlogin FATEntry::Bad 421004e86ffSlogin } 422004e86ffSlogin 0 => FATEntry::Unused, 423004e86ffSlogin 0x0ffffff7 => FATEntry::Bad, 424004e86ffSlogin 0x0ffffff8..=0x0fffffff => FATEntry::EndOfChain, 425004e86ffSlogin _n => FATEntry::Next(Cluster { 426004e86ffSlogin cluster_num: entry as u64, 427004e86ffSlogin parent_cluster: current_cluster, 428004e86ffSlogin }), 429004e86ffSlogin } 430004e86ffSlogin } 431004e86ffSlogin }; 432004e86ffSlogin return Ok(res); 433004e86ffSlogin } 434004e86ffSlogin 435004e86ffSlogin /// @brief 读取当前簇在FAT表中存储的信息(直接返回读取到的值,而不加处理) 436004e86ffSlogin /// 437004e86ffSlogin /// @param cluster 当前簇 438004e86ffSlogin /// 439004e86ffSlogin /// @return Ok(u64) 当前簇在FAT表中,存储的信息。 440004e86ffSlogin /// @return Err(i32) 错误码 441004e86ffSlogin pub fn get_fat_entry_raw(&self, cluster: Cluster) -> Result<u64, i32> { 442004e86ffSlogin let current_cluster = cluster.cluster_num; 443004e86ffSlogin 444004e86ffSlogin let fat_type: FATType = self.bpb.fat_type; 445004e86ffSlogin // 获取FAT表的起始扇区(相对分区起始扇区的偏移量) 446004e86ffSlogin let fat_start_sector = self.fat_start_sector(); 447004e86ffSlogin let bytes_per_sec = self.bpb.bytes_per_sector as u64; 448004e86ffSlogin 449004e86ffSlogin // cluster对应的FAT表项在分区内的字节偏移量 450004e86ffSlogin let fat_bytes_offset = 451004e86ffSlogin fat_type.get_fat_bytes_offset(cluster, fat_start_sector, bytes_per_sec); 452004e86ffSlogin 453004e86ffSlogin // FAT表项所在的LBA地址 454004e86ffSlogin let fat_ent_lba = self.get_lba_from_offset(self.bytes_to_sector(fat_bytes_offset)); 455004e86ffSlogin 456004e86ffSlogin // FAT表项在逻辑块内的字节偏移量 457004e86ffSlogin let blk_offset = self.get_in_block_offset(fat_bytes_offset); 458004e86ffSlogin 459004e86ffSlogin let mut v = Vec::<u8>::new(); 460004e86ffSlogin v.resize(self.bpb.bytes_per_sector as usize, 0); 461004e86ffSlogin self.partition 462004e86ffSlogin .disk() 463004e86ffSlogin .read_at(fat_ent_lba, 1 * self.lba_per_sector(), &mut v)?; 464004e86ffSlogin 465004e86ffSlogin let mut cursor = VecCursor::new(v); 466004e86ffSlogin cursor.seek(SeekFrom::SeekSet(blk_offset as i64))?; 467004e86ffSlogin 468004e86ffSlogin let res = match self.bpb.fat_type { 469004e86ffSlogin FATType::FAT12(_) => { 470004e86ffSlogin let mut entry = cursor.read_u16()?; 471004e86ffSlogin entry = if (current_cluster & 0x0001) > 0 { 472004e86ffSlogin entry >> 4 473004e86ffSlogin } else { 474004e86ffSlogin entry & 0x0fff 475004e86ffSlogin }; 476004e86ffSlogin entry as u64 477004e86ffSlogin } 478004e86ffSlogin FATType::FAT16(_) => { 479004e86ffSlogin let entry = (cursor.read_u16()?) as u64; 480004e86ffSlogin entry 481004e86ffSlogin } 482004e86ffSlogin FATType::FAT32(_) => { 483004e86ffSlogin let entry = cursor.read_u32()? & 0x0fff_ffff; 484004e86ffSlogin entry as u64 485004e86ffSlogin } 486004e86ffSlogin }; 487004e86ffSlogin 488004e86ffSlogin return Ok(res); 489004e86ffSlogin } 490004e86ffSlogin 491004e86ffSlogin /// @brief 获取当前文件系统的root inode,在磁盘上的字节偏移量 492004e86ffSlogin pub fn root_dir_bytes_offset(&self) -> u64 { 493004e86ffSlogin match self.bpb.fat_type { 494004e86ffSlogin FATType::FAT32(s) => { 495004e86ffSlogin let first_sec_cluster: u64 = (s.root_cluster as u64 - 2) 496004e86ffSlogin * (self.bpb.sector_per_cluster as u64) 497004e86ffSlogin + self.first_data_sector; 498004e86ffSlogin return (self.get_lba_from_offset(first_sec_cluster) * LBA_SIZE) as u64; 499004e86ffSlogin } 500004e86ffSlogin _ => { 501004e86ffSlogin let root_sec = (self.bpb.rsvd_sec_cnt as u64) 502004e86ffSlogin + (self.bpb.num_fats as u64) * (self.bpb.fat_size_16 as u64); 503004e86ffSlogin return (self.get_lba_from_offset(root_sec) * LBA_SIZE) as u64; 504004e86ffSlogin } 505004e86ffSlogin } 506004e86ffSlogin } 507004e86ffSlogin 508004e86ffSlogin /// @brief 获取当前文件系统的根目录项区域的结束位置,在磁盘上的字节偏移量。 509004e86ffSlogin /// 请注意,当前函数只对FAT12/FAT16生效。对于FAT32,返回None 510004e86ffSlogin pub fn root_dir_end_bytes_offset(&self) -> Option<u64> { 511004e86ffSlogin match self.bpb.fat_type { 512004e86ffSlogin FATType::FAT12(_) | FATType::FAT16(_) => { 513004e86ffSlogin return Some( 514004e86ffSlogin self.root_dir_bytes_offset() + (self.bpb.root_entries_cnt as u64) * 32, 515004e86ffSlogin ); 516004e86ffSlogin } 517004e86ffSlogin _ => { 518004e86ffSlogin return None; 519004e86ffSlogin } 520004e86ffSlogin } 521004e86ffSlogin } 522004e86ffSlogin 523004e86ffSlogin /// @brief 获取簇在磁盘内的字节偏移量(相对磁盘起始位置。注意,不是分区内偏移量) 524004e86ffSlogin pub fn cluster_bytes_offset(&self, cluster: Cluster) -> u64 { 525004e86ffSlogin if cluster.cluster_num >= 2 { 526004e86ffSlogin // 指定簇的第一个扇区号 527004e86ffSlogin let first_sec_of_cluster = (cluster.cluster_num - 2) 528004e86ffSlogin * (self.bpb.sector_per_cluster as u64) 529004e86ffSlogin + self.first_data_sector; 530004e86ffSlogin return (self.get_lba_from_offset(first_sec_of_cluster) * LBA_SIZE) as u64; 531004e86ffSlogin } else { 532004e86ffSlogin return 0; 533004e86ffSlogin } 534004e86ffSlogin } 535004e86ffSlogin 536004e86ffSlogin /// @brief 获取一个空闲簇 537004e86ffSlogin /// 538004e86ffSlogin /// @param prev_cluster 簇链的前一个簇。本函数将会把新获取的簇,连接到它的后面。 539004e86ffSlogin /// 540004e86ffSlogin /// @return Ok(Cluster) 新获取的空闲簇 541004e86ffSlogin /// @return Err(i32) 错误码 542004e86ffSlogin pub fn allocate_cluster(&self, prev_cluster: Option<Cluster>) -> Result<Cluster, i32> { 543004e86ffSlogin let end_cluster: Cluster = self.max_cluster_number(); 544004e86ffSlogin let start_cluster: Cluster = match self.bpb.fat_type { 545004e86ffSlogin FATType::FAT32(_) => { 546004e86ffSlogin let next_free: u64 = match self.fs_info.0.lock().next_free() { 547004e86ffSlogin Some(x) => x, 548004e86ffSlogin None => 0xffffffff, 549004e86ffSlogin }; 550004e86ffSlogin if next_free < end_cluster.cluster_num { 551004e86ffSlogin Cluster::new(next_free) 552004e86ffSlogin } else { 553004e86ffSlogin Cluster::new(RESERVED_CLUSTERS as u64) 554004e86ffSlogin } 555004e86ffSlogin } 556004e86ffSlogin _ => Cluster::new(RESERVED_CLUSTERS as u64), 557004e86ffSlogin }; 558004e86ffSlogin 559004e86ffSlogin // 寻找一个空的簇 560004e86ffSlogin let free_cluster: Cluster = match self.get_free_cluster(start_cluster, end_cluster) { 561004e86ffSlogin Ok(c) => c, 562004e86ffSlogin Err(_) if start_cluster.cluster_num > RESERVED_CLUSTERS as u64 => { 563004e86ffSlogin self.get_free_cluster(Cluster::new(RESERVED_CLUSTERS as u64), end_cluster)? 564004e86ffSlogin } 565004e86ffSlogin Err(e) => return Err(e), 566004e86ffSlogin }; 567004e86ffSlogin 568004e86ffSlogin self.set_entry(free_cluster, FATEntry::EndOfChain)?; 569004e86ffSlogin // 减少空闲簇计数 570004e86ffSlogin self.fs_info.0.lock().update_free_count_delta(-1); 571004e86ffSlogin // 更新搜索空闲簇的参考量 572004e86ffSlogin self.fs_info 573004e86ffSlogin .0 574004e86ffSlogin .lock() 575004e86ffSlogin .update_next_free((free_cluster.cluster_num + 1) as u32); 576004e86ffSlogin 577004e86ffSlogin // 如果这个空闲簇不是簇链的第一个簇,那么把当前簇跟前一个簇连上。 578004e86ffSlogin if let Some(prev_cluster) = prev_cluster { 579004e86ffSlogin // kdebug!("set entry, prev ={prev_cluster:?}, next = {free_cluster:?}"); 580004e86ffSlogin self.set_entry(prev_cluster, FATEntry::Next(free_cluster))?; 581004e86ffSlogin } 582004e86ffSlogin // 清空新获取的这个簇 583004e86ffSlogin self.zero_cluster(free_cluster)?; 584004e86ffSlogin return Ok(free_cluster); 585004e86ffSlogin } 586004e86ffSlogin 587004e86ffSlogin /// @brief 释放簇链上的所有簇 588004e86ffSlogin /// 589004e86ffSlogin /// @param start_cluster 簇链的第一个簇 590004e86ffSlogin pub fn deallocate_cluster_chain(&self, start_cluster: Cluster) -> Result<(), i32> { 591004e86ffSlogin let clusters: Vec<Cluster> = self.clusters(start_cluster); 592004e86ffSlogin for c in clusters { 593004e86ffSlogin self.deallocate_cluster(c)?; 594004e86ffSlogin } 595004e86ffSlogin return Ok(()); 596004e86ffSlogin } 597004e86ffSlogin 598004e86ffSlogin /// @brief 释放簇 599004e86ffSlogin /// 600004e86ffSlogin /// @param 要释放的簇 601004e86ffSlogin pub fn deallocate_cluster(&self, cluster: Cluster) -> Result<(), i32> { 602004e86ffSlogin let entry: FATEntry = self.get_fat_entry(cluster)?; 603004e86ffSlogin // 如果不是坏簇 604004e86ffSlogin if entry != FATEntry::Bad { 605004e86ffSlogin self.set_entry(cluster, FATEntry::Unused)?; 606004e86ffSlogin self.fs_info.0.lock().update_free_count_delta(1); 607004e86ffSlogin // 安全选项:清空被释放的簇 608004e86ffSlogin #[cfg(feature = "secure")] 609004e86ffSlogin self.zero_cluster(cluster)?; 610004e86ffSlogin return Ok(()); 611004e86ffSlogin } else { 612004e86ffSlogin // 不能释放坏簇 613004e86ffSlogin kerror!("Bad clusters cannot be freed."); 614004e86ffSlogin return Err(-(EFAULT as i32)); 615004e86ffSlogin } 616004e86ffSlogin } 617004e86ffSlogin 618004e86ffSlogin /// @brief 获取文件系统的根目录项 619004e86ffSlogin pub fn root_dir(&self) -> FATDir { 620004e86ffSlogin match self.bpb.fat_type { 621004e86ffSlogin FATType::FAT32(s) => { 622004e86ffSlogin return FATDir { 623004e86ffSlogin first_cluster: Cluster::new(s.root_cluster as u64), 624004e86ffSlogin dir_name: String::from("/"), 625004e86ffSlogin root_offset: None, 626004e86ffSlogin short_dir_entry: None, 627004e86ffSlogin loc: None, 628004e86ffSlogin }; 629004e86ffSlogin } 630004e86ffSlogin _ => FATDir { 631004e86ffSlogin first_cluster: Cluster::new(0), 632004e86ffSlogin dir_name: String::from("/"), 633004e86ffSlogin root_offset: Some(self.root_dir_bytes_offset()), 634004e86ffSlogin short_dir_entry: None, 635004e86ffSlogin loc: None, 636004e86ffSlogin }, 637004e86ffSlogin } 638004e86ffSlogin } 639004e86ffSlogin 640004e86ffSlogin /// @brief 获取FAT表的起始扇区(相对分区起始扇区的偏移量) 641004e86ffSlogin pub fn fat_start_sector(&self) -> u64 { 642004e86ffSlogin let active_fat = self.active_fat(); 643004e86ffSlogin let fat_size = self.fat_size(); 644004e86ffSlogin return self.bpb.rsvd_sec_cnt as u64 + active_fat * fat_size; 645004e86ffSlogin } 646004e86ffSlogin 647004e86ffSlogin /// @brief 获取当前活动的FAT表 648004e86ffSlogin pub fn active_fat(&self) -> u64 { 649004e86ffSlogin if self.mirroring_enabled() { 650004e86ffSlogin return 0; 651004e86ffSlogin } else { 652004e86ffSlogin match self.bpb.fat_type { 653004e86ffSlogin FATType::FAT32(bpb32) => { 654004e86ffSlogin return (bpb32.ext_flags & 0x0f) as u64; 655004e86ffSlogin } 656004e86ffSlogin _ => { 657004e86ffSlogin return 0; 658004e86ffSlogin } 659004e86ffSlogin } 660004e86ffSlogin } 661004e86ffSlogin } 662004e86ffSlogin 663004e86ffSlogin /// @brief 获取当前文件系统的每个FAT表的大小 664004e86ffSlogin pub fn fat_size(&self) -> u64 { 665004e86ffSlogin if self.bpb.fat_size_16 != 0 { 666004e86ffSlogin return self.bpb.fat_size_16 as u64; 667004e86ffSlogin } else { 668004e86ffSlogin match self.bpb.fat_type { 669004e86ffSlogin FATType::FAT32(bpb32) => { 670004e86ffSlogin return bpb32.fat_size_32 as u64; 671004e86ffSlogin } 672004e86ffSlogin 673004e86ffSlogin _ => { 674004e86ffSlogin panic!("FAT12 and FAT16 volumes should have non-zero BPB_FATSz16"); 675004e86ffSlogin } 676004e86ffSlogin } 677004e86ffSlogin } 678004e86ffSlogin } 679004e86ffSlogin 680004e86ffSlogin /// @brief 判断当前文件系统是否启用了FAT表镜像 681004e86ffSlogin pub fn mirroring_enabled(&self) -> bool { 682004e86ffSlogin match self.bpb.fat_type { 683004e86ffSlogin FATType::FAT32(bpb32) => { 684004e86ffSlogin return (bpb32.ext_flags & 0x80) == 0; 685004e86ffSlogin } 686004e86ffSlogin _ => { 687004e86ffSlogin return false; 688004e86ffSlogin } 689004e86ffSlogin } 690004e86ffSlogin } 691004e86ffSlogin 692004e86ffSlogin /// @brief 根据分区内的扇区偏移量,获得在磁盘上的LBA地址 693004e86ffSlogin #[inline] 694004e86ffSlogin pub fn get_lba_from_offset(&self, in_partition_sec_offset: u64) -> usize { 695004e86ffSlogin return (self.partition.lba_start 696004e86ffSlogin + in_partition_sec_offset * (self.bpb.bytes_per_sector as u64 / LBA_SIZE as u64)) 697004e86ffSlogin as usize; 698004e86ffSlogin } 699004e86ffSlogin 700004e86ffSlogin /// @brief 获取每个扇区占用多少个LBA 701004e86ffSlogin #[inline] 702004e86ffSlogin pub fn lba_per_sector(&self) -> usize { 703004e86ffSlogin return self.bpb.bytes_per_sector as usize / LBA_SIZE; 704004e86ffSlogin } 705004e86ffSlogin 706004e86ffSlogin /// @brief 将分区内字节偏移量转换为扇区偏移量 707004e86ffSlogin #[inline] 708004e86ffSlogin pub fn bytes_to_sector(&self, in_partition_bytes_offset: u64) -> u64 { 709004e86ffSlogin return in_partition_bytes_offset / (self.bpb.bytes_per_sector as u64); 710004e86ffSlogin } 711004e86ffSlogin 712004e86ffSlogin /// @brief 根据磁盘上的字节偏移量,获取对应位置在分区内的字节偏移量 713004e86ffSlogin #[inline] 714004e86ffSlogin pub fn get_in_partition_bytes_offset(&self, disk_bytes_offset: u64) -> u64 { 715004e86ffSlogin return disk_bytes_offset - (self.partition.lba_start * LBA_SIZE as u64); 716004e86ffSlogin } 717004e86ffSlogin 718004e86ffSlogin /// @brief 根据字节偏移量计算在逻辑块内的字节偏移量 719004e86ffSlogin #[inline] 720004e86ffSlogin pub fn get_in_block_offset(&self, bytes_offset: u64) -> u64 { 721004e86ffSlogin return bytes_offset % LBA_SIZE as u64; 722004e86ffSlogin } 723004e86ffSlogin 724004e86ffSlogin /// @brief 获取在FAT表中,以start_cluster开头的FAT链的所有簇的信息 725004e86ffSlogin /// 726004e86ffSlogin /// @param start_cluster 整个FAT链的起始簇号 727004e86ffSlogin pub fn clusters(&self, start_cluster: Cluster) -> Vec<Cluster> { 728004e86ffSlogin return self.cluster_iter(start_cluster).collect(); 729004e86ffSlogin } 730004e86ffSlogin 731004e86ffSlogin /// @brief 获取在FAT表中,以start_cluster开头的FAT链的长度(总计经过多少个簇) 732004e86ffSlogin /// 733004e86ffSlogin /// @param start_cluster 整个FAT链的起始簇号 734004e86ffSlogin pub fn num_clusters_chain(&self, start_cluster: Cluster) -> u64 { 735004e86ffSlogin return self 736004e86ffSlogin .cluster_iter(start_cluster) 737004e86ffSlogin .fold(0, |size, _cluster| size + 1); 738004e86ffSlogin } 739004e86ffSlogin /// @brief 获取一个簇迭代器对象 740004e86ffSlogin /// 741004e86ffSlogin /// @param start_cluster 整个FAT链的起始簇号 742004e86ffSlogin fn cluster_iter(&self, start_cluster: Cluster) -> ClusterIter { 743004e86ffSlogin return ClusterIter { 744004e86ffSlogin current_cluster: Some(start_cluster), 745004e86ffSlogin fs: self, 746004e86ffSlogin }; 747004e86ffSlogin } 748004e86ffSlogin 749004e86ffSlogin /// @brief 获取从start_cluster开始的簇链中,第n个簇的信息。(请注意,下标从0开始) 750004e86ffSlogin #[inline] 751004e86ffSlogin pub fn get_cluster_by_relative(&self, start_cluster: Cluster, n: usize) -> Option<Cluster> { 752004e86ffSlogin return self.cluster_iter(start_cluster).skip(n).next(); 753004e86ffSlogin } 754004e86ffSlogin 755004e86ffSlogin /// @brief 获取整个簇链的最后一个簇 756004e86ffSlogin #[inline] 757004e86ffSlogin pub fn get_last_cluster(&self, start_cluster: Cluster) -> Option<Cluster> { 758004e86ffSlogin return self.cluster_iter(start_cluster).last(); 759004e86ffSlogin } 760004e86ffSlogin 761004e86ffSlogin /// @brief 判断FAT文件系统的shut bit是否正常。 762004e86ffSlogin /// shut bit 表示文件系统是否正常卸载。如果这一位是1,则表示这个卷是“干净的” 763004e86ffSlogin /// 参考资料:https://thestarman.pcministry.com/DOS/DirtyShutdownFlag.html 764004e86ffSlogin /// 765004e86ffSlogin /// @return Ok(true) 正常 766004e86ffSlogin /// @return Ok(false) 不正常 767004e86ffSlogin /// @return Err(i32) 在判断时发生错误 768004e86ffSlogin pub fn is_shut_bit_ok(&mut self) -> Result<bool, i32> { 769004e86ffSlogin match self.bpb.fat_type { 770004e86ffSlogin FATType::FAT32(_) => { 771004e86ffSlogin // 对于FAT32, error bit位于第一个扇区的第8字节。 772004e86ffSlogin let bit = self.get_fat_entry_raw(Cluster::new(1))? & 0x0800_0000; 773004e86ffSlogin return Ok(bit > 0); 774004e86ffSlogin } 775004e86ffSlogin FATType::FAT16(_) => { 776004e86ffSlogin let bit = self.get_fat_entry_raw(Cluster::new(1))? & 0x8000; 777004e86ffSlogin return Ok(bit > 0); 778004e86ffSlogin } 779004e86ffSlogin _ => return Ok(true), 780004e86ffSlogin } 781004e86ffSlogin } 782004e86ffSlogin 783004e86ffSlogin /// @brief 判断FAT文件系统的hard error bit是否正常。 784004e86ffSlogin /// 如果此位为0,则文件系统驱动程序在上次安装卷时遇到磁盘 I/O 错误,这表明 785004e86ffSlogin /// 卷上的某些扇区可能已损坏。 786004e86ffSlogin /// 参考资料:https://thestarman.pcministry.com/DOS/DirtyShutdownFlag.html 787004e86ffSlogin /// 788004e86ffSlogin /// @return Ok(true) 正常 789004e86ffSlogin /// @return Ok(false) 不正常 790004e86ffSlogin /// @return Err(i32) 在判断时发生错误 791004e86ffSlogin pub fn is_hard_error_bit_ok(&mut self) -> Result<bool, i32> { 792004e86ffSlogin match self.bpb.fat_type { 793004e86ffSlogin FATType::FAT32(_) => { 794004e86ffSlogin let bit = self.get_fat_entry_raw(Cluster::new(1))? & 0x0400_0000; 795004e86ffSlogin return Ok(bit > 0); 796004e86ffSlogin } 797004e86ffSlogin FATType::FAT16(_) => { 798004e86ffSlogin let bit = self.get_fat_entry_raw(Cluster::new(1))? & 0x4000; 799004e86ffSlogin return Ok(bit > 0); 800004e86ffSlogin } 801004e86ffSlogin _ => return Ok(true), 802004e86ffSlogin } 803004e86ffSlogin } 804004e86ffSlogin 805004e86ffSlogin /// @brief 设置文件系统的shut bit为正常状态 806004e86ffSlogin /// 参考资料:https://thestarman.pcministry.com/DOS/DirtyShutdownFlag.html 807004e86ffSlogin /// 808004e86ffSlogin /// @return Ok(()) 设置成功 809004e86ffSlogin /// @return Err(i32) 在设置过程中,出现错误 810004e86ffSlogin pub fn set_shut_bit_ok(&mut self) -> Result<(), i32> { 811004e86ffSlogin match self.bpb.fat_type { 812004e86ffSlogin FATType::FAT32(_) => { 813004e86ffSlogin let raw_entry = self.get_fat_entry_raw(Cluster::new(1))? | 0x0800_0000; 814004e86ffSlogin self.set_entry(Cluster::new(1), FATEntry::Next(Cluster::new(raw_entry)))?; 815004e86ffSlogin 816004e86ffSlogin return Ok(()); 817004e86ffSlogin } 818004e86ffSlogin 819004e86ffSlogin FATType::FAT16(_) => { 820004e86ffSlogin let raw_entry = self.get_fat_entry_raw(Cluster::new(1))? | 0x8000; 821004e86ffSlogin self.set_entry(Cluster::new(1), FATEntry::Next(Cluster::new(raw_entry)))?; 822004e86ffSlogin return Ok(()); 823004e86ffSlogin } 824004e86ffSlogin _ => return Ok(()), 825004e86ffSlogin } 826004e86ffSlogin } 827004e86ffSlogin 828004e86ffSlogin /// @brief 设置文件系统的hard error bit为正常状态 829004e86ffSlogin /// 参考资料:https://thestarman.pcministry.com/DOS/DirtyShutdownFlag.html 830004e86ffSlogin /// 831004e86ffSlogin /// @return Ok(()) 设置成功 832004e86ffSlogin /// @return Err(i32) 在设置过程中,出现错误 833004e86ffSlogin pub fn set_hard_error_bit_ok(&mut self) -> Result<(), i32> { 834004e86ffSlogin match self.bpb.fat_type { 835004e86ffSlogin FATType::FAT32(_) => { 836004e86ffSlogin let raw_entry = self.get_fat_entry_raw(Cluster::new(1))? | 0x0400_0000; 837004e86ffSlogin self.set_entry(Cluster::new(1), FATEntry::Next(Cluster::new(raw_entry)))?; 838004e86ffSlogin return Ok(()); 839004e86ffSlogin } 840004e86ffSlogin 841004e86ffSlogin FATType::FAT16(_) => { 842004e86ffSlogin let raw_entry = self.get_fat_entry_raw(Cluster::new(1))? | 0x4000; 843004e86ffSlogin self.set_entry(Cluster::new(1), FATEntry::Next(Cluster::new(raw_entry)))?; 844004e86ffSlogin return Ok(()); 845004e86ffSlogin } 846004e86ffSlogin _ => return Ok(()), 847004e86ffSlogin } 848004e86ffSlogin } 849004e86ffSlogin 850004e86ffSlogin /// @brief 执行文件系统卸载前的一些准备工作:设置好对应的标志位,并把缓存中的数据刷入磁盘 851004e86ffSlogin pub fn umount(&mut self) -> Result<(), i32> { 852004e86ffSlogin self.fs_info.0.lock().flush(&self.partition)?; 853004e86ffSlogin 854004e86ffSlogin self.set_shut_bit_ok()?; 855004e86ffSlogin 856004e86ffSlogin self.set_hard_error_bit_ok()?; 857004e86ffSlogin 858004e86ffSlogin self.partition.disk().sync()?; 859004e86ffSlogin 860004e86ffSlogin return Ok(()); 861004e86ffSlogin } 862004e86ffSlogin 863004e86ffSlogin /// @brief 获取文件系统的最大簇号 864004e86ffSlogin pub fn max_cluster_number(&self) -> Cluster { 865004e86ffSlogin match self.bpb.fat_type { 866004e86ffSlogin FATType::FAT32(s) => { 867004e86ffSlogin // FAT32 868004e86ffSlogin 869004e86ffSlogin // 数据扇区数量(总扇区数-保留扇区-FAT占用的扇区) 870004e86ffSlogin let data_sec: u64 = self.bpb.total_sectors_32 as u64 871004e86ffSlogin - (self.bpb.rsvd_sec_cnt as u64 872004e86ffSlogin + self.bpb.num_fats as u64 * s.fat_size_32 as u64); 873004e86ffSlogin 874004e86ffSlogin // 数据区的簇数量 875004e86ffSlogin let total_clusters: u64 = data_sec / self.bpb.sector_per_cluster as u64; 876004e86ffSlogin 877004e86ffSlogin // 返回最大的簇号 878004e86ffSlogin return Cluster::new(total_clusters + RESERVED_CLUSTERS as u64 - 1); 879004e86ffSlogin } 880004e86ffSlogin 881004e86ffSlogin _ => { 882004e86ffSlogin // FAT12 / FAT16 883004e86ffSlogin let root_dir_sectors: u64 = (((self.bpb.root_entries_cnt as u64) * 32) 884004e86ffSlogin + self.bpb.bytes_per_sector as u64 885004e86ffSlogin - 1) 886004e86ffSlogin / self.bpb.bytes_per_sector as u64; 887004e86ffSlogin // 数据区扇区数 888004e86ffSlogin let data_sec: u64 = self.bpb.total_sectors_16 as u64 889004e86ffSlogin - (self.bpb.rsvd_sec_cnt as u64 890004e86ffSlogin + (self.bpb.num_fats as u64 * self.bpb.fat_size_16 as u64) 891004e86ffSlogin + root_dir_sectors); 892004e86ffSlogin let total_clusters = data_sec / self.bpb.sector_per_cluster as u64; 893004e86ffSlogin return Cluster::new(total_clusters + RESERVED_CLUSTERS as u64 - 1); 894004e86ffSlogin } 895004e86ffSlogin } 896004e86ffSlogin } 897004e86ffSlogin 898004e86ffSlogin /// @brief 在文件系统中寻找一个簇号在给定的范围(左闭右开区间)内的空闲簇 899004e86ffSlogin /// 900004e86ffSlogin /// @param start_cluster 起始簇号 901004e86ffSlogin /// @param end_cluster 终止簇号(不包含) 902004e86ffSlogin /// 903004e86ffSlogin /// @return Ok(Cluster) 寻找到的空闲簇 904004e86ffSlogin /// @return Err(i32) 错误码。如果磁盘无剩余空间,或者簇号达到给定的最大值,则返回-ENOSPC. 905004e86ffSlogin pub fn get_free_cluster( 906004e86ffSlogin &self, 907004e86ffSlogin start_cluster: Cluster, 908004e86ffSlogin end_cluster: Cluster, 909004e86ffSlogin ) -> Result<Cluster, i32> { 910004e86ffSlogin let max_cluster: Cluster = self.max_cluster_number(); 911004e86ffSlogin let mut cluster: u64 = start_cluster.cluster_num; 912004e86ffSlogin 913004e86ffSlogin let fat_type: FATType = self.bpb.fat_type; 914004e86ffSlogin let fat_start_sector: u64 = self.fat_start_sector(); 915004e86ffSlogin let bytes_per_sec: u64 = self.bpb.bytes_per_sector as u64; 916004e86ffSlogin 917004e86ffSlogin match fat_type { 918004e86ffSlogin FATType::FAT12(_) => { 919004e86ffSlogin let part_bytes_offset: u64 = 920004e86ffSlogin fat_type.get_fat_bytes_offset(start_cluster, fat_start_sector, bytes_per_sec); 921004e86ffSlogin let in_block_offset = self.get_in_block_offset(part_bytes_offset); 922004e86ffSlogin 923004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset)); 924004e86ffSlogin 925004e86ffSlogin // 由于FAT12的FAT表不大于6K,因此直接读取6K 926004e86ffSlogin let num_lba = (6 * 1024) / LBA_SIZE; 927004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 928004e86ffSlogin v.resize(num_lba * LBA_SIZE, 0); 929004e86ffSlogin self.partition.disk().read_at(lba, num_lba, &mut v)?; 930004e86ffSlogin 931004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 932004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 933004e86ffSlogin 934004e86ffSlogin let mut packed_val: u16 = cursor.read_u16()?; 935004e86ffSlogin loop { 936004e86ffSlogin let val = if (cluster & 0x1) > 0 { 937004e86ffSlogin packed_val >> 4 938004e86ffSlogin } else { 939004e86ffSlogin packed_val & 0x0fff 940004e86ffSlogin }; 941004e86ffSlogin if val == 0 { 942004e86ffSlogin return Ok(Cluster::new(cluster as u64)); 943004e86ffSlogin } 944004e86ffSlogin 945004e86ffSlogin cluster += 1; 946004e86ffSlogin 947004e86ffSlogin // 磁盘无剩余空间,或者簇号达到给定的最大值 948004e86ffSlogin if cluster == end_cluster.cluster_num || cluster == max_cluster.cluster_num { 949004e86ffSlogin return Err(-(ENOSPC as i32)); 950004e86ffSlogin } 951004e86ffSlogin 952004e86ffSlogin packed_val = match cluster & 1 { 953004e86ffSlogin 0 => cursor.read_u16()?, 954004e86ffSlogin _ => { 955004e86ffSlogin let next_byte = cursor.read_u8()? as u16; 956004e86ffSlogin (packed_val >> 8) | (next_byte << 8) 957004e86ffSlogin } 958004e86ffSlogin }; 959004e86ffSlogin } 960004e86ffSlogin } 961004e86ffSlogin FATType::FAT16(_) => { 962004e86ffSlogin // todo: 优化这里,减少读取磁盘的次数。 963004e86ffSlogin while cluster < end_cluster.cluster_num && cluster < max_cluster.cluster_num { 964004e86ffSlogin let part_bytes_offset: u64 = fat_type.get_fat_bytes_offset( 965004e86ffSlogin Cluster::new(cluster), 966004e86ffSlogin fat_start_sector, 967004e86ffSlogin bytes_per_sec, 968004e86ffSlogin ); 969004e86ffSlogin let in_block_offset = self.get_in_block_offset(part_bytes_offset); 970004e86ffSlogin 971004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset)); 972004e86ffSlogin 973004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 974004e86ffSlogin v.resize(self.lba_per_sector() * LBA_SIZE, 0); 975004e86ffSlogin self.partition 976004e86ffSlogin .disk() 977004e86ffSlogin .read_at(lba, self.lba_per_sector(), &mut v)?; 978004e86ffSlogin 979004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 980004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 981004e86ffSlogin 982004e86ffSlogin let val = cursor.read_u16()?; 983004e86ffSlogin // 找到空闲簇 984004e86ffSlogin if val == 0 { 985004e86ffSlogin return Ok(Cluster::new(val as u64)); 986004e86ffSlogin } 987004e86ffSlogin cluster += 1; 988004e86ffSlogin } 989004e86ffSlogin 990004e86ffSlogin // 磁盘无剩余空间,或者簇号达到给定的最大值 991004e86ffSlogin return Err(-(ENOSPC as i32)); 992004e86ffSlogin } 993004e86ffSlogin FATType::FAT32(_) => { 994004e86ffSlogin // todo: 优化这里,减少读取磁盘的次数。 995004e86ffSlogin while cluster < end_cluster.cluster_num && cluster < max_cluster.cluster_num { 996004e86ffSlogin let part_bytes_offset: u64 = fat_type.get_fat_bytes_offset( 997004e86ffSlogin Cluster::new(cluster), 998004e86ffSlogin fat_start_sector, 999004e86ffSlogin bytes_per_sec, 1000004e86ffSlogin ); 1001004e86ffSlogin let in_block_offset = self.get_in_block_offset(part_bytes_offset); 1002004e86ffSlogin 1003004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset)); 1004004e86ffSlogin 1005004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1006004e86ffSlogin v.resize(self.lba_per_sector() * LBA_SIZE, 0); 1007004e86ffSlogin self.partition 1008004e86ffSlogin .disk() 1009004e86ffSlogin .read_at(lba, self.lba_per_sector(), &mut v)?; 1010004e86ffSlogin 1011004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1012004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1013004e86ffSlogin 1014004e86ffSlogin let val = cursor.read_u32()? & 0x0fffffff; 1015004e86ffSlogin 1016004e86ffSlogin if val == 0 { 1017004e86ffSlogin return Ok(Cluster::new(cluster)); 1018004e86ffSlogin } 1019004e86ffSlogin cluster += 1; 1020004e86ffSlogin } 1021004e86ffSlogin 1022004e86ffSlogin // 磁盘无剩余空间,或者簇号达到给定的最大值 1023004e86ffSlogin return Err(-(ENOSPC as i32)); 1024004e86ffSlogin } 1025004e86ffSlogin } 1026004e86ffSlogin } 1027004e86ffSlogin 1028004e86ffSlogin /// @brief 在FAT表中,设置指定的簇的信息。 1029004e86ffSlogin /// 1030004e86ffSlogin /// @param cluster 目标簇 1031004e86ffSlogin /// @param fat_entry 这个簇在FAT表中,存储的信息(下一个簇的簇号) 1032004e86ffSlogin pub fn set_entry(&self, cluster: Cluster, fat_entry: FATEntry) -> Result<(), i32> { 1033004e86ffSlogin // fat表项在分区上的字节偏移量 1034004e86ffSlogin let fat_part_bytes_offset: u64 = self.bpb.fat_type.get_fat_bytes_offset( 1035004e86ffSlogin cluster, 1036004e86ffSlogin self.fat_start_sector(), 1037004e86ffSlogin self.bpb.bytes_per_sector as u64, 1038004e86ffSlogin ); 1039004e86ffSlogin 1040004e86ffSlogin match self.bpb.fat_type { 1041004e86ffSlogin FATType::FAT12(_) => { 1042004e86ffSlogin // 计算要写入的值 1043004e86ffSlogin let raw_val: u16 = match fat_entry { 1044004e86ffSlogin FATEntry::Unused => 0, 1045004e86ffSlogin FATEntry::Bad => 0xff7, 1046004e86ffSlogin FATEntry::EndOfChain => 0xfff, 1047004e86ffSlogin FATEntry::Next(c) => c.cluster_num as u16, 1048004e86ffSlogin }; 1049004e86ffSlogin 1050004e86ffSlogin let in_block_offset = self.get_in_block_offset(fat_part_bytes_offset); 1051004e86ffSlogin 1052004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset)); 1053004e86ffSlogin 1054004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1055004e86ffSlogin v.resize(LBA_SIZE, 0); 1056004e86ffSlogin self.partition.disk().read_at(lba, 1, &mut v)?; 1057004e86ffSlogin 1058004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1059004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1060004e86ffSlogin 1061004e86ffSlogin let old_val: u16 = cursor.read_u16()?; 1062004e86ffSlogin let new_val: u16 = if (cluster.cluster_num & 0x1) > 0 { 1063004e86ffSlogin (old_val & 0x000f) | (raw_val << 4) 1064004e86ffSlogin } else { 1065004e86ffSlogin (old_val & 0xf000) | raw_val 1066004e86ffSlogin }; 1067004e86ffSlogin 1068004e86ffSlogin // 写回数据到磁盘上 1069004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1070004e86ffSlogin cursor.write_u16(new_val)?; 1071004e86ffSlogin self.partition.disk().write_at(lba, 1, cursor.as_slice())?; 1072004e86ffSlogin return Ok(()); 1073004e86ffSlogin } 1074004e86ffSlogin FATType::FAT16(_) => { 1075004e86ffSlogin // 计算要写入的值 1076004e86ffSlogin let raw_val: u16 = match fat_entry { 1077004e86ffSlogin FATEntry::Unused => 0, 1078004e86ffSlogin FATEntry::Bad => 0xfff7, 1079004e86ffSlogin FATEntry::EndOfChain => 0xfdff, 1080004e86ffSlogin FATEntry::Next(c) => c.cluster_num as u16, 1081004e86ffSlogin }; 1082004e86ffSlogin 1083004e86ffSlogin let in_block_offset = self.get_in_block_offset(fat_part_bytes_offset); 1084004e86ffSlogin 1085004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset)); 1086004e86ffSlogin 1087004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1088004e86ffSlogin v.resize(LBA_SIZE, 0); 1089004e86ffSlogin self.partition.disk().read_at(lba, 1, &mut v)?; 1090004e86ffSlogin 1091004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1092004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1093004e86ffSlogin 1094004e86ffSlogin cursor.write_u16(raw_val)?; 1095004e86ffSlogin self.partition.disk().write_at(lba, 1, cursor.as_slice())?; 1096004e86ffSlogin 1097004e86ffSlogin return Ok(()); 1098004e86ffSlogin } 1099004e86ffSlogin FATType::FAT32(_) => { 1100004e86ffSlogin let fat_size: u64 = self.fat_size(); 1101004e86ffSlogin let bound: u64 = if self.mirroring_enabled() { 1102004e86ffSlogin 1 1103004e86ffSlogin } else { 1104004e86ffSlogin self.bpb.num_fats as u64 1105004e86ffSlogin }; 1106004e86ffSlogin // kdebug!("set entry, bound={bound}, fat_size={fat_size}"); 1107004e86ffSlogin for i in 0..bound { 1108004e86ffSlogin // 当前操作的FAT表在磁盘上的字节偏移量 1109004e86ffSlogin let f_offset: u64 = fat_part_bytes_offset + i * fat_size; 1110004e86ffSlogin let in_block_offset: u64 = self.get_in_block_offset(f_offset); 1111004e86ffSlogin let lba = self.get_lba_from_offset(self.bytes_to_sector(f_offset)); 1112004e86ffSlogin 1113004e86ffSlogin // kdebug!("set entry, lba={lba}, in_block_offset={in_block_offset}"); 1114004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1115004e86ffSlogin v.resize(LBA_SIZE, 0); 1116004e86ffSlogin self.partition.disk().read_at(lba, 1, &mut v)?; 1117004e86ffSlogin 1118004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1119004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1120004e86ffSlogin 1121004e86ffSlogin // FAT32的高4位保留 1122004e86ffSlogin let old_bits = cursor.read_u32()? & 0xf0000000; 1123004e86ffSlogin 1124004e86ffSlogin if fat_entry == FATEntry::Unused 1125004e86ffSlogin && cluster.cluster_num >= 0x0ffffff7 1126004e86ffSlogin && cluster.cluster_num <= 0x0fffffff 1127004e86ffSlogin { 1128004e86ffSlogin kerror!( 1129004e86ffSlogin "FAT32: Reserved Cluster {:?} cannot be marked as free", 1130004e86ffSlogin cluster 1131004e86ffSlogin ); 1132004e86ffSlogin return Err(-(EPERM as i32)); 1133004e86ffSlogin } 1134004e86ffSlogin 1135004e86ffSlogin // 计算要写入的值 1136004e86ffSlogin let mut raw_val: u32 = match fat_entry { 1137004e86ffSlogin FATEntry::Unused => 0, 1138004e86ffSlogin FATEntry::Bad => 0x0FFFFFF7, 1139004e86ffSlogin FATEntry::EndOfChain => 0x0FFFFFFF, 1140004e86ffSlogin FATEntry::Next(c) => c.cluster_num as u32, 1141004e86ffSlogin }; 1142004e86ffSlogin 1143004e86ffSlogin // 恢复保留位 1144004e86ffSlogin raw_val |= old_bits; 1145004e86ffSlogin 1146004e86ffSlogin // kdebug!("sent entry, raw_val={raw_val}"); 1147004e86ffSlogin 1148004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1149004e86ffSlogin cursor.write_u32(raw_val)?; 1150004e86ffSlogin 1151004e86ffSlogin self.partition.disk().write_at(lba, 1, cursor.as_slice())?; 1152004e86ffSlogin } 1153004e86ffSlogin 1154004e86ffSlogin return Ok(()); 1155004e86ffSlogin } 1156004e86ffSlogin } 1157004e86ffSlogin } 1158004e86ffSlogin 1159004e86ffSlogin /// @brief 清空指定的簇 1160004e86ffSlogin /// 1161004e86ffSlogin /// @param cluster 要被清空的簇 1162004e86ffSlogin pub fn zero_cluster(&self, cluster: Cluster) -> Result<(), i32> { 1163004e86ffSlogin // 准备数据,用于写入 1164004e86ffSlogin let zeros: Vec<u8> = vec![0u8; self.bytes_per_cluster() as usize]; 1165004e86ffSlogin let offset: usize = self.cluster_bytes_offset(cluster) as usize; 1166004e86ffSlogin self.partition 1167004e86ffSlogin .disk() 1168004e86ffSlogin .device() 1169004e86ffSlogin .write_at(offset, zeros.len(), zeros.as_slice())?; 1170004e86ffSlogin return Ok(()); 1171004e86ffSlogin } 1172004e86ffSlogin } 1173004e86ffSlogin 1174004e86ffSlogin impl Drop for FATFileSystem { 1175004e86ffSlogin fn drop(&mut self) { 1176004e86ffSlogin let r = self.umount(); 1177004e86ffSlogin if r.is_err() { 1178004e86ffSlogin kerror!( 1179004e86ffSlogin "Umount FAT filesystem failed: errno={}, FS detail:{self:?}", 1180004e86ffSlogin r.unwrap_err() 1181004e86ffSlogin ); 1182004e86ffSlogin } 1183004e86ffSlogin } 1184004e86ffSlogin } 1185004e86ffSlogin 1186004e86ffSlogin impl FATFsInfo { 1187004e86ffSlogin const LEAD_SIG: u32 = 0x41615252; 1188004e86ffSlogin const STRUC_SIG: u32 = 0x61417272; 1189004e86ffSlogin const TRAIL_SIG: u32 = 0xAA550000; 1190004e86ffSlogin const FS_INFO_SIZE: u64 = 512; 1191004e86ffSlogin 1192004e86ffSlogin /// @brief 从磁盘上读取FAT文件系统的FSInfo结构体 1193004e86ffSlogin /// 1194004e86ffSlogin /// @param partition 磁盘分区 1195004e86ffSlogin /// @param in_disk_fs_info_offset FSInfo扇区在磁盘内的字节偏移量(单位:字节) 1196004e86ffSlogin /// @param bytes_per_sec 每扇区字节数 1197004e86ffSlogin pub fn new( 1198004e86ffSlogin partition: Arc<Partition>, 1199004e86ffSlogin in_disk_fs_info_offset: u64, 1200004e86ffSlogin bytes_per_sec: usize, 1201004e86ffSlogin ) -> Result<Self, i32> { 1202004e86ffSlogin let mut v = Vec::<u8>::new(); 1203004e86ffSlogin v.resize(bytes_per_sec, 0); 1204004e86ffSlogin 1205004e86ffSlogin // 计算fs_info扇区在磁盘上的字节偏移量,从磁盘读取数据 1206004e86ffSlogin partition 1207004e86ffSlogin .disk() 1208004e86ffSlogin .read_at(in_disk_fs_info_offset as usize / LBA_SIZE, 1, &mut v)?; 1209004e86ffSlogin let mut cursor = VecCursor::new(v); 1210004e86ffSlogin 1211004e86ffSlogin let mut fsinfo = FATFsInfo::default(); 1212004e86ffSlogin 1213004e86ffSlogin fsinfo.lead_sig = cursor.read_u32()?; 1214004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(480))?; 1215004e86ffSlogin fsinfo.struc_sig = cursor.read_u32()?; 1216004e86ffSlogin fsinfo.free_count = cursor.read_u32()?; 1217004e86ffSlogin fsinfo.next_free = cursor.read_u32()?; 1218004e86ffSlogin 1219004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(12))?; 1220004e86ffSlogin 1221004e86ffSlogin fsinfo.trail_sig = cursor.read_u32()?; 1222004e86ffSlogin fsinfo.dirty = false; 1223004e86ffSlogin fsinfo.offset = Some(in_disk_fs_info_offset); 1224004e86ffSlogin 1225004e86ffSlogin if fsinfo.is_valid() { 1226004e86ffSlogin return Ok(fsinfo); 1227004e86ffSlogin } else { 1228004e86ffSlogin kerror!("Error occurred while parsing FATFsInfo."); 1229004e86ffSlogin return Err(-(EINVAL as i32)); 1230004e86ffSlogin } 1231004e86ffSlogin } 1232004e86ffSlogin 1233004e86ffSlogin /// @brief 判断是否为正确的FsInfo结构体 1234004e86ffSlogin fn is_valid(&self) -> bool { 1235004e86ffSlogin self.lead_sig == Self::LEAD_SIG 1236004e86ffSlogin && self.struc_sig == Self::STRUC_SIG 1237004e86ffSlogin && self.trail_sig == Self::TRAIL_SIG 1238004e86ffSlogin } 1239004e86ffSlogin 1240004e86ffSlogin /// @brief 根据fsinfo的信息,计算当前总的空闲簇数量 1241004e86ffSlogin /// 1242004e86ffSlogin /// @param 当前文件系统的最大簇号 1243004e86ffSlogin pub fn count_free_cluster(&self, max_cluster: Cluster) -> Option<u64> { 1244004e86ffSlogin let count_clusters = max_cluster.cluster_num - RESERVED_CLUSTERS as u64 + 1; 1245004e86ffSlogin // 信息不合理,当前的FsInfo中存储的free count大于计算出来的值 1246004e86ffSlogin if self.free_count as u64 > count_clusters { 1247004e86ffSlogin return None; 1248004e86ffSlogin } else { 1249004e86ffSlogin match self.free_count { 1250004e86ffSlogin // free count字段不可用 1251004e86ffSlogin 0xffffffff => return None, 1252004e86ffSlogin // 返回FsInfo中存储的数据 1253004e86ffSlogin n => return Some(n as u64), 1254004e86ffSlogin } 1255004e86ffSlogin } 1256004e86ffSlogin } 1257004e86ffSlogin 1258004e86ffSlogin /// @brief 更新FsInfo中的“空闲簇统计信息“为new_count 1259004e86ffSlogin /// 1260004e86ffSlogin /// 请注意,除非手动调用`flush()`,否则本函数不会将数据刷入磁盘 1261004e86ffSlogin pub fn update_free_count_abs(&mut self, new_count: u32) { 1262004e86ffSlogin self.free_count = new_count; 1263004e86ffSlogin } 1264004e86ffSlogin 1265004e86ffSlogin /// @brief 更新FsInfo中的“空闲簇统计信息“,把它加上delta. 1266004e86ffSlogin /// 1267004e86ffSlogin /// 请注意,除非手动调用`flush()`,否则本函数不会将数据刷入磁盘 1268004e86ffSlogin pub fn update_free_count_delta(&mut self, delta: i32) { 1269004e86ffSlogin self.free_count = (self.free_count as i32 + delta) as u32; 1270004e86ffSlogin } 1271004e86ffSlogin 1272004e86ffSlogin /// @brief 更新FsInfo中的“第一个空闲簇统计信息“为next_free. 1273004e86ffSlogin /// 1274004e86ffSlogin /// 请注意,除非手动调用`flush()`,否则本函数不会将数据刷入磁盘 1275004e86ffSlogin pub fn update_next_free(&mut self, next_free: u32) { 1276004e86ffSlogin // 这个值是参考量,不一定要准确,仅供加速查找 1277004e86ffSlogin self.next_free = next_free; 1278004e86ffSlogin } 1279004e86ffSlogin 1280004e86ffSlogin /// @brief 获取fs info 记载的第一个空闲簇。(不一定准确,仅供参考) 1281004e86ffSlogin pub fn next_free(&self) -> Option<u64> { 1282004e86ffSlogin match self.next_free { 1283004e86ffSlogin 0xffffffff => return None, 1284004e86ffSlogin 0 | 1 => return None, 1285004e86ffSlogin n => return Some(n as u64), 1286004e86ffSlogin }; 1287004e86ffSlogin } 1288004e86ffSlogin 1289004e86ffSlogin /// @brief 把fs info刷入磁盘 1290004e86ffSlogin /// 1291004e86ffSlogin /// @param partition fs info所在的分区 1292004e86ffSlogin pub fn flush(&self, partition: &Arc<Partition>) -> Result<(), i32> { 1293004e86ffSlogin if let Some(off) = self.offset { 1294004e86ffSlogin let in_block_offset = off % LBA_SIZE as u64; 1295004e86ffSlogin 1296004e86ffSlogin let lba = off as usize / LBA_SIZE; 1297004e86ffSlogin 1298004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1299004e86ffSlogin v.resize(LBA_SIZE, 0); 1300004e86ffSlogin partition.disk().read_at(lba, 1, &mut v)?; 1301004e86ffSlogin 1302004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1303004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1304004e86ffSlogin 1305004e86ffSlogin cursor.write_u32(self.lead_sig)?; 1306004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(480))?; 1307004e86ffSlogin cursor.write_u32(self.struc_sig)?; 1308004e86ffSlogin cursor.write_u32(self.free_count)?; 1309004e86ffSlogin cursor.write_u32(self.next_free)?; 1310004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(12))?; 1311004e86ffSlogin cursor.write_u32(self.trail_sig)?; 1312004e86ffSlogin 1313004e86ffSlogin partition.disk().write_at(lba, 1, cursor.as_slice())?; 1314004e86ffSlogin } 1315004e86ffSlogin return Ok(()); 1316004e86ffSlogin } 1317004e86ffSlogin 1318004e86ffSlogin /// @brief 读取磁盘上的Fs Info扇区,将里面的内容更新到结构体中 1319004e86ffSlogin /// 1320004e86ffSlogin /// @param partition fs info所在的分区 1321004e86ffSlogin pub fn update(&mut self, partition: Arc<Partition>) -> Result<(), i32> { 1322004e86ffSlogin if let Some(off) = self.offset { 1323004e86ffSlogin let in_block_offset = off % LBA_SIZE as u64; 1324004e86ffSlogin 1325004e86ffSlogin let lba = off as usize / LBA_SIZE; 1326004e86ffSlogin 1327004e86ffSlogin let mut v: Vec<u8> = Vec::new(); 1328004e86ffSlogin v.resize(LBA_SIZE, 0); 1329004e86ffSlogin partition.disk().read_at(lba, 1, &mut v)?; 1330004e86ffSlogin let mut cursor: VecCursor = VecCursor::new(v); 1331004e86ffSlogin cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?; 1332004e86ffSlogin self.lead_sig = cursor.read_u32()?; 1333004e86ffSlogin 1334004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(480))?; 1335004e86ffSlogin self.struc_sig = cursor.read_u32()?; 1336004e86ffSlogin self.free_count = cursor.read_u32()?; 1337004e86ffSlogin self.next_free = cursor.read_u32()?; 1338004e86ffSlogin cursor.seek(SeekFrom::SeekCurrent(12))?; 1339004e86ffSlogin self.trail_sig = cursor.read_u32()?; 1340004e86ffSlogin } 1341004e86ffSlogin return Ok(()); 1342004e86ffSlogin } 1343004e86ffSlogin } 1344004e86ffSlogin 1345004e86ffSlogin impl IndexNode for LockedFATInode { 1346004e86ffSlogin fn read_at( 1347004e86ffSlogin &self, 1348004e86ffSlogin offset: usize, 1349004e86ffSlogin len: usize, 1350004e86ffSlogin buf: &mut [u8], 1351004e86ffSlogin _data: &mut FilePrivateData, 1352004e86ffSlogin ) -> Result<usize, i32> { 1353004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1354004e86ffSlogin match &guard.inode_type { 1355004e86ffSlogin FATDirEntry::File(f) | FATDirEntry::VolId(f) => { 1356004e86ffSlogin let r = f.read( 1357004e86ffSlogin &guard.fs.upgrade().unwrap(), 1358004e86ffSlogin &mut buf[0..len], 1359004e86ffSlogin offset as u64, 1360004e86ffSlogin ); 1361004e86ffSlogin guard.update_metadata(); 1362004e86ffSlogin return r; 1363004e86ffSlogin } 1364004e86ffSlogin FATDirEntry::Dir(_) => { 1365004e86ffSlogin return Err(-(EISDIR as i32)); 1366004e86ffSlogin } 1367004e86ffSlogin FATDirEntry::UnInit => { 1368004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1369004e86ffSlogin return Err(-(EROFS as i32)); 1370004e86ffSlogin } 1371004e86ffSlogin } 1372004e86ffSlogin } 1373004e86ffSlogin 1374004e86ffSlogin fn write_at( 1375004e86ffSlogin &self, 1376004e86ffSlogin offset: usize, 1377004e86ffSlogin len: usize, 1378004e86ffSlogin buf: &[u8], 1379004e86ffSlogin _data: &mut FilePrivateData, 1380004e86ffSlogin ) -> Result<usize, i32> { 1381004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1382004e86ffSlogin let fs: &Arc<FATFileSystem> = &guard.fs.upgrade().unwrap(); 1383004e86ffSlogin 1384004e86ffSlogin match &mut guard.inode_type { 1385004e86ffSlogin FATDirEntry::File(f) | FATDirEntry::VolId(f) => { 1386004e86ffSlogin let r = f.write(fs, &buf[0..len], offset as u64); 1387004e86ffSlogin guard.update_metadata(); 1388004e86ffSlogin return r; 1389004e86ffSlogin } 1390004e86ffSlogin FATDirEntry::Dir(_) => { 1391004e86ffSlogin return Err(-(EISDIR as i32)); 1392004e86ffSlogin } 1393004e86ffSlogin FATDirEntry::UnInit => { 1394004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1395004e86ffSlogin return Err(-(EROFS as i32)); 1396004e86ffSlogin } 1397004e86ffSlogin } 1398004e86ffSlogin } 1399004e86ffSlogin 1400004e86ffSlogin fn poll(&self) -> Result<PollStatus, i32> { 1401004e86ffSlogin // 加锁 1402004e86ffSlogin let inode: SpinLockGuard<FATInode> = self.0.lock(); 1403004e86ffSlogin 1404004e86ffSlogin // 检查当前inode是否为一个文件夹,如果是的话,就返回错误 1405004e86ffSlogin if inode.metadata.file_type == FileType::Dir { 1406004e86ffSlogin return Err(-(EISDIR as i32)); 1407004e86ffSlogin } 1408004e86ffSlogin 1409004e86ffSlogin return Ok(PollStatus { 1410004e86ffSlogin flags: PollStatus::READ_MASK | PollStatus::WRITE_MASK, 1411004e86ffSlogin }); 1412004e86ffSlogin } 1413004e86ffSlogin 1414004e86ffSlogin fn create( 1415004e86ffSlogin &self, 1416004e86ffSlogin name: &str, 1417004e86ffSlogin file_type: FileType, 1418004e86ffSlogin _mode: u32, 1419004e86ffSlogin ) -> Result<Arc<dyn IndexNode>, i32> { 1420004e86ffSlogin // 由于FAT32不支持文件权限的功能,因此忽略mode参数 1421004e86ffSlogin 1422004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1423004e86ffSlogin let fs: &Arc<FATFileSystem> = &guard.fs.upgrade().unwrap(); 1424004e86ffSlogin 1425004e86ffSlogin match &mut guard.inode_type { 1426004e86ffSlogin FATDirEntry::File(_) | FATDirEntry::VolId(_) => { 1427004e86ffSlogin return Err(-(ENOTDIR as i32)); 1428004e86ffSlogin } 1429004e86ffSlogin FATDirEntry::Dir(d) => match file_type { 1430004e86ffSlogin FileType::File => { 1431004e86ffSlogin d.create_file(name, fs)?; 1432004e86ffSlogin return Ok(guard.find(name)?); 1433004e86ffSlogin } 1434004e86ffSlogin FileType::Dir => { 1435004e86ffSlogin d.create_dir(name, fs)?; 1436004e86ffSlogin return Ok(guard.find(name)?); 1437004e86ffSlogin } 1438004e86ffSlogin 1439004e86ffSlogin FileType::SymLink => return Err(-(ENOTSUP as i32)), 1440004e86ffSlogin _ => return Err(-(EINVAL as i32)), 1441004e86ffSlogin }, 1442004e86ffSlogin FATDirEntry::UnInit => { 1443004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1444004e86ffSlogin return Err(-(EROFS as i32)); 1445004e86ffSlogin } 1446004e86ffSlogin } 1447004e86ffSlogin } 1448004e86ffSlogin 1449004e86ffSlogin fn fs(&self) -> Arc<dyn FileSystem> { 1450004e86ffSlogin return self.0.lock().fs.upgrade().unwrap(); 1451004e86ffSlogin } 1452004e86ffSlogin 1453004e86ffSlogin fn as_any_ref(&self) -> &dyn core::any::Any { 1454004e86ffSlogin return self; 1455004e86ffSlogin } 1456004e86ffSlogin 1457004e86ffSlogin fn metadata(&self) -> Result<Metadata, i32> { 1458004e86ffSlogin return Ok(self.0.lock().metadata.clone()); 1459004e86ffSlogin } 1460004e86ffSlogin 1461004e86ffSlogin fn list(&self) -> Result<Vec<String>, i32> { 1462004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1463004e86ffSlogin let fatent: &FATDirEntry = &guard.inode_type; 1464004e86ffSlogin match fatent { 1465004e86ffSlogin FATDirEntry::File(_) | FATDirEntry::VolId(_) => { 1466004e86ffSlogin return Err(-(ENOTDIR as i32)); 1467004e86ffSlogin } 1468004e86ffSlogin FATDirEntry::Dir(dir) => { 1469004e86ffSlogin // 获取当前目录下的所有目录项 1470004e86ffSlogin let mut ret: Vec<String> = Vec::new(); 1471004e86ffSlogin let dir_iter: FATDirIter = dir.to_iter(guard.fs.upgrade().unwrap()); 1472004e86ffSlogin for ent in dir_iter { 1473004e86ffSlogin ret.push(ent.name()); 1474004e86ffSlogin 1475004e86ffSlogin // ====== 生成inode缓存,存入B树 1476004e86ffSlogin let name: String = ent.name(); 1477004e86ffSlogin // kdebug!("name={name}"); 1478004e86ffSlogin 1479004e86ffSlogin if guard.children.contains_key(&name.to_uppercase()) == false 1480004e86ffSlogin && name != "." 1481004e86ffSlogin && name != ".." 1482004e86ffSlogin { 1483004e86ffSlogin // 创建新的inode 1484004e86ffSlogin let entry_inode: Arc<LockedFATInode> = LockedFATInode::new( 1485004e86ffSlogin guard.fs.upgrade().unwrap(), 1486004e86ffSlogin guard.self_ref.clone(), 1487004e86ffSlogin ent, 1488004e86ffSlogin ); 1489004e86ffSlogin // 加入缓存区, 由于FAT文件系统的大小写不敏感问题,因此存入缓存区的key应当是全大写的 1490004e86ffSlogin guard 1491004e86ffSlogin .children 1492004e86ffSlogin .insert(name.to_uppercase(), entry_inode.clone()); 1493004e86ffSlogin } 1494004e86ffSlogin } 1495004e86ffSlogin return Ok(ret); 1496004e86ffSlogin } 1497004e86ffSlogin FATDirEntry::UnInit => { 1498004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1499004e86ffSlogin return Err(-(EROFS as i32)); 1500004e86ffSlogin } 1501004e86ffSlogin } 1502004e86ffSlogin } 1503004e86ffSlogin 1504004e86ffSlogin fn find(&self, name: &str) -> Result<Arc<dyn IndexNode>, i32> { 1505004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1506004e86ffSlogin let target = guard.find(name)?; 1507004e86ffSlogin return Ok(target); 1508004e86ffSlogin } 1509004e86ffSlogin 15100d48c3c9Slogin fn open(&self, _data: &mut FilePrivateData, _mode: &FileMode) -> Result<(), i32> { 1511004e86ffSlogin return Ok(()); 1512004e86ffSlogin } 1513004e86ffSlogin 1514004e86ffSlogin fn close(&self, _data: &mut FilePrivateData) -> Result<(), i32> { 1515004e86ffSlogin return Ok(()); 1516004e86ffSlogin } 1517004e86ffSlogin 1518004e86ffSlogin fn unlink(&self, name: &str) -> Result<(), i32> { 1519004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1520004e86ffSlogin let target: Arc<LockedFATInode> = guard.find(name)?; 1521004e86ffSlogin // 对目标inode上锁,以防更改 1522004e86ffSlogin let target_guard: SpinLockGuard<FATInode> = target.0.lock(); 1523004e86ffSlogin // 先从缓存删除 1524004e86ffSlogin guard.children.remove(&name.to_uppercase()); 1525004e86ffSlogin 1526004e86ffSlogin let dir = match &guard.inode_type { 1527004e86ffSlogin FATDirEntry::File(_) | FATDirEntry::VolId(_) => { 1528004e86ffSlogin return Err(-(ENOTDIR as i32)); 1529004e86ffSlogin } 1530004e86ffSlogin FATDirEntry::Dir(d) => d, 1531004e86ffSlogin FATDirEntry::UnInit => { 1532004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1533004e86ffSlogin return Err(-(EROFS as i32)); 1534004e86ffSlogin } 1535004e86ffSlogin }; 1536004e86ffSlogin // 检查文件是否存在 1537004e86ffSlogin dir.check_existence(name, Some(false), guard.fs.upgrade().unwrap())?; 1538004e86ffSlogin 1539004e86ffSlogin // 再从磁盘删除 1540004e86ffSlogin let r = dir.remove(guard.fs.upgrade().unwrap().clone(), name, true); 1541004e86ffSlogin drop(target_guard); 1542004e86ffSlogin return r; 1543004e86ffSlogin } 1544004e86ffSlogin 1545004e86ffSlogin fn rmdir(&self, name: &str) -> Result<(), i32> { 1546004e86ffSlogin let mut guard: SpinLockGuard<FATInode> = self.0.lock(); 1547004e86ffSlogin let target: Arc<LockedFATInode> = guard.find(name)?; 1548004e86ffSlogin // 对目标inode上锁,以防更改 1549004e86ffSlogin let target_guard: SpinLockGuard<FATInode> = target.0.lock(); 1550004e86ffSlogin // 先从缓存删除 1551004e86ffSlogin guard.children.remove(&name.to_uppercase()); 1552004e86ffSlogin 1553004e86ffSlogin let dir = match &guard.inode_type { 1554004e86ffSlogin FATDirEntry::File(_) | FATDirEntry::VolId(_) => { 1555004e86ffSlogin return Err(-(ENOTDIR as i32)); 1556004e86ffSlogin } 1557004e86ffSlogin FATDirEntry::Dir(d) => d, 1558004e86ffSlogin FATDirEntry::UnInit => { 1559004e86ffSlogin kerror!("FATFS: param: Inode_type uninitialized."); 1560004e86ffSlogin return Err(-(EROFS as i32)); 1561004e86ffSlogin } 1562004e86ffSlogin }; 1563004e86ffSlogin // 检查文件夹是否存在 1564004e86ffSlogin dir.check_existence(name, Some(true), guard.fs.upgrade().unwrap())?; 1565004e86ffSlogin 1566004e86ffSlogin // 再从磁盘删除 1567004e86ffSlogin let r: Result<(), i32> = dir.remove(guard.fs.upgrade().unwrap().clone(), name, true); 1568004e86ffSlogin if r.is_ok() { 1569004e86ffSlogin return r; 1570004e86ffSlogin } else { 1571004e86ffSlogin let r = r.unwrap_err(); 1572004e86ffSlogin if r == -(ENOTEMPTY as i32) { 1573004e86ffSlogin // 如果要删除的是目录,且不为空,则删除动作未发生,重新加入缓存 1574004e86ffSlogin guard.children.insert(name.to_uppercase(), target.clone()); 1575004e86ffSlogin drop(target_guard); 1576004e86ffSlogin } 1577004e86ffSlogin return Err(r); 1578004e86ffSlogin } 1579004e86ffSlogin } 1580004e86ffSlogin 1581004e86ffSlogin fn get_entry_name(&self, ino: InodeId) -> Result<String, i32> { 1582004e86ffSlogin let guard: SpinLockGuard<FATInode> = self.0.lock(); 1583004e86ffSlogin if guard.metadata.file_type != FileType::Dir { 1584004e86ffSlogin return Err(-(ENOTDIR as i32)); 1585004e86ffSlogin } 1586004e86ffSlogin match ino { 1587004e86ffSlogin 0 => { 1588004e86ffSlogin return Ok(String::from(".")); 1589004e86ffSlogin } 1590004e86ffSlogin 1 => { 1591004e86ffSlogin return Ok(String::from("..")); 1592004e86ffSlogin } 1593004e86ffSlogin ino => { 1594004e86ffSlogin // 暴力遍历所有的children,判断inode id是否相同 1595004e86ffSlogin // TODO: 优化这里,这个地方性能很差! 1596004e86ffSlogin let mut key: Vec<String> = guard 1597004e86ffSlogin .children 1598004e86ffSlogin .keys() 1599004e86ffSlogin .filter(|k| guard.children.get(*k).unwrap().metadata().unwrap().inode_id == ino) 1600004e86ffSlogin .cloned() 1601004e86ffSlogin .collect(); 1602004e86ffSlogin 1603004e86ffSlogin match key.len() { 1604004e86ffSlogin 0=>{return Err(-(ENOENT as i32));} 1605004e86ffSlogin 1=>{return Ok(key.remove(0));} 1606004e86ffSlogin _ => panic!("FatFS get_entry_name: key.len()={key_len}>1, current inode_id={inode_id}, to find={to_find}", key_len=key.len(), inode_id = guard.metadata.inode_id, to_find=ino) 1607004e86ffSlogin } 1608004e86ffSlogin } 1609004e86ffSlogin } 1610004e86ffSlogin } 1611004e86ffSlogin } 1612004e86ffSlogin 1613004e86ffSlogin impl Default for FATFsInfo { 1614004e86ffSlogin fn default() -> Self { 1615004e86ffSlogin return FATFsInfo { 1616004e86ffSlogin lead_sig: FATFsInfo::LEAD_SIG, 1617004e86ffSlogin struc_sig: FATFsInfo::STRUC_SIG, 1618004e86ffSlogin free_count: 0xFFFFFFFF, 1619004e86ffSlogin next_free: RESERVED_CLUSTERS, 1620004e86ffSlogin trail_sig: FATFsInfo::TRAIL_SIG, 1621004e86ffSlogin dirty: false, 1622004e86ffSlogin offset: None, 1623004e86ffSlogin }; 1624004e86ffSlogin } 1625004e86ffSlogin } 1626004e86ffSlogin 1627004e86ffSlogin impl Cluster { 1628004e86ffSlogin pub fn new(cluster: u64) -> Self { 1629004e86ffSlogin return Cluster { 1630004e86ffSlogin cluster_num: cluster, 1631004e86ffSlogin parent_cluster: 0, 1632004e86ffSlogin }; 1633004e86ffSlogin } 1634004e86ffSlogin } 1635004e86ffSlogin 1636004e86ffSlogin /// @brief 用于迭代FAT表的内容的簇迭代器对象 1637004e86ffSlogin #[derive(Debug)] 1638004e86ffSlogin struct ClusterIter<'a> { 1639004e86ffSlogin /// 迭代器的next要返回的簇 1640004e86ffSlogin current_cluster: Option<Cluster>, 1641004e86ffSlogin /// 属于的文件系统 1642004e86ffSlogin fs: &'a FATFileSystem, 1643004e86ffSlogin } 1644004e86ffSlogin 1645004e86ffSlogin impl<'a> Iterator for ClusterIter<'a> { 1646004e86ffSlogin type Item = Cluster; 1647004e86ffSlogin 1648004e86ffSlogin fn next(&mut self) -> Option<Self::Item> { 1649004e86ffSlogin // 当前要返回的簇 1650004e86ffSlogin let ret: Option<Cluster> = self.current_cluster; 1651004e86ffSlogin 1652004e86ffSlogin // 获得下一个要返回簇 1653004e86ffSlogin let new: Option<Cluster> = match self.current_cluster { 1654004e86ffSlogin Some(c) => { 1655004e86ffSlogin let entry: Option<FATEntry> = self.fs.get_fat_entry(c).ok(); 1656004e86ffSlogin match entry { 1657004e86ffSlogin Some(FATEntry::Next(c)) => Some(c), 1658004e86ffSlogin _ => None, 1659004e86ffSlogin } 1660004e86ffSlogin } 1661004e86ffSlogin _ => None, 1662004e86ffSlogin }; 1663004e86ffSlogin 1664004e86ffSlogin self.current_cluster = new; 1665004e86ffSlogin return ret; 1666004e86ffSlogin } 1667004e86ffSlogin } 1668