refactor file.rs

This commit is contained in:
Garen Tyler 2023-11-09 22:29:41 -07:00
parent f3e31d869a
commit 52cad30dad
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
7 changed files with 22 additions and 42 deletions

View File

@ -55,7 +55,7 @@ pub unsafe fn devintr() -> i32 {
// IRQ indicates which device interrupted.
let irq = interrupt::handle_interrupt();
let mut uart_interrupt = false;
for (uart_irq, uart) in &crate::hardware::UARTS {
if irq == *uart_irq {
@ -63,7 +63,7 @@ pub unsafe fn devintr() -> i32 {
uart.interrupt();
}
}
if !uart_interrupt {
if irq == VIRTIO0_IRQ {
virtio_disk_intr();

View File

@ -12,12 +12,12 @@ pub mod printf;
use crate::{
fs::file::{devsw, CONSOLE},
hardware::uart::Uart,
proc::{
process::{procdump, Process},
scheduler::wakeup,
},
sync::mutex::Mutex,
hardware::uart::Uart,
};
use core::{ffi::c_void, ptr::addr_of_mut};
@ -26,7 +26,7 @@ extern "C" {
fn either_copyout(user_dst: i32, dst: u64, src: *mut c_void, len: u64) -> i32;
}
pub static UART0: &'static Uart = &crate::hardware::UARTS[0].1;
pub static UART0: &Uart = &crate::hardware::UARTS[0].1;
pub const BACKSPACE: u8 = 0x00;
pub const INPUT_BUF_SIZE: usize = 128;

View File

@ -5,7 +5,7 @@ use crate::{
fs::{log, stat::Stat},
io::pipe::Pipe,
proc::process::Process,
sync::{sleeplock::Sleeplock, spinlock::Spinlock},
sync::{mutex::Mutex, sleeplock::Sleeplock, spinlock::Spinlock},
};
use core::ptr::{addr_of_mut, null_mut};
@ -36,8 +36,9 @@ pub struct File {
/// FileType::Device
pub major: i16,
}
unsafe impl Send for File {}
impl File {
pub const unsafe fn uninitialized() -> File {
pub const fn uninitialized() -> File {
File {
kind: FileType::None,
references: 0,
@ -121,33 +122,15 @@ pub struct FileTable {
#[no_mangle]
pub static mut devsw: [Devsw; crate::NDEV] = [Devsw::new(); crate::NDEV];
#[no_mangle]
pub static mut ftable: FileTable = FileTable {
lock: Spinlock::new(),
files: unsafe { [File::uninitialized(); crate::NFILE] },
};
pub static FILES: Mutex<[File; crate::NFILE]> = Mutex::new([File::uninitialized(); crate::NFILE]);
pub const CONSOLE: usize = 1;
extern "C" {
// pub fn fileinit();
// pub fn filealloc() -> *mut File;
// pub fn filedup(file: *mut File) -> *mut File;
// pub fn fileclose(file: *mut File);
// pub fn filestat(file: *mut File, addr: u64) -> i32;
// pub fn fileread(file: *mut File, addr: u64, n: i32) -> i32;
// pub fn filewrite(file: *mut File, addr: u64, n: i32) -> i32;
}
pub unsafe fn fileinit() {
ftable.lock = Spinlock::new();
}
/// Allocate a file structure.
#[no_mangle]
pub unsafe extern "C" fn filealloc() -> *mut File {
let _guard = ftable.lock.lock();
let mut files = FILES.lock_spinning();
for file in &mut ftable.files {
for file in files.as_mut() {
if file.references == 0 {
file.references = 1;
return addr_of_mut!(*file);
@ -158,9 +141,8 @@ pub unsafe extern "C" fn filealloc() -> *mut File {
}
/// Increment reference count for file `file`.
#[no_mangle]
pub unsafe extern "C" fn filedup(file: *mut File) -> *mut File {
let _guard = ftable.lock.lock();
pub unsafe fn filedup(file: *mut File) -> *mut File {
let _guard = FILES.lock_spinning();
if (*file).references < 1 {
panic!("filedup");
@ -176,7 +158,7 @@ pub unsafe extern "C" fn filedup(file: *mut File) -> *mut File {
/// Decrement reference count, and close when reaching 0.
#[no_mangle]
pub unsafe extern "C" fn fileclose(file: *mut File) {
let guard = ftable.lock.lock();
let guard = FILES.lock_spinning();
if (*file).references < 1 {
panic!("fileclose");
@ -204,8 +186,7 @@ pub unsafe extern "C" fn fileclose(file: *mut File) {
/// Get metadata about file `file`.
///
/// `addr` is a user virtual address, pointing to a Stat.
#[no_mangle]
pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 {
pub unsafe fn filestat(file: *mut File, addr: u64) -> i32 {
let proc = Process::current().unwrap();
let mut stat = Stat::default();
@ -234,8 +215,7 @@ pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 {
/// Read from file `file`.
///
/// `addr` is a user virtual address.
#[no_mangle]
pub unsafe extern "C" fn fileread(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
pub unsafe fn fileread(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
if (*file).readable == 0 {
return -1;
}
@ -270,8 +250,7 @@ pub unsafe extern "C" fn fileread(file: *mut File, addr: u64, num_bytes: i32) ->
/// Write to file `file`.
///
/// `addr` is as user virtual address.
#[no_mangle]
pub unsafe extern "C" fn filewrite(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
pub unsafe fn filewrite(file: *mut File, addr: u64, num_bytes: i32) -> i32 {
if (*file).writable == 0 {
return -1;
}

View File

@ -6,6 +6,4 @@ pub mod virtio_disk;
use uart::Uart;
pub static UARTS: [(usize, Uart); 1] = [
(10, Uart::new(0x1000_0000)),
];
pub static UARTS: [(usize, Uart); 1] = [(10, Uart::new(0x1000_0000))];

View File

@ -68,7 +68,6 @@ pub unsafe fn main() -> ! {
arch::interrupt::inithart();
io::bio::binit();
fs::iinit();
fs::file::fileinit();
hardware::virtio_disk::virtio_disk_init();
proc::process::userinit();
STARTED = true;

View File

@ -80,6 +80,7 @@ pub enum ProcessError {
/// Per-process state.
#[repr(C)]
#[derive(Clone)]
pub struct Process {
pub lock: Spinlock,

View File

@ -42,7 +42,10 @@ impl<T> Mutex<T> {
}
}
unsafe impl<T> Sync for Mutex<T> where T: Send {}
impl<T> Clone for Mutex<T> where T: Clone {
impl<T> Clone for Mutex<T>
where
T: Clone,
{
fn clone(&self) -> Self {
let value: T = self.lock_spinning().as_ref().clone();
Mutex::new(value)