Skip to content

Commit 0af18b5

Browse files
ext2: rename
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 2fce3e4 commit 0af18b5

File tree

9 files changed

+229
-54
lines changed

9 files changed

+229
-54
lines changed

src/aero_kernel/src/drivers/block/ide/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ impl BlockDeviceInterface for IdeDrive {
7979
unimplemented!()
8080
}
8181

82+
fn write_dma(
83+
&self,
84+
_sector: usize,
85+
_start: crate::mem::paging::PhysAddr,
86+
_size: usize,
87+
) -> Option<usize> {
88+
todo!()
89+
}
90+
8291
fn write_block(&self, _sector: usize, _buf: &[u8]) -> Option<usize> {
8392
unimplemented!()
8493
}

src/aero_kernel/src/drivers/block/nvme/command.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ bitflags::bitflags! {
3939
#[repr(u8)]
4040
#[derive(Default, Copy, Clone)]
4141
pub enum CommandOpcode {
42+
Write = 0x1,
4243
Read = 0x2,
4344

4445
#[default]

src/aero_kernel/src/drivers/block/nvme/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ struct Namespace<'a> {
230230
}
231231

232232
impl<'a> Namespace<'a> {
233-
fn read(&self, sector: usize, start: PhysAddr, size_bytes: usize) {
233+
fn rw_command(&self, opcode: CommandOpcode, sector: usize, start: PhysAddr, size_bytes: usize) {
234234
assert!(size_bytes != 0);
235235

236236
let blocks = size_bytes.ceil_div(self.block_size);
237237
let mut read_cmd = ReadWriteCommand::default();
238238

239-
read_cmd.opcode = CommandOpcode::Read as u8;
239+
read_cmd.opcode = opcode as u8;
240240
read_cmd.nsid = self.nsid;
241241
read_cmd.start_lba = sector as u64;
242242
read_cmd.length = (blocks - 1) as u16;
@@ -463,13 +463,23 @@ impl<'a> Controller<'a> {
463463

464464
impl<'a> BlockDeviceInterface for Controller<'a> {
465465
fn read_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize> {
466-
self.namespaces.lock()[0].read(sector, start, size);
466+
self.namespaces.lock()[0].rw_command(CommandOpcode::Read, sector, start, size);
467+
Some(size)
468+
}
469+
470+
fn write_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize> {
471+
self.namespaces.lock()[0].rw_command(CommandOpcode::Write, sector, start, size);
467472
Some(size)
468473
}
469474

470475
fn read_block(&self, sector: usize, dest: &mut [MaybeUninit<u8>]) -> Option<usize> {
471476
let buffer = Dma::<u8>::new_uninit_slice(dest.len());
472-
self.namespaces.lock()[0].read(sector, buffer.addr(), dest.len());
477+
self.namespaces.lock()[0].rw_command(
478+
CommandOpcode::Read,
479+
sector,
480+
buffer.addr(),
481+
dest.len(),
482+
);
473483

474484
// SAFETY: The buffer is initialized above.
475485
dest.copy_from_slice(&buffer);

src/aero_kernel/src/fs/block/mod.rs

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mod gpt;
2222
use gpt::Gpt;
2323

2424
use core::mem::MaybeUninit;
25+
use core::ops::{Deref, DerefMut};
26+
use core::sync::atomic::{AtomicBool, Ordering};
2527

2628
use alloc::collections::BTreeMap;
2729
use alloc::sync::{Arc, Weak};
@@ -45,6 +47,7 @@ struct CachedPage {
4547
device: Weak<dyn CachedAccess>,
4648
offset: usize,
4749
page: PhysFrame,
50+
dirty: AtomicBool,
4851
}
4952

5053
impl CachedPage {
@@ -55,6 +58,7 @@ impl CachedPage {
5558
page: FRAME_ALLOCATOR
5659
.allocate_frame()
5760
.expect("page_cache: out of memory"),
61+
dirty: AtomicBool::new(false),
5862
}
5963
}
6064

@@ -77,6 +81,39 @@ impl CachedPage {
7781
fn make_key(device: Weak<dyn CachedAccess>, offset: usize) -> PageCacheKey {
7882
(device.as_ptr() as *const u8 as usize, offset)
7983
}
84+
85+
/// Returns whether the page has been marked dirty.
86+
fn is_dirty(&self) -> bool {
87+
self.dirty.load(Ordering::SeqCst)
88+
}
89+
90+
fn mark_dirty(&self) {
91+
self.dirty.store(true, Ordering::SeqCst);
92+
}
93+
94+
fn device(&self) -> Arc<dyn CachedAccess> {
95+
self.device.upgrade().unwrap()
96+
}
97+
98+
fn sync(&self) {
99+
if !self.is_dirty() {
100+
return;
101+
}
102+
103+
// Commit the changes made to the cache to the disk.
104+
let disk = self.device();
105+
106+
let offset_bytes = self.offset * Size4KiB::SIZE as usize;
107+
let sector = offset_bytes / disk.block_size();
108+
109+
disk.write_dma(sector, self.data_addr(), Size4KiB::SIZE as usize);
110+
}
111+
}
112+
113+
impl Drop for CachedPage {
114+
fn drop(&mut self) {
115+
self.sync()
116+
}
80117
}
81118

82119
impl Cacheable<PageCacheKey> for CachedPage {
@@ -96,9 +133,8 @@ impl Cache<PageCacheKey, CachedPage> {
96133
/// ## Arguments
97134
///
98135
/// * `device` - The device to get the page from.
99-
///
100-
/// * `offset` - The offset in bytes to the data. This will be rounded down to
101-
/// the nearest page boundary.
136+
/// * `offset` - The offset in bytes to the data. This will be rounded down to the nearest page
137+
/// boundary.
102138
pub fn get_page(&self, device: Weak<dyn CachedAccess>, offset: usize) -> PageCacheItem {
103139
let cache_offset = offset / Size4KiB::SIZE as usize;
104140
let cache_key = CachedPage::make_key(device.clone(), cache_offset);
@@ -121,10 +157,48 @@ impl Cache<PageCacheKey, CachedPage> {
121157
}
122158
}
123159

160+
pub struct DirtyRef<T: Sized> {
161+
cache: PageCacheItem,
162+
ptr: *mut T,
163+
}
164+
165+
impl<T> DirtyRef<T> {
166+
pub fn new(device: Weak<dyn CachedAccess>, offset: usize) -> Self {
167+
let cache = PAGE_CACHE.get_page(device, offset);
168+
169+
let ptr_offset = offset % Size4KiB::SIZE as usize;
170+
let ptr = &cache.data_mut()[ptr_offset..ptr_offset + core::mem::size_of::<T>()];
171+
172+
Self {
173+
ptr: ptr.as_ptr() as *mut T,
174+
cache,
175+
}
176+
}
177+
}
178+
179+
impl<T> Deref for DirtyRef<T> {
180+
type Target = T;
181+
182+
fn deref(&self) -> &Self::Target {
183+
unsafe { &*self.ptr }
184+
}
185+
}
186+
187+
impl<T> DerefMut for DirtyRef<T> {
188+
fn deref_mut(&mut self) -> &mut Self::Target {
189+
self.cache.mark_dirty();
190+
unsafe { &mut *self.ptr }
191+
}
192+
}
193+
194+
unsafe impl<T> Sync for DirtyRef<T> {}
195+
unsafe impl<T> Send for DirtyRef<T> {}
196+
124197
pub trait BlockDeviceInterface: Send + Sync {
125198
fn block_size(&self) -> usize;
126199

127200
fn read_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize>;
201+
fn write_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize>;
128202

129203
fn read_block(&self, sector: usize, dest: &mut [MaybeUninit<u8>]) -> Option<usize>;
130204
fn write_block(&self, sector: usize, buf: &[u8]) -> Option<usize>;
@@ -228,6 +302,10 @@ impl BlockDeviceInterface for BlockDevice {
228302
self.dev.read_dma(sector, start, size)
229303
}
230304

305+
fn write_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize> {
306+
self.dev.write_dma(sector, start, size)
307+
}
308+
231309
fn read_block(&self, sector: usize, dest: &mut [MaybeUninit<u8>]) -> Option<usize> {
232310
self.dev.read_block(sector, dest)
233311
}
@@ -292,6 +370,14 @@ impl BlockDeviceInterface for PartitionBlockDevice {
292370
self.device.write_block(self.offset + sector, buf)
293371
}
294372

373+
fn write_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize> {
374+
if sector >= self.size {
375+
return None;
376+
}
377+
378+
self.write_dma(self.offset + sector, start, size)
379+
}
380+
295381
fn read_dma(&self, sector: usize, start: PhysAddr, size: usize) -> Option<usize> {
296382
if sector >= self.size {
297383
return None;

src/aero_kernel/src/fs/cache.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ impl<K: CacheKey, V: Cacheable<K>> Cache<K, V> {
249249
}
250250
}
251251

252+
pub fn rehash<F>(&self, item: CacheArc<CacheItem<K, V>>, update: F)
253+
where
254+
F: FnOnce(),
255+
{
256+
let mut index = self.index.lock();
257+
let key = item.cache_key();
258+
259+
index.used.remove(&key).expect("cache: item is not used");
260+
update();
261+
262+
let new_key = item.cache_key();
263+
index.used.insert(new_key, Arc::downgrade(&item));
264+
}
265+
252266
pub fn log(&self) {
253267
let index = self.index.lock();
254268

src/aero_kernel/src/fs/ext2/disk.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,6 @@ impl DirEntry {
166166

167167
name_bytes.copy_from_slice(name.as_bytes());
168168
}
169-
170-
pub fn as_bytes(&self) -> &[u8] {
171-
unsafe {
172-
core::slice::from_raw_parts(self as *const Self as *const u8, self.entry_size as usize)
173-
}
174-
}
175169
}
176170

177171
#[repr(u8)]

0 commit comments

Comments
 (0)