|
@@ -12,6 +12,7 @@ use alloc::{
|
|
|
vec::Vec,
|
|
|
};
|
|
|
|
|
|
+use crate::driver::base::block::gendisk::GenDisk;
|
|
|
use crate::driver::base::device::device_number::DeviceNumber;
|
|
|
use crate::filesystem::vfs::utils::DName;
|
|
|
use crate::filesystem::vfs::{Magic, SpecialNodeData, SuperBlock};
|
|
@@ -71,7 +72,7 @@ impl Eq for Cluster {}
|
|
|
#[derive(Debug)]
|
|
|
pub struct FATFileSystem {
|
|
|
/// 当前文件系统所在的分区
|
|
|
- pub partition: Arc<Partition>,
|
|
|
+ pub gendisk: Arc<GenDisk>,
|
|
|
/// 当前文件系统的BOPB
|
|
|
pub bpb: BiosParameterBlock,
|
|
|
/// 当前文件系统的第一个数据扇区(相对分区开始位置)
|
|
@@ -281,17 +282,16 @@ impl FATFileSystem {
|
|
|
/// FAT32允许的最大簇号
|
|
|
pub const FAT32_MAX_CLUSTER: u32 = 0x0FFFFFF7;
|
|
|
|
|
|
- pub fn new(partition: Arc<Partition>) -> Result<Arc<FATFileSystem>, SystemError> {
|
|
|
- let bpb = BiosParameterBlock::new(partition.clone())?;
|
|
|
-
|
|
|
+ pub fn new(gendisk: Arc<GenDisk>) -> Result<Arc<FATFileSystem>, SystemError> {
|
|
|
+ let bpb = BiosParameterBlock::new(&gendisk)?;
|
|
|
// 从磁盘上读取FAT32文件系统的FsInfo结构体
|
|
|
let fs_info: FATFsInfo = match bpb.fat_type {
|
|
|
FATType::FAT32(bpb32) => {
|
|
|
- let fs_info_in_disk_bytes_offset = partition.lba_start * LBA_SIZE as u64
|
|
|
- + bpb32.fs_info as u64 * bpb.bytes_per_sector as u64;
|
|
|
+ let fs_info_in_gendisk_bytes_offset =
|
|
|
+ bpb32.fs_info as usize * bpb.bytes_per_sector as usize;
|
|
|
FATFsInfo::new(
|
|
|
- partition.clone(),
|
|
|
- fs_info_in_disk_bytes_offset,
|
|
|
+ &gendisk,
|
|
|
+ fs_info_in_gendisk_bytes_offset,
|
|
|
bpb.bytes_per_sector as usize,
|
|
|
)?
|
|
|
}
|
|
@@ -351,7 +351,7 @@ impl FATFileSystem {
|
|
|
})));
|
|
|
|
|
|
let result: Arc<FATFileSystem> = Arc::new(FATFileSystem {
|
|
|
- partition,
|
|
|
+ gendisk,
|
|
|
bpb,
|
|
|
first_data_sector,
|
|
|
fs_info: Arc::new(LockedFATFsInfo::new(fs_info)),
|
|
@@ -398,17 +398,14 @@ impl FATFileSystem {
|
|
|
let fat_bytes_offset =
|
|
|
fat_type.get_fat_bytes_offset(cluster, fat_start_sector, bytes_per_sec);
|
|
|
|
|
|
- // FAT表项所在的LBA地址
|
|
|
- // let fat_ent_lba = self.get_lba_from_offset(self.bytes_to_sector(fat_bytes_offset));
|
|
|
- let fat_ent_lba = self.partition.lba_start + fat_bytes_offset / LBA_SIZE as u64;
|
|
|
+ // FAT表项所在的分区内LBA地址
|
|
|
+ let fat_ent_lba = fat_bytes_offset / LBA_SIZE as u64;
|
|
|
|
|
|
// FAT表项在逻辑块内的字节偏移量
|
|
|
let blk_offset = self.get_in_block_offset(fat_bytes_offset);
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; self.bpb.bytes_per_sector as usize];
|
|
|
- self.partition
|
|
|
- .disk()
|
|
|
- .read_at(fat_ent_lba as usize, self.lba_per_sector(), &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, fat_ent_lba as usize)?;
|
|
|
|
|
|
let mut cursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(blk_offset as i64))?;
|
|
@@ -492,16 +489,14 @@ impl FATFileSystem {
|
|
|
let fat_bytes_offset =
|
|
|
fat_type.get_fat_bytes_offset(cluster, fat_start_sector, bytes_per_sec);
|
|
|
|
|
|
- // FAT表项所在的LBA地址
|
|
|
- let fat_ent_lba = self.get_lba_from_offset(self.bytes_to_sector(fat_bytes_offset));
|
|
|
+ // FAT表项所在的分区内LBA地址
|
|
|
+ let fat_ent_lba = self.gendisk_lba_from_offset(self.bytes_to_sector(fat_bytes_offset));
|
|
|
|
|
|
// FAT表项在逻辑块内的字节偏移量
|
|
|
let blk_offset = self.get_in_block_offset(fat_bytes_offset);
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; self.bpb.bytes_per_sector as usize];
|
|
|
- self.partition
|
|
|
- .disk()
|
|
|
- .read_at(fat_ent_lba, self.lba_per_sector(), &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, fat_ent_lba)?;
|
|
|
|
|
|
let mut cursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(blk_offset as i64))?;
|
|
@@ -529,24 +524,24 @@ impl FATFileSystem {
|
|
|
return Ok(res);
|
|
|
}
|
|
|
|
|
|
- /// @brief 获取当前文件系统的root inode,在磁盘上的字节偏移量
|
|
|
+ /// @brief 获取当前文件系统的root inode,在分区内的字节偏移量
|
|
|
pub fn root_dir_bytes_offset(&self) -> u64 {
|
|
|
match self.bpb.fat_type {
|
|
|
FATType::FAT32(s) => {
|
|
|
let first_sec_cluster: u64 = (s.root_cluster as u64 - 2)
|
|
|
* (self.bpb.sector_per_cluster as u64)
|
|
|
+ self.first_data_sector;
|
|
|
- return (self.get_lba_from_offset(first_sec_cluster) * LBA_SIZE) as u64;
|
|
|
+ return (self.gendisk_lba_from_offset(first_sec_cluster) * LBA_SIZE) as u64;
|
|
|
}
|
|
|
_ => {
|
|
|
let root_sec = (self.bpb.rsvd_sec_cnt as u64)
|
|
|
+ (self.bpb.num_fats as u64) * (self.bpb.fat_size_16 as u64);
|
|
|
- return (self.get_lba_from_offset(root_sec) * LBA_SIZE) as u64;
|
|
|
+ return (self.gendisk_lba_from_offset(root_sec) * LBA_SIZE) as u64;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// @brief 获取当前文件系统的根目录项区域的结束位置,在磁盘上的字节偏移量。
|
|
|
+ /// @brief 获取当前文件系统的根目录项区域的结束位置,在分区内的字节偏移量。
|
|
|
/// 请注意,当前函数只对FAT12/FAT16生效。对于FAT32,返回None
|
|
|
pub fn root_dir_end_bytes_offset(&self) -> Option<u64> {
|
|
|
match self.bpb.fat_type {
|
|
@@ -561,14 +556,14 @@ impl FATFileSystem {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// @brief 获取簇在磁盘内的字节偏移量(相对磁盘起始位置。注意,不是分区内偏移量)
|
|
|
+ /// 获取簇在分区内的字节偏移量
|
|
|
pub fn cluster_bytes_offset(&self, cluster: Cluster) -> u64 {
|
|
|
if cluster.cluster_num >= 2 {
|
|
|
// 指定簇的第一个扇区号
|
|
|
let first_sec_of_cluster = (cluster.cluster_num - 2)
|
|
|
* (self.bpb.sector_per_cluster as u64)
|
|
|
+ self.first_data_sector;
|
|
|
- return (self.get_lba_from_offset(first_sec_of_cluster) * LBA_SIZE) as u64;
|
|
|
+ return first_sec_of_cluster * (self.bpb.bytes_per_sector as u64);
|
|
|
} else {
|
|
|
return 0;
|
|
|
}
|
|
@@ -727,11 +722,10 @@ impl FATFileSystem {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// @brief 根据分区内的扇区偏移量,获得在磁盘上的LBA地址
|
|
|
+ /// 获取分区内的扇区偏移量
|
|
|
#[inline]
|
|
|
- pub fn get_lba_from_offset(&self, in_partition_sec_offset: u64) -> usize {
|
|
|
- return (self.partition.lba_start
|
|
|
- + in_partition_sec_offset * (self.bpb.bytes_per_sector as u64 / LBA_SIZE as u64))
|
|
|
+ pub fn gendisk_lba_from_offset(&self, in_partition_sec_offset: u64) -> usize {
|
|
|
+ return (in_partition_sec_offset * (self.bpb.bytes_per_sector as u64 / LBA_SIZE as u64))
|
|
|
as usize;
|
|
|
}
|
|
|
|
|
@@ -747,12 +741,6 @@ impl FATFileSystem {
|
|
|
return in_partition_bytes_offset / (self.bpb.bytes_per_sector as u64);
|
|
|
}
|
|
|
|
|
|
- /// @brief 根据磁盘上的字节偏移量,获取对应位置在分区内的字节偏移量
|
|
|
- #[inline]
|
|
|
- pub fn get_in_partition_bytes_offset(&self, disk_bytes_offset: u64) -> u64 {
|
|
|
- return disk_bytes_offset - (self.partition.lba_start * LBA_SIZE as u64);
|
|
|
- }
|
|
|
-
|
|
|
/// @brief 根据字节偏移量计算在逻辑块内的字节偏移量
|
|
|
#[inline]
|
|
|
pub fn get_in_block_offset(&self, bytes_offset: u64) -> u64 {
|
|
@@ -888,13 +876,13 @@ impl FATFileSystem {
|
|
|
|
|
|
/// @brief 执行文件系统卸载前的一些准备工作:设置好对应的标志位,并把缓存中的数据刷入磁盘
|
|
|
pub fn umount(&mut self) -> Result<(), SystemError> {
|
|
|
- self.fs_info.0.lock().flush(&self.partition)?;
|
|
|
+ self.fs_info.0.lock().flush(&self.gendisk)?;
|
|
|
|
|
|
self.set_shut_bit_ok()?;
|
|
|
|
|
|
self.set_hard_error_bit_ok()?;
|
|
|
|
|
|
- self.partition.disk().sync()?;
|
|
|
+ self.gendisk.sync()?;
|
|
|
|
|
|
return Ok(());
|
|
|
}
|
|
@@ -959,12 +947,12 @@ impl FATFileSystem {
|
|
|
fat_type.get_fat_bytes_offset(start_cluster, fat_start_sector, bytes_per_sec);
|
|
|
let in_block_offset = self.get_in_block_offset(part_bytes_offset);
|
|
|
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
|
|
|
// 由于FAT12的FAT表不大于6K,因此直接读取6K
|
|
|
let num_lba = (6 * 1024) / LBA_SIZE;
|
|
|
let mut v: Vec<u8> = vec![0; num_lba * LBA_SIZE];
|
|
|
- self.partition.disk().read_at(lba, num_lba, &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1006,12 +994,10 @@ impl FATFileSystem {
|
|
|
);
|
|
|
let in_block_offset = self.get_in_block_offset(part_bytes_offset);
|
|
|
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; self.lba_per_sector() * LBA_SIZE];
|
|
|
- self.partition
|
|
|
- .disk()
|
|
|
- .read_at_sync(lba, self.lba_per_sector(), &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1037,12 +1023,10 @@ impl FATFileSystem {
|
|
|
);
|
|
|
let in_block_offset = self.get_in_block_offset(part_bytes_offset);
|
|
|
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(part_bytes_offset));
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; self.lba_per_sector() * LBA_SIZE];
|
|
|
- self.partition
|
|
|
- .disk()
|
|
|
- .read_at_sync(lba, self.lba_per_sector(), &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1085,10 +1069,10 @@ impl FATFileSystem {
|
|
|
|
|
|
let in_block_offset = self.get_in_block_offset(fat_part_bytes_offset);
|
|
|
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset));
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; LBA_SIZE];
|
|
|
- self.partition.disk().read_at_sync(lba, 1, &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1103,7 +1087,7 @@ impl FATFileSystem {
|
|
|
// 写回数据到磁盘上
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
|
cursor.write_u16(new_val)?;
|
|
|
- self.partition.disk().write_at(lba, 1, cursor.as_slice())?;
|
|
|
+ self.gendisk.write_at(cursor.as_slice(), lba)?;
|
|
|
return Ok(());
|
|
|
}
|
|
|
FATType::FAT16(_) => {
|
|
@@ -1117,16 +1101,16 @@ impl FATFileSystem {
|
|
|
|
|
|
let in_block_offset = self.get_in_block_offset(fat_part_bytes_offset);
|
|
|
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(fat_part_bytes_offset));
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; LBA_SIZE];
|
|
|
- self.partition.disk().read_at(lba, 1, &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
|
|
|
|
cursor.write_u16(raw_val)?;
|
|
|
- self.partition.disk().write_at(lba, 1, cursor.as_slice())?;
|
|
|
+ self.gendisk.write_at(cursor.as_slice(), lba)?;
|
|
|
|
|
|
return Ok(());
|
|
|
}
|
|
@@ -1142,11 +1126,11 @@ impl FATFileSystem {
|
|
|
// 当前操作的FAT表在磁盘上的字节偏移量
|
|
|
let f_offset: u64 = fat_part_bytes_offset + i * fat_size;
|
|
|
let in_block_offset: u64 = self.get_in_block_offset(f_offset);
|
|
|
- let lba = self.get_lba_from_offset(self.bytes_to_sector(f_offset));
|
|
|
+ let lba = self.gendisk_lba_from_offset(self.bytes_to_sector(f_offset));
|
|
|
|
|
|
// debug!("set entry, lba={lba}, in_block_offset={in_block_offset}");
|
|
|
let mut v: Vec<u8> = vec![0; LBA_SIZE];
|
|
|
- self.partition.disk().read_at(lba, 1, &mut v)?;
|
|
|
+ self.gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1181,7 +1165,7 @@ impl FATFileSystem {
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
|
cursor.write_u32(raw_val)?;
|
|
|
|
|
|
- self.partition.disk().write_at(lba, 1, cursor.as_slice())?;
|
|
|
+ self.gendisk.write_at(cursor.as_slice(), lba)?;
|
|
|
}
|
|
|
|
|
|
return Ok(());
|
|
@@ -1189,16 +1173,15 @@ impl FATFileSystem {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// @brief 清空指定的簇
|
|
|
+ /// # 清空指定的簇
|
|
|
///
|
|
|
- /// @param cluster 要被清空的簇
|
|
|
+ /// # 参数
|
|
|
+ /// - cluster 要被清空的簇
|
|
|
pub fn zero_cluster(&self, cluster: Cluster) -> Result<(), SystemError> {
|
|
|
// 准备数据,用于写入
|
|
|
let zeros: Vec<u8> = vec![0u8; self.bytes_per_cluster() as usize];
|
|
|
- let offset: usize = self.cluster_bytes_offset(cluster) as usize;
|
|
|
- self.partition
|
|
|
- .disk()
|
|
|
- .write_at_bytes(offset, zeros.len(), zeros.as_slice())?;
|
|
|
+ let offset = self.cluster_bytes_offset(cluster) as usize;
|
|
|
+ self.gendisk.write_at_bytes(&zeros, offset)?;
|
|
|
return Ok(());
|
|
|
}
|
|
|
}
|
|
@@ -1225,19 +1208,18 @@ impl FATFsInfo {
|
|
|
/// @brief 从磁盘上读取FAT文件系统的FSInfo结构体
|
|
|
///
|
|
|
/// @param partition 磁盘分区
|
|
|
- /// @param in_disk_fs_info_offset FSInfo扇区在磁盘内的字节偏移量(单位:字节)
|
|
|
+ /// @param in_gendisk_fs_info_offset FSInfo扇区在gendisk内的字节偏移量(单位:字节)
|
|
|
/// @param bytes_per_sec 每扇区字节数
|
|
|
pub fn new(
|
|
|
- partition: Arc<Partition>,
|
|
|
- in_disk_fs_info_offset: u64,
|
|
|
+ gendisk: &Arc<GenDisk>,
|
|
|
+ in_gendisk_fs_info_offset: usize,
|
|
|
bytes_per_sec: usize,
|
|
|
) -> Result<Self, SystemError> {
|
|
|
let mut v = vec![0; bytes_per_sec];
|
|
|
|
|
|
- // 计算fs_info扇区在磁盘上的字节偏移量,从磁盘读取数据
|
|
|
- partition
|
|
|
- .disk()
|
|
|
- .read_at_sync(in_disk_fs_info_offset as usize / LBA_SIZE, 1, &mut v)?;
|
|
|
+ // 读取磁盘上的FsInfo扇区
|
|
|
+ gendisk.read_at_bytes(&mut v, in_gendisk_fs_info_offset)?;
|
|
|
+
|
|
|
let mut cursor = VecCursor::new(v);
|
|
|
|
|
|
let mut fsinfo = FATFsInfo {
|
|
@@ -1253,7 +1235,7 @@ impl FATFsInfo {
|
|
|
|
|
|
fsinfo.trail_sig = cursor.read_u32()?;
|
|
|
fsinfo.dirty = false;
|
|
|
- fsinfo.offset = Some(in_disk_fs_info_offset);
|
|
|
+ fsinfo.offset = Some(gendisk.disk_bytes_offset(in_gendisk_fs_info_offset) as u64);
|
|
|
|
|
|
if fsinfo.is_valid() {
|
|
|
return Ok(fsinfo);
|
|
@@ -1322,14 +1304,14 @@ impl FATFsInfo {
|
|
|
/// @brief 把fs info刷入磁盘
|
|
|
///
|
|
|
/// @param partition fs info所在的分区
|
|
|
- pub fn flush(&self, partition: &Arc<Partition>) -> Result<(), SystemError> {
|
|
|
+ pub fn flush(&self, gendisk: &Arc<GenDisk>) -> Result<(), SystemError> {
|
|
|
if let Some(off) = self.offset {
|
|
|
let in_block_offset = off % LBA_SIZE as u64;
|
|
|
|
|
|
let lba = off as usize / LBA_SIZE;
|
|
|
|
|
|
let mut v: Vec<u8> = vec![0; LBA_SIZE];
|
|
|
- partition.disk().read_at(lba, 1, &mut v)?;
|
|
|
+ gendisk.read_at(&mut v, lba)?;
|
|
|
|
|
|
let mut cursor: VecCursor = VecCursor::new(v);
|
|
|
cursor.seek(SeekFrom::SeekSet(in_block_offset as i64))?;
|
|
@@ -1342,7 +1324,7 @@ impl FATFsInfo {
|
|
|
cursor.seek(SeekFrom::SeekCurrent(12))?;
|
|
|
cursor.write_u32(self.trail_sig)?;
|
|
|
|
|
|
- partition.disk().write_at(lba, 1, cursor.as_slice())?;
|
|
|
+ gendisk.write_at(cursor.as_slice(), lba)?;
|
|
|
}
|
|
|
return Ok(());
|
|
|
}
|