Use SpinLock on FileTable for efficiency

This commit is contained in:
Shaowei Song
2024-09-13 03:36:33 +00:00
committed by Tate, Hongliang Tian
parent 4a43e317b2
commit 1186fb7ca9
14 changed files with 108 additions and 66 deletions

View File

@ -13,8 +13,10 @@ use crate::{
pub fn sys_fchmod(fd: FileDesc, mode: u16, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}, mode = 0o{:o}", fd, mode);
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
file.set_mode(InodeMode::from_bits_truncate(mode))?;
Ok(SyscallReturn::Return(0))
}

View File

@ -20,8 +20,10 @@ pub fn sys_fchown(fd: FileDesc, uid: i32, gid: i32, ctx: &Context) -> Result<Sys
return Ok(SyscallReturn::Return(0));
}
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
if let Some(uid) = uid {
file.set_owner(uid)?;
}

View File

@ -11,7 +11,7 @@ use crate::{
},
},
prelude::*,
process::Pid,
process::{process_table, Pid},
};
pub fn sys_fcntl(fd: FileDesc, cmd: i32, arg: u64, ctx: &Context) -> Result<SyscallReturn> {
@ -142,8 +142,6 @@ fn handle_getown(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
}
fn handle_setown(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn> {
let file_table = ctx.process.file_table().lock();
let file_entry = file_table.get_entry(fd)?;
// A process ID is specified as a positive value; a process group ID is specified as a negative value.
let abs_arg = (arg as i32).unsigned_abs();
if abs_arg > i32::MAX as u32 {
@ -151,7 +149,19 @@ fn handle_setown(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn>
}
let pid = Pid::try_from(abs_arg)
.map_err(|_| Error::with_message(Errno::EINVAL, "invalid process (group) id"))?;
file_entry.set_owner(pid)?;
let owner_process = if pid == 0 {
None
} else {
Some(process_table::get_process(pid).ok_or(Error::with_message(
Errno::ESRCH,
"cannot set_owner with an invalid pid",
))?)
};
let mut file_table = ctx.process.file_table().lock();
let file_entry = file_table.get_entry_mut(fd)?;
file_entry.set_owner(owner_process.as_ref())?;
Ok(SyscallReturn::Return(0))
}

View File

@ -15,8 +15,11 @@ pub fn sys_ioctl(fd: FileDesc, cmd: u32, arg: Vaddr, ctx: &Context) -> Result<Sy
"fd = {}, ioctl_cmd = {:?}, arg = 0x{:x}",
fd, ioctl_cmd, arg
);
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
let res = match ioctl_cmd {
IoctlCmd::FIONBIO => {
let is_nonblocking = ctx.get_user_space().read_val::<i32>(arg)? != 0;

View File

@ -8,6 +8,7 @@ use crate::{
pub fn sys_lseek(fd: FileDesc, offset: isize, whence: u32, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}, offset = {}, whence = {}", fd, offset, whence);
let seek_from = match whence {
0 => {
if offset < 0 {
@ -19,8 +20,11 @@ pub fn sys_lseek(fd: FileDesc, offset: isize, whence: u32, ctx: &Context) -> Res
2 => SeekFrom::End(offset),
_ => return_errno!(Errno::EINVAL),
};
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
let offset = file.seek(seek_from)?;
Ok(SyscallReturn::Return(offset as _))
}

View File

@ -15,8 +15,11 @@ use crate::{
pub fn sys_fstat(fd: FileDesc, stat_buf_ptr: Vaddr, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}, stat_buf_addr = 0x{:x}", fd, stat_buf_ptr);
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
let stat = Stat::from(file.metadata());
ctx.get_user_space().write_val(stat_buf_ptr, &stat)?;
Ok(SyscallReturn::Return(0))

View File

@ -29,13 +29,16 @@ pub fn sys_statfs(path_ptr: Vaddr, statfs_buf_ptr: Vaddr, ctx: &Context) -> Resu
pub fn sys_fstatfs(fd: FileDesc, statfs_buf_ptr: Vaddr, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}, statfs_buf_addr = 0x{:x}", fd, statfs_buf_ptr);
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let inode_handle = file
.downcast_ref::<InodeHandle>()
.ok_or(Error::with_message(Errno::EBADF, "not inode"))?;
let dentry = inode_handle.dentry();
let statfs = Statfs::from(dentry.fs().sb());
let fs = {
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let inode_handle = file
.downcast_ref::<InodeHandle>()
.ok_or(Error::with_message(Errno::EBADF, "not inode"))?;
inode_handle.dentry().fs()
};
let statfs = Statfs::from(fs.sb());
ctx.get_user_space().write_val(statfs_buf_ptr, &statfs)?;
Ok(SyscallReturn::Return(0))
}

View File

@ -16,8 +16,11 @@ pub fn sys_ftruncate(fd: FileDesc, len: isize, ctx: &Context) -> Result<SyscallR
check_length(len, ctx)?;
let file_table = ctx.process.file_table().lock();
let file = file_table.get_file(fd)?;
let file = {
let file_table = ctx.process.file_table().lock();
file_table.get_file(fd)?.clone()
};
file.resize(len as usize)?;
Ok(SyscallReturn::Return(0))
}