Drop files at the correct time

This commit is contained in:
Ruihan Li
2025-04-05 14:28:15 +08:00
committed by Tate, Hongliang Tian
parent 7e1abc1fbb
commit 8600278a5f
62 changed files with 223 additions and 141 deletions

View File

@ -47,7 +47,7 @@ fn do_accept(
flags: Flags,
ctx: &Context,
) -> Result<FileDesc> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;
@ -74,7 +74,7 @@ fn do_accept(
}
let fd = {
let mut file_table_locked = file_table.write();
let mut file_table_locked = file_table.unwrap().write();
file_table_locked.insert(connected_socket, fd_flags)
};

View File

@ -16,7 +16,7 @@ pub fn sys_bind(
let socket_addr = read_socket_addr_from_user(sockaddr_ptr, addrlen as usize)?;
debug!("sockfd = {sockfd}, socket_addr = {socket_addr:?}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -35,7 +35,7 @@ pub fn sys_fchdir(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}", fd);
let dentry = {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
file.as_inode_or_err()?.dentry().clone()
};

View File

@ -13,7 +13,7 @@ use crate::{
pub fn sys_fchmod(fd: FileDesc, mode: u16, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}, mode = 0o{:o}", fd, mode);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
file.set_mode(InodeMode::from_bits_truncate(mode))?;
Ok(SyscallReturn::Return(0))

View File

@ -20,7 +20,7 @@ pub fn sys_fchown(fd: FileDesc, uid: i32, gid: i32, ctx: &Context) -> Result<Sys
return Ok(SyscallReturn::Return(0));
}
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
if let Some(uid) = uid {
file.set_owner(uid)?;

View File

@ -7,8 +7,8 @@ pub fn sys_close(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}", fd);
let file = {
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let _ = file_table_locked.get_file(fd)?;
file_table_locked.close_file(fd).unwrap()
};

View File

@ -16,7 +16,7 @@ pub fn sys_connect(
let socket_addr = read_socket_addr_from_user(sockaddr_ptr, addr_len as _)?;
debug!("fd = {sockfd}, socket_addr = {socket_addr:?}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -10,8 +10,8 @@ use crate::{
pub fn sys_dup(old_fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
debug!("old_fd = {}", old_fd);
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let new_fd = file_table_locked.dup(old_fd, 0, FdFlags::empty())?;
Ok(SyscallReturn::Return(new_fd as _))
@ -21,7 +21,7 @@ pub fn sys_dup2(old_fd: FileDesc, new_fd: FileDesc, ctx: &Context) -> Result<Sys
debug!("old_fd = {}, new_fd = {}", old_fd, new_fd);
if old_fd == new_fd {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let _file = get_file_fast!(&mut file_table, old_fd);
return Ok(SyscallReturn::Return(new_fd as _));
}
@ -66,8 +66,8 @@ fn do_dup3(
return_errno!(Errno::EBADF);
}
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let _ = file_table_locked.close_file(new_fd);
let new_fd = file_table_locked.dup(old_fd, new_fd, flags)?;

View File

@ -41,8 +41,8 @@ pub fn sys_epoll_create1(flags: u32, ctx: &Context) -> Result<SyscallReturn> {
};
let epoll_file: Arc<EpollFile> = EpollFile::new();
let file_table = ctx.thread_local.file_table().borrow();
let fd = file_table.write().insert(epoll_file, fd_flags);
let file_table = ctx.thread_local.borrow_file_table();
let fd = file_table.unwrap().write().insert(epoll_file, fd_flags);
Ok(SyscallReturn::Return(fd as _))
}
@ -79,9 +79,9 @@ pub fn sys_epoll_ctl(
_ => return_errno_with_message!(Errno::EINVAL, "invalid op"),
};
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, epfd).into_owned();
// Drop `file_table` as `EpollFile::control` also performs `file_table().borrow_mut()`.
// Drop `file_table` as `EpollFile::control` also performs `borrow_file_table_mut()`.
drop(file_table);
let epoll_file = file
@ -110,7 +110,7 @@ fn do_epoll_wait(
None
};
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, epfd);
let epoll_file = file
.downcast_ref::<EpollFile>()

View File

@ -54,8 +54,8 @@ pub fn sys_eventfd2(init_val: u64, flags: u32, ctx: &Context) -> Result<SyscallR
fn do_sys_eventfd2(init_val: u64, flags: Flags, ctx: &Context) -> FileDesc {
let event_file = EventFile::new(init_val, flags);
let fd = {
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let fd_flags = if flags.contains(Flags::EFD_CLOEXEC) {
FdFlags::CLOEXEC
} else {

View File

@ -62,7 +62,7 @@ fn lookup_executable_file(
ctx: &Context,
) -> Result<Dentry> {
let dentry = if flags.contains(OpenFlags::AT_EMPTY_PATH) && filename.is_empty() {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, dfd);
file.as_inode_or_err()?.dentry().clone()
} else {
@ -111,8 +111,8 @@ fn do_execve(
// Ensure that the file descriptors with the close-on-exec flag are closed.
// FIXME: This is just wrong if the file table is shared with other processes.
let closed_files = thread_local
.file_table()
.borrow()
.borrow_file_table()
.unwrap()
.write()
.close_files_on_exec();
drop(closed_files);

View File

@ -24,7 +24,7 @@ pub fn sys_fallocate(
check_offset_and_len(offset, len, ctx)?;
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let falloc_mode = FallocMode::try_from(

View File

@ -35,13 +35,16 @@ pub fn sys_fcntl(fd: FileDesc, cmd: i32, arg: u64, ctx: &Context) -> Result<Sysc
}
fn handle_dupfd(fd: FileDesc, arg: u64, flags: FdFlags, ctx: &Context) -> Result<SyscallReturn> {
let file_table = ctx.thread_local.file_table().borrow();
let new_fd = file_table.write().dup(fd, arg as FileDesc, flags)?;
let file_table = ctx.thread_local.borrow_file_table();
let new_fd = file_table
.unwrap()
.write()
.dup(fd, arg as FileDesc, flags)?;
Ok(SyscallReturn::Return(new_fd as _))
}
fn handle_getfd(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
file_table.read_with(|inner| {
let fd_flags = inner.get_entry(fd)?.flags();
Ok(SyscallReturn::Return(fd_flags.bits() as _))
@ -54,7 +57,7 @@ fn handle_setfd(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn>
} else {
FdFlags::from_bits(arg as u8).ok_or(Error::with_message(Errno::EINVAL, "invalid flags"))?
};
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
file_table.read_with(|inner| {
inner.get_entry(fd)?.set_flags(flags);
Ok(SyscallReturn::Return(0))
@ -62,7 +65,7 @@ fn handle_setfd(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn>
}
fn handle_getfl(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let status_flags = file.status_flags();
let access_mode = file.access_mode();
@ -72,7 +75,7 @@ fn handle_getfl(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
}
fn handle_setfl(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let valid_flags_mask = StatusFlags::O_APPEND
| StatusFlags::O_ASYNC
@ -87,7 +90,7 @@ fn handle_setfl(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn>
}
fn handle_getlk(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let lock_mut_ptr = arg as Vaddr;
let mut lock_mut_c = ctx.user_space().read_val::<c_flock>(lock_mut_ptr)?;
@ -112,7 +115,7 @@ fn handle_setlk(
is_nonblocking: bool,
ctx: &Context,
) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let lock_mut_ptr = arg as Vaddr;
let lock_mut_c = ctx.user_space().read_val::<c_flock>(lock_mut_ptr)?;
@ -127,7 +130,7 @@ fn handle_setlk(
}
fn handle_getown(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
file_table.read_with(|inner| {
let pid = inner.get_entry(fd)?.owner().unwrap_or(0);
Ok(SyscallReturn::Return(pid as _))
@ -152,8 +155,8 @@ fn handle_setown(fd: FileDesc, arg: u64, ctx: &Context) -> Result<SyscallReturn>
))?)
};
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let file_entry = file_table_locked.get_entry_mut(fd)?;
file_entry.set_owner(owner_process.as_ref())?;
Ok(SyscallReturn::Return(0))

View File

@ -12,7 +12,7 @@ use crate::{
pub fn sys_flock(fd: FileDesc, ops: i32, ctx: &Context) -> Result<SyscallReturn> {
debug!("flock: fd: {}, ops: {:?}", fd, ops);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let inode_file = file.as_inode_or_err()?;
let ops: FlockOps = FlockOps::from_i32(ops)?;

View File

@ -9,7 +9,7 @@ use crate::{
pub fn sys_fsync(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}", fd);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let dentry = file.as_inode_or_err()?.dentry();
dentry.sync_all()?;
@ -19,7 +19,7 @@ pub fn sys_fsync(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
pub fn sys_fdatasync(fd: FileDesc, ctx: &Context) -> Result<SyscallReturn> {
debug!("fd = {}", fd);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let dentry = file.as_inode_or_err()?.dentry();
dentry.sync_data()?;

View File

@ -22,7 +22,7 @@ pub fn sys_getdents(
fd, buf_addr, buf_len
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let inode_handle = file.as_inode_or_err()?;
if inode_handle.dentry().type_() != InodeType::Dir {
@ -48,7 +48,7 @@ pub fn sys_getdents64(
fd, buf_addr, buf_len
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let inode_handle = file.as_inode_or_err()?;
if inode_handle.dentry().type_() != InodeType::Dir {

View File

@ -15,7 +15,7 @@ pub fn sys_getpeername(
) -> Result<SyscallReturn> {
debug!("sockfd = {sockfd}, addr = 0x{addr:x}, addrlen_ptr = 0x{addrlen_ptr:x}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -15,7 +15,7 @@ pub fn sys_getsockname(
) -> Result<SyscallReturn> {
debug!("sockfd = {sockfd}, addr = 0x{addr:x}, addrlen_ptr = 0x{addrlen_ptr:x}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -25,7 +25,7 @@ pub fn sys_getsockopt(
debug!("level = {level:?}, sockfd = {sockfd}, optname = {optname:?}, optlen = {optlen}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -67,7 +67,7 @@ pub fn sys_fgetxattr(
value_len: usize,
ctx: &Context,
) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let user_space = ctx.user_space();

View File

@ -16,7 +16,7 @@ pub fn sys_ioctl(fd: FileDesc, cmd: u32, arg: Vaddr, ctx: &Context) -> Result<Sy
fd, ioctl_cmd, arg
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let res = match ioctl_cmd {
IoctlCmd::FIONBIO => {

View File

@ -9,7 +9,7 @@ use crate::{
pub fn sys_listen(sockfd: FileDesc, backlog: i32, ctx: &Context) -> Result<SyscallReturn> {
debug!("sockfd = {sockfd}, backlog = {backlog}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -60,7 +60,7 @@ pub fn sys_flistxattr(
list_len: usize,
ctx: &Context,
) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let user_space = ctx.user_space();

View File

@ -23,7 +23,7 @@ pub fn sys_lseek(fd: FileDesc, offset: isize, whence: u32, ctx: &Context) -> Res
2 => SeekFrom::End(offset),
_ => return_errno!(Errno::EINVAL),
};
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let offset = file.seek(seek_from)?;

View File

@ -125,7 +125,7 @@ fn do_sys_mmap(
}
} else {
let vmo = {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let inode_handle = file.as_inode_or_err()?;

View File

@ -42,8 +42,8 @@ pub fn sys_openat(
};
let fd = {
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let fd_flags =
if CreationFlags::from_bits_truncate(flags).contains(CreationFlags::O_CLOEXEC) {
FdFlags::CLOEXEC

View File

@ -21,8 +21,8 @@ pub fn sys_pipe2(fds: Vaddr, flags: u32, ctx: &Context) -> Result<SyscallReturn>
FdFlags::empty()
};
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let pipe_fds = PipeFds {
reader_fd: file_table_locked.insert(pipe_reader, fd_flags),

View File

@ -59,7 +59,8 @@ pub fn sys_poll(fds: Vaddr, nfds: u64, timeout: i32, ctx: &Context) -> Result<Sy
}
pub fn do_poll(poll_fds: &[PollFd], timeout: Option<&Duration>, ctx: &Context) -> Result<usize> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file_table = file_table.unwrap();
let poll_files = if let Some(file_table_inner) = file_table.get() {
PollFiles::new_borrowed(poll_fds, file_table_inner)

View File

@ -22,7 +22,7 @@ pub fn sys_pread64(
return_errno_with_message!(Errno::EINVAL, "offset cannot be negative");
}
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
// TODO: Check (f.file->f_mode & FMODE_PREAD); We don't have f_mode in our FileLike trait

View File

@ -65,7 +65,7 @@ fn do_sys_preadv(
return_errno_with_message!(Errno::EINVAL, "offset cannot be negative");
}
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
if io_vec_count == 0 {
@ -126,7 +126,7 @@ fn do_sys_readv(
fd, io_vec_ptr, io_vec_count
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
if io_vec_count == 0 {

View File

@ -22,7 +22,7 @@ pub fn sys_pwrite64(
return_errno_with_message!(Errno::EINVAL, "offset cannot be negative");
}
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
// TODO: Check (f.file->f_mode & FMODE_PWRITE); We don't have f_mode in our FileLike trait

View File

@ -66,7 +66,7 @@ fn do_sys_pwritev(
return_errno_with_message!(Errno::EINVAL, "offset cannot be negative");
}
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
// TODO: Check (f.file->f_mode & FMODE_PREAD); We don't have f_mode in our FileLike trait
@ -123,7 +123,7 @@ fn do_sys_writev(
fd, io_vec_ptr, io_vec_count
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let mut total_len = 0;

View File

@ -17,7 +17,7 @@ pub fn sys_read(
fd, user_buf_addr, buf_len
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
// According to <https://man7.org/linux/man-pages/man2/read.2.html>, if

View File

@ -20,7 +20,7 @@ pub fn sys_recvfrom(
let flags = SendRecvFlags::from_bits_truncate(flags);
debug!("sockfd = {sockfd}, buf = 0x{buf:x}, len = {len}, flags = {flags:?}, src_addr = 0x{src_addr:x}, addrlen_ptr = 0x{addrlen_ptr:x}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -22,7 +22,7 @@ pub fn sys_recvmsg(
sockfd, c_user_msghdr, flags
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -32,7 +32,7 @@ pub fn sys_lremovexattr(path_ptr: Vaddr, name_ptr: Vaddr, ctx: &Context) -> Resu
}
pub fn sys_fremovexattr(fd: FileDesc, name_ptr: Vaddr, ctx: &Context) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let user_space = ctx.user_space();

View File

@ -38,8 +38,7 @@ pub fn sys_sendfile(
let (out_file, in_file) = ctx
.thread_local
.file_table()
.borrow_mut()
.borrow_file_table_mut()
.read_with(|inner| {
let out_file = inner.get_file(out_fd)?.clone();
// FIXME: the in_file must support mmap-like operations (i.e., it cannot be a socket).

View File

@ -22,7 +22,7 @@ pub fn sys_sendmsg(
sockfd, c_user_msghdr, flags
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -26,7 +26,7 @@ pub fn sys_sendto(
};
debug!("sockfd = {sockfd}, buf = 0x{buf:x}, len = 0x{len:x}, flags = {flags:?}, socket_addr = {socket_addr:?}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -25,7 +25,7 @@ pub fn sys_setsockopt(
level, sockfd, optname, optlen
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -74,7 +74,7 @@ pub fn sys_fsetxattr(
flags: i32,
ctx: &Context,
) -> Result<SyscallReturn> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let user_space = ctx.user_space();

View File

@ -11,7 +11,7 @@ pub fn sys_shutdown(sockfd: FileDesc, how: i32, ctx: &Context) -> Result<Syscall
let shutdown_cmd = SockShutdownCmd::try_from(how)?;
debug!("sockfd = {sockfd}, cmd = {shutdown_cmd:?}");
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, sockfd);
let socket = file.as_socket_or_err()?;

View File

@ -94,8 +94,8 @@ fn create_new_signalfd(
register_observer(ctx, &signal_file, mask)?;
let file_table = ctx.thread_local.file_table().borrow();
let fd = file_table.write().insert(signal_file, fd_flags);
let file_table = ctx.thread_local.borrow_file_table();
let fd = file_table.unwrap().write().insert(signal_file, fd_flags);
Ok(fd)
}
@ -105,7 +105,7 @@ fn update_existing_signalfd(
new_mask: SigMask,
non_blocking: bool,
) -> Result<FileDesc> {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let signal_file = file
.downcast_ref::<SignalFile>()

View File

@ -43,8 +43,8 @@ pub fn sys_socket(domain: i32, type_: i32, protocol: i32, ctx: &Context) -> Resu
_ => return_errno_with_message!(Errno::EAFNOSUPPORT, "unsupported domain"),
};
let fd = {
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let fd_flags = if sock_flags.contains(SockFlags::SOCK_CLOEXEC) {
FdFlags::CLOEXEC
} else {

View File

@ -37,8 +37,8 @@ pub fn sys_socketpair(
};
let socket_fds = {
let file_table = ctx.thread_local.file_table().borrow();
let mut file_table_locked = file_table.write();
let file_table = ctx.thread_local.borrow_file_table();
let mut file_table_locked = file_table.unwrap().write();
let fd_flags = if sock_flags.contains(SockFlags::SOCK_CLOEXEC) {
FdFlags::CLOEXEC
} else {

View File

@ -15,7 +15,7 @@ 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 mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
let stat = Stat::from(file.metadata());

View File

@ -29,7 +29,7 @@ pub fn sys_fstatfs(fd: FileDesc, statfs_buf_ptr: Vaddr, ctx: &Context) -> Result
debug!("fd = {}, statfs_buf_addr = 0x{:x}", fd, statfs_buf_ptr);
let fs = {
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
file.as_inode_or_err()?.dentry().fs()
};

View File

@ -16,7 +16,7 @@ pub fn sys_ftruncate(fd: FileDesc, len: isize, ctx: &Context) -> Result<SyscallR
check_length(len, ctx)?;
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
file.resize(len as usize)?;
Ok(SyscallReturn::Return(0))

View File

@ -17,7 +17,7 @@ pub fn sys_write(
fd, user_buf_ptr, user_buf_len
);
let mut file_table = ctx.thread_local.file_table().borrow_mut();
let mut file_table = ctx.thread_local.borrow_file_table_mut();
let file = get_file_fast!(&mut file_table, fd);
// According to <https://man7.org/linux/man-pages/man2/write.2.html>, if