From 76ac602c3d79bb39c133c81a38425a77bc0b8b1f Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Sat, 25 Nov 2023 02:13:22 -0800 Subject: Some FUSE callbacks, some POSIX interface implementation --- src/memory/cached_block.rs | 85 +++++----------------------------------------- 1 file changed, 9 insertions(+), 76 deletions(-) (limited to 'src/memory/cached_block.rs') diff --git a/src/memory/cached_block.rs b/src/memory/cached_block.rs index eb59a4b..77cdf61 100644 --- a/src/memory/cached_block.rs +++ b/src/memory/cached_block.rs @@ -1,5 +1,6 @@ use crate::block_device::{BlockDevice, BLOCK_SIZE}; use crate::disk::block::Block; +use crate::disk::inode::Inode; use crate::AyaFS; use and_then_some::BoolExt; use log::debug; @@ -70,17 +71,18 @@ impl BlockCache { /// 从 LRU cache 里获取一个 block 的引用, 如果没有在 cache 中会加载. pub(crate) fn get_block(&mut self, index: usize) -> Option<&CachedBlock> { - self.load_block(index) - .and_then(|| self.cache.get(&index).map(convert::)) + if !self.cache.contains(&index) { + self.load_block(index); + } + self.cache.get(&index).map(convert::) } /// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载. pub(crate) fn get_block_mut(&mut self, index: usize) -> Option<&mut CachedBlock> { - debug!("Blockcache get block mut"); - self.load_block(index).and_then(|| { - debug!("block loaded"); - self.cache.get_mut(&index).map(convert_mut::) - }) + if !self.cache.contains(&index) { + self.load_block(index); + } + self.cache.get_mut(&index).map(convert_mut::) } /// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载. @@ -145,73 +147,4 @@ impl AyaFS { pub(crate) fn update_block(&mut self, block: CachedBlock) -> bool { self.cached_blocks.update_block(block) } - - // pub(crate) fn update_block(&mut self, block: CachedBlock) -> bool { - // if self.cached_blocks.contains(&block.index) { - // let data_block = convert::(&block).clone(); - // self.cached_blocks.push(block.index, data_block); - // true - // } else { - // false - // } - // } - // - // /// 从 LRU cache 里获取一个 block 的引用, 如果没有在 cache 中会加载. - // pub(crate) fn get_block(&mut self, index: usize) -> Option<&CachedBlock> { - // self.load_block(index) - // .and_then(|| self.cached_blocks.get(&index).map(convert::)) - // } - // - // /// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载. - // pub(crate) fn get_block_mut(&mut self, index: usize) -> Option<&mut CachedBlock> { - // self.load_block(index).and_then(|| { - // self.cached_blocks - // .get_mut(&index) - // .map(convert_mut::) - // }) - // } - // - // /// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载. - // pub(crate) fn peek_block(&self, index: usize) -> Option<&CachedBlock> { - // self.cached_blocks.peek(&index).map(convert::) - // } - // - // /// 从 LRU cache 中读取一个 block 的可变引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载. - // pub(crate) fn peek_block_mut(&mut self, index: usize) -> Option<&mut CachedBlock> { - // self.cached_blocks.peek_mut(&index).map(convert_mut::) - // } - // - // pub(crate) fn load_block(&mut self, index: usize) -> bool { - // // 先看这个 block 是不是 valid, 不 valid 直接返回 false. - // if !self.data_bitmap.query(index) { - // self.cached_blocks.pop(&index); - // // deallocate 时只更新 bitmap 没有动 cache, lazy 地驱逐 cache 中的无效 entry. - // return false; - // } - // - // // 接下来这个 block 一定 valid. 如果 cache 里没有这个 block 就把它 load 到 cache 里 - // if self.cached_blocks.contains(&index) == false { - // let block = DataBlock::default(); - // let buffer = unsafe { - // std::slice::from_raw_parts_mut(&block as *const DataBlock as *mut u8, BLOCK_SIZE) - // }; - // self.device.read(index, buffer); - // let cached_block = CachedBlock { - // block, - // index, - // dirty: false, - // }; - // if let Some((old_index, old_block)) = self.cached_blocks.push(index, cached_block) { - // assert_ne!(old_index, index); // 只有 block 不在 cache 里的时候才会插入 - // if old_block.dirty { - // let old_block_ptr = &old_block.block as *const DataBlock as *mut u8; - // let old_block_buffer = - // unsafe { std::slice::from_raw_parts(old_block_ptr, BLOCK_SIZE) }; - // self.device.write(old_index, old_block_buffer); - // } - // } - // } - // - // true - // } } -- cgit v1.2.3-70-g09d2