Skip to content

Commit

Permalink
feat: implement ext4fs driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Yttehs-HDX committed Dec 25, 2024
1 parent fdfdd3a commit 7305bff
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ simple_range = { path = "../lib/simple_range" }
virtio-drivers = "0.8.0"
fatfs = { version = "0.4", git = "https://github.com/rafalh/rust-fatfs", default-features = false, features = ["lfn", "alloc"] }
spin = "0.9.8"
ext4_rs = "1.1.0"

[profile.release]
debug = true
Expand Down
30 changes: 30 additions & 0 deletions kernel/src/fs/ext4/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub use virtio::*;

use alloc::sync::Arc;
use core::ptr::NonNull;
use ext4_rs::Ext4;
use virtio_drivers::{device::blk::VirtIOBlk, transport::mmio::{MmioTransport, VirtIOHeader}};
use crate::{board::VIRT_IO, drivers::VirtIOHal};

mod virtio;

// region Ext4Fs begin
pub struct Ext4Fs {
pub inner: Ext4,
}

impl Ext4Fs {
pub fn new(device_id: usize) -> Self {
let addr = VIRT_IO + device_id * 0x1000;
let header = NonNull::new(addr as *mut VirtIOHeader).unwrap();
let transport =
unsafe { MmioTransport::new(header).expect("Failed to create mmio transport") };
let blk = VirtIOBlk::<VirtIOHal, MmioTransport>::new(transport)
.expect("Failed to create VirtIOBlk");

let device = VirtIODisk::new(blk);
let ext4 = Ext4::open(Arc::new(device));
Self { inner: ext4 }
}
}
// region Ext4Fs end
45 changes: 45 additions & 0 deletions kernel/src/fs/ext4/virtio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use core::cell::RefMut;
use crate::{drivers::VirtIOHal, sync::UPSafeCell};
use alloc::vec;
use ext4_rs::BlockDevice;
use virtio_drivers::{device::blk::VirtIOBlk, transport::mmio::MmioTransport};

const BLOCK_SIZE: usize = 512;

// region VirtIODisk begin
pub struct VirtIODisk {
inner: UPSafeCell<VirtIODiskInner>,
}

impl VirtIODisk {
pub fn new(virt_io_blk: VirtIODiskInner) -> Self {
VirtIODisk {
inner: unsafe { UPSafeCell::new(virt_io_blk) },
}
}

fn inner_mut(&self) -> RefMut<VirtIODiskInner> {
self.inner.exclusive_access()
}
}

impl BlockDevice for VirtIODisk {
fn read_offset(&self, offset: usize) -> alloc::vec::Vec<u8> {
let mut inner = self.inner_mut();
let mut buf = vec![0; BLOCK_SIZE];
inner
.read_blocks(offset, &mut buf)
.expect("Error occurred when reading VirtIOBlk");
buf
}

fn write_offset(&self, offset: usize, data: &[u8]) {
let mut inner = self.inner_mut();
inner
.write_blocks(offset, &data)
.expect("Error occurred when writing VirtIOBlk");
}
}
// region VirtIODisk end

type VirtIODiskInner = VirtIOBlk<VirtIOHal, MmioTransport>;
1 change: 1 addition & 0 deletions kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use stdio::*;
use alloc::sync::Arc;
use lazy_static::lazy_static;

pub mod ext4;
pub mod fat;
mod interface;
mod path;
Expand Down

0 comments on commit 7305bff

Please sign in to comment.