From 1eac97eea4ec0bcef0be061a2cba93a584355283 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Thu, 30 Nov 2023 02:15:06 -0800 Subject: Add real disk i/o, add mkfs.aya (not yet implemented) --- ayafs/src/block_device/memory_disk.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ayafs/src/block_device/memory_disk.rs (limited to 'ayafs/src/block_device/memory_disk.rs') diff --git a/ayafs/src/block_device/memory_disk.rs b/ayafs/src/block_device/memory_disk.rs new file mode 100644 index 0000000..0639d3e --- /dev/null +++ b/ayafs/src/block_device/memory_disk.rs @@ -0,0 +1,33 @@ +use crate::block_device::{BlockDevice, BLOCK_SIZE}; +use std::cell::RefCell; + +#[repr(C)] +pub struct MemoryDisk { + /// Emulating a block device with a segment of RAM, + /// which is 64MiB == 4KiB per block * 16384 blocks + pub arena: RefCell>, +} + +impl MemoryDisk { + pub fn new(block_number: usize) -> Self { + Self { + arena: RefCell::new(vec![0u8; BLOCK_SIZE * block_number]), + } + } +} + +impl BlockDevice for MemoryDisk { + fn read(&self, block_id: usize, buffer: &mut [u8]) { + let block_front = block_id * BLOCK_SIZE; + let block_back = block_front + BLOCK_SIZE; + let arena = self.arena.borrow(); + buffer.copy_from_slice(&arena[block_front..block_back]); + } + + fn write(&self, block_id: usize, buffer: &[u8]) { + let block_front = block_id * BLOCK_SIZE; + let block_back = block_front + BLOCK_SIZE; + let mut arena = self.arena.borrow_mut(); + arena[block_front..block_back].copy_from_slice(buffer); + } +} -- cgit v1.2.3-70-g09d2