From b8afa7cfb02b32278e268924e189170496f81c1b Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Thu, 23 Nov 2023 02:11:16 -0800 Subject: Add some callback (not finished) --- src/memory/cached_block.rs | 16 +++++++++------- src/memory/cached_inode.rs | 10 +++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src/memory') diff --git a/src/memory/cached_block.rs b/src/memory/cached_block.rs index 9e266ef..eb59a4b 100644 --- a/src/memory/cached_block.rs +++ b/src/memory/cached_block.rs @@ -2,6 +2,7 @@ use crate::block_device::{BlockDevice, BLOCK_SIZE}; use crate::disk::block::Block; use crate::AyaFS; use and_then_some::BoolExt; +use log::debug; use lru::LruCache; use std::num::NonZeroUsize; use std::sync::Arc; @@ -46,11 +47,9 @@ impl BlockCache { pub(crate) fn load_block(&mut self, index: usize) -> bool { if self.cache.contains(&index) == false { - let block = T::default(); - let buffer = unsafe { - std::slice::from_raw_parts_mut(&block as *const T as *mut u8, BLOCK_SIZE) - }; - self.device.read(index, buffer); + let mut buffer = [0u8; BLOCK_SIZE]; + self.device.read(index, &mut buffer); + let block: T = unsafe { std::mem::transmute_copy(&buffer) }; let cached_block = CachedBlock { block, index, @@ -77,8 +76,11 @@ impl BlockCache { /// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载. pub(crate) fn get_block_mut(&mut self, index: usize) -> Option<&mut CachedBlock> { - self.load_block(index) - .and_then(|| self.cache.get_mut(&index).map(convert_mut::)) + debug!("Blockcache get block mut"); + self.load_block(index).and_then(|| { + debug!("block loaded"); + self.cache.get_mut(&index).map(convert_mut::) + }) } /// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载. diff --git a/src/memory/cached_inode.rs b/src/memory/cached_inode.rs index b81bd2e..dd5e5c3 100644 --- a/src/memory/cached_inode.rs +++ b/src/memory/cached_inode.rs @@ -1,7 +1,8 @@ -use crate::disk::block::{Block, InodeBlock}; +use crate::disk::block::InodeBlock; use crate::disk::inode::{Inode, InodeMode, INODE_SIZE}; -use crate::AyaFS; +use crate::{utils, AyaFS}; use and_then_some::BoolExt; +use log::debug; impl AyaFS { pub(crate) fn create_inode( @@ -12,20 +13,23 @@ impl AyaFS { gid: u32, flags: u32, ) -> Option { + debug!("create inode"); self.inode_bitmap.allocate().map(|inode_index| { + debug!("creating inode"); self.get_inode_mut(inode_index).map(|inode| { *inode = Inode::make_inode( permissions, mode, uid, gid, - Self::time_now(), + utils::time_now(), flags, 0, 0, 0, ); }); + debug!("inode created"); inode_index }) } -- cgit v1.2.3-70-g09d2