Add support for close syscall

This commit is contained in:
LI Qing 2023-02-15 16:46:42 +08:00
parent 551406b9f4
commit 15d2e07a8c
4 changed files with 25 additions and 3 deletions

View File

@ -28,4 +28,8 @@ pub trait File: Send + Sync + Any {
fn poll(&self) -> IoEvents {
IoEvents::empty()
}
fn flush(&self) -> Result<()> {
Ok(())
}
}

View File

@ -6,6 +6,7 @@ mod inode_handle;
use crate::prelude::*;
use crate::rights::{ReadOp, WriteOp};
use alloc::sync::Arc;
use core::ops::Range;
pub use self::file::File;
pub use self::inode_handle::InodeHandle;
@ -65,4 +66,20 @@ impl FileHandle {
}
}
}
pub fn clean_for_close(&self) -> Result<()> {
match &self.inner {
Inner::Inode(inode_handle) => {
let dentry = inode_handle.dentry();
let ref_count = Arc::strong_count(dentry);
// The dentry is held by dentry cache and self
if ref_count == 2 {
let page_cache_size = dentry.vnode().pages().size();
dentry.vnode().pages().decommit(0..page_cache_size)?;
}
}
Inner::File(file) => file.flush()?,
}
Ok(())
}
}

View File

@ -58,8 +58,8 @@ impl FileTable {
fd
}
pub fn close_file(&mut self, fd: FileDescripter) {
self.table.remove(&fd);
pub fn close_file(&mut self, fd: FileDescripter) -> Option<FileHandle> {
self.table.remove(&fd)
}
pub fn get_file(&self, fd: FileDescripter) -> Result<&FileHandle> {

View File

@ -9,6 +9,7 @@ pub fn sys_close(fd: FileDescripter) -> Result<SyscallReturn> {
let current = current!();
let mut file_table = current.file_table().lock();
let _ = file_table.get_file(fd)?;
file_table.close_file(fd);
let file = file_table.close_file(fd).unwrap();
file.clean_for_close()?;
Ok(SyscallReturn::Return(0))
}