Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mm): 修复fat文件系统的PageCache同步问题 #1005

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
add9d4b
实现read系统调用读取pagecache
MemoryShore Oct 19, 2024
d2ad608
完善read逻辑
MemoryShore Oct 20, 2024
60e36ca
完善create_pages
MemoryShore Oct 21, 2024
773e36d
添加read_direct
MemoryShore Oct 21, 2024
f99aec5
解决create_pages时的死锁问题
MemoryShore Oct 21, 2024
032db07
修复元数据未更新的问题
MemoryShore Oct 21, 2024
19ea62b
Merge branch 'master' into patch-fix-pagecache
MemoryShore Oct 21, 2024
8365ca3
重构read逻辑
MemoryShore Oct 26, 2024
acc821a
重写write逻辑
MemoryShore Oct 26, 2024
7d13a5b
修正页面回写逻辑
MemoryShore Oct 26, 2024
f97b0b7
Merge branch 'master' into patch-fix-pagecache
MemoryShore Oct 26, 2024
0553021
优化代码
MemoryShore Oct 27, 2024
63bd233
优化代码
MemoryShore Nov 10, 2024
fd59c5b
Merge branch 'master' into patch-fix-pagecache
MemoryShore Nov 10, 2024
1a7e9cc
修复lru链表缩减逻辑
MemoryShore Nov 10, 2024
668a279
修正page_writeback逻辑
MemoryShore Nov 11, 2024
ec98b84
添加write_direct
MemoryShore Nov 11, 2024
74c7059
Merge branch 'master' into patch-fix-pagecache
MemoryShore Nov 12, 2024
7a3608b
完善页面释放逻辑
MemoryShore Nov 12, 2024
0ba9483
完善Page结构体和页面创建流程
MemoryShore Nov 16, 2024
8c8051d
优化PageCache的drop逻辑
MemoryShore Nov 16, 2024
839bef9
PageCache改为HashMap存储页面
MemoryShore Nov 18, 2024
da46956
修正PageCache读写逻辑
MemoryShore Nov 18, 2024
67039ad
完善fat元数据的更新逻辑
MemoryShore Nov 18, 2024
49ab468
允许execve加载文件时缓存到PageCache
MemoryShore Nov 18, 2024
bac195a
优化代码
MemoryShore Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions kernel/src/driver/net/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::arch::mm::kernel_page_flags;
use crate::arch::MMArch;

use crate::mm::kernel_mapper::KernelMapper;
use crate::mm::page::{page_manager_lock_irqsave, EntryFlags};
use crate::mm::page::EntryFlags;
use crate::mm::{
allocator::page_frame::{
allocate_page_frames, deallocate_page_frames, PageFrameCount, PhysPageFrame,
Expand Down Expand Up @@ -61,11 +61,7 @@ pub unsafe fn dma_dealloc(paddr: usize, vaddr: NonNull<u8>, pages: usize) -> i32
flusher.flush();

unsafe {
deallocate_page_frames(
PhysPageFrame::new(PhysAddr::new(paddr)),
page_count,
&mut page_manager_lock_irqsave(),
);
deallocate_page_frames(PhysPageFrame::new(PhysAddr::new(paddr)), page_count);
}
return 0;
}
8 changes: 2 additions & 6 deletions kernel/src/driver/virtio/virtio_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::arch::mm::kernel_page_flags;
use crate::arch::MMArch;

use crate::mm::kernel_mapper::KernelMapper;
use crate::mm::page::{page_manager_lock_irqsave, EntryFlags};
use crate::mm::page::EntryFlags;
use crate::mm::{
allocator::page_frame::{
allocate_page_frames, deallocate_page_frames, PageFrameCount, PhysPageFrame,
Expand Down Expand Up @@ -72,11 +72,7 @@ unsafe impl Hal for HalImpl {
flusher.flush();

unsafe {
deallocate_page_frames(
PhysPageFrame::new(PhysAddr::new(paddr)),
page_count,
&mut page_manager_lock_irqsave(),
);
deallocate_page_frames(PhysPageFrame::new(PhysAddr::new(paddr)), page_count);
}
return 0;
}
Expand Down
125 changes: 97 additions & 28 deletions kernel/src/filesystem/fat/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ pub struct FATInode {
}

impl FATInode {
/// @brief 更新当前inode的元数据
pub fn update_metadata(&mut self) {
// todo: 更新文件的访问时间等信息
/// 将inode的元数据与磁盘同步
pub fn synchronize_metadata(&mut self) {
match &self.inode_type {
FATDirEntry::File(f) | FATDirEntry::VolId(f) => {
self.metadata.size = f.size() as i64;
Expand All @@ -146,6 +145,19 @@ impl FATInode {
};
}

/// 更新inode的元数据
pub fn update_metadata(&mut self, size: Option<i64>) {
if let Some(new_size) = size {
self.metadata.size = new_size;
}
self.update_time();
}

/// 更新访问时间
pub fn update_time(&mut self) {
log::warn!("update_time has not yet been implemented");
}

fn find(&mut self, name: &str) -> Result<Arc<LockedFATInode>, SystemError> {
match &self.inode_type {
FATDirEntry::Dir(d) => {
Expand Down Expand Up @@ -234,7 +246,7 @@ impl LockedFATInode {

inode.0.lock().self_ref = Arc::downgrade(&inode);

inode.0.lock().update_metadata();
inode.0.lock().synchronize_metadata();

return inode;
}
Expand Down Expand Up @@ -1386,24 +1398,14 @@ impl FATFsInfo {
}

impl IndexNode for LockedFATInode {
fn read_at(
&self,
offset: usize,
len: usize,
buf: &mut [u8],
_data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
let mut guard: SpinLockGuard<FATInode> = self.0.lock();
fn read_sync(&self, offset: usize, buf: &mut [u8]) -> Result<usize, SystemError> {
let guard: SpinLockGuard<FATInode> = self.0.lock();
match &guard.inode_type {
FATDirEntry::File(f) | FATDirEntry::VolId(f) => {
let r = f.read(
&guard.fs.upgrade().unwrap(),
&mut buf[0..len],
offset as u64,
);
guard.update_metadata();
fslongjin marked this conversation as resolved.
Show resolved Hide resolved
let r = f.read(&guard.fs.upgrade().unwrap(), buf, offset as u64);
return r;
}

FATDirEntry::Dir(_) => {
return Err(SystemError::EISDIR);
}
Expand All @@ -1414,32 +1416,95 @@ impl IndexNode for LockedFATInode {
}
}

fn write_at(
&self,
offset: usize,
len: usize,
buf: &[u8],
_data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
fn write_sync(&self, offset: usize, buf: &[u8]) -> Result<usize, SystemError> {
let mut guard: SpinLockGuard<FATInode> = self.0.lock();
let fs: &Arc<FATFileSystem> = &guard.fs.upgrade().unwrap();

match &mut guard.inode_type {
FATDirEntry::File(f) | FATDirEntry::VolId(f) => {
let r = f.write(fs, &buf[0..len], offset as u64);
guard.update_metadata();
let r = f.write(fs, buf, offset as u64);
return r;
}

FATDirEntry::Dir(_) => {
return Err(SystemError::EISDIR);
}

FATDirEntry::UnInit => {
error!("FATFS: param: Inode_type uninitialized.");
return Err(SystemError::EROFS);
}
}
}

fn read_at(
&self,
offset: usize,
len: usize,
buf: &mut [u8],
data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
let len = core::cmp::min(len, buf.len());
let buf = &mut buf[0..len];

let page_cache = self.0.lock().page_cache.clone();
if let Some(page_cache) = page_cache {
let r = page_cache.read(offset, &mut buf[0..len]);
// self.0.lock_irqsave().update_metadata();
return r;
} else {
return self.read_direct(offset, len, buf, data);
}
}

fn write_at(
&self,
offset: usize,
len: usize,
buf: &[u8],
data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
let len = core::cmp::min(len, buf.len());
let buf = &buf[0..len];

let page_cache = self.0.lock().page_cache.clone();
if let Some(page_cache) = page_cache {
let write_len = page_cache.write(offset, buf)?;
let mut guard = self.0.lock();
let old_size = guard.metadata.size;
guard.update_metadata(Some(core::cmp::max(old_size, (offset + write_len) as i64)));
return Ok(write_len);
} else {
return self.write_direct(offset, len, buf, data);
}
}

fn read_direct(
&self,
offset: usize,
len: usize,
buf: &mut [u8],
_data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
let len = core::cmp::min(len, buf.len());
let r = self.read_sync(offset, &mut buf[0..len]);
// self.0.lock_irqsave().update_metadata();
return r;
}

fn write_direct(
&self,
offset: usize,
len: usize,
buf: &[u8],
_data: SpinLockGuard<FilePrivateData>,
) -> Result<usize, SystemError> {
let len = core::cmp::min(len, buf.len());
let r = self.write_sync(offset, &buf[0..len]);
// self.0.lock_irqsave().update_metadata();
return r;
}

fn create(
&self,
name: &str,
Expand Down Expand Up @@ -1496,6 +1561,10 @@ impl IndexNode for LockedFATInode {
Ok(())
}
fn resize(&self, len: usize) -> Result<(), SystemError> {
if let Some(page_cache) = self.page_cache() {
return page_cache.resize(len);
}

let mut guard: SpinLockGuard<FATInode> = self.0.lock();
let fs: &Arc<FATFileSystem> = &guard.fs.upgrade().unwrap();
let old_size = guard.metadata.size as usize;
Expand Down Expand Up @@ -1527,7 +1596,7 @@ impl IndexNode for LockedFATInode {
file.truncate(fs, len as u64)?;
}
}
guard.update_metadata();
guard.synchronize_metadata();
return Ok(());
}
FATDirEntry::Dir(_) => return Err(SystemError::ENOSYS),
Expand Down
Loading
Loading