From f0df17af9e06e3754c67441ceebd4a24f76c6bd7 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 10 Aug 2020 08:59:02 +0100 Subject: [PATCH] [thin_check (rust)] Get SyncIoEngine working again. --- src/io_engine.rs | 24 +++++++++++++----------- src/pdata/btree.rs | 6 +++--- src/thin/check.rs | 5 +++-- src/thin/superblock.rs | 2 +- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/io_engine.rs b/src/io_engine.rs index fe68bb7..5b0a313 100644 --- a/src/io_engine.rs +++ b/src/io_engine.rs @@ -4,7 +4,7 @@ use io_uring::IoUring; use std::alloc::{alloc, dealloc, Layout}; use std::fs::File; use std::fs::OpenOptions; -use std::io; +use std::io::{self, Seek, Read}; use std::os::unix::fs::OpenOptionsExt; use std::os::unix::io::{AsRawFd, RawFd}; use std::path::Path; @@ -60,10 +60,9 @@ fn get_nr_blocks(path: &Path) -> io::Result { //------------------------------------------ -/* pub struct SyncIoEngine { nr_blocks: u64, - input: File, + input: Mutex, } impl SyncIoEngine { @@ -76,7 +75,7 @@ impl SyncIoEngine { Ok(SyncIoEngine { nr_blocks: get_nr_blocks(path)?, - input, + input: Mutex::new(input), }) } } @@ -86,23 +85,26 @@ impl IoEngine for SyncIoEngine { self.nr_blocks } - fn read(&mut self, b: &mut Block) -> Result<()> { - self.input - .seek(io::SeekFrom::Start(b.loc * BLOCK_SIZE as u64))?; - self.input.read_exact(&mut b.get_data())?; + + fn read(&self, b: &mut Block) -> Result<()> { + let mut input = self.input.lock().unwrap(); + input.seek(io::SeekFrom::Start(b.loc * BLOCK_SIZE as u64))?; + input.read_exact(&mut b.get_data())?; Ok(()) } - fn read_many(&mut self, blocks: &mut Vec) -> Result<()> { + fn read_many(&self, blocks: &mut Vec) -> Result<()> { + let mut input = self.input.lock().unwrap(); for b in blocks { - self.read(b); + input.seek(io::SeekFrom::Start(b.loc * BLOCK_SIZE as u64))?; + input.read_exact(&mut b.get_data())?; } Ok(()) } } -*/ + //------------------------------------------ pub struct AsyncIoEngine_ { diff --git a/src/pdata/btree.rs b/src/pdata/btree.rs index 9a61aff..50bf8f8 100644 --- a/src/pdata/btree.rs +++ b/src/pdata/btree.rs @@ -189,13 +189,13 @@ pub trait NodeVisitor { #[derive(Clone)] pub struct BTreeWalker { - pub engine: Arc, + pub engine: Arc, pub seen: Arc>, ignore_non_fatal: bool, } impl BTreeWalker { - pub fn new(engine: Arc, ignore_non_fatal: bool) -> BTreeWalker { + pub fn new(engine: Arc, ignore_non_fatal: bool) -> BTreeWalker { let nr_blocks = engine.get_nr_blocks() as usize; let r: BTreeWalker = BTreeWalker { engine, @@ -206,7 +206,7 @@ impl BTreeWalker { } pub fn new_with_seen( - engine: Arc, + engine: Arc, seen: Arc>, ignore_non_fatal: bool, ) -> BTreeWalker { diff --git a/src/thin/check.rs b/src/thin/check.rs index 3ef76c5..a93eaaf 100644 --- a/src/thin/check.rs +++ b/src/thin/check.rs @@ -7,7 +7,7 @@ use std::sync::{Arc, Mutex}; use std::time::Instant; use threadpool::ThreadPool; -use crate::io_engine::{AsyncIoEngine, Block, IoEngine}; +use crate::io_engine::{AsyncIoEngine, SyncIoEngine, Block, IoEngine}; use crate::checksum; use crate::pdata::btree::{unpack, BTreeWalker, Node, NodeVisitor, Unpack}; use crate::pdata::space_map::*; @@ -265,7 +265,8 @@ impl<'a> NodeVisitor for OverflowChecker<'a> { const MAX_CONCURRENT_IO: u32 = 1024; pub fn check(dev: &Path) -> Result<()> { - let engine = Arc::new(AsyncIoEngine::new(dev, MAX_CONCURRENT_IO)?); + //let engine = Arc::new(AsyncIoEngine::new(dev, MAX_CONCURRENT_IO)?); + let engine: Arc = Arc::new(SyncIoEngine::new(dev)?); let now = Instant::now(); let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?; diff --git a/src/thin/superblock.rs b/src/thin/superblock.rs index bd3e8ba..3bb2637 100644 --- a/src/thin/superblock.rs +++ b/src/thin/superblock.rs @@ -85,7 +85,7 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> { )) } -pub fn read_superblock(engine: &E, loc: u64) -> Result { +pub fn read_superblock(engine: &dyn IoEngine, loc: u64) -> Result { let mut b = Block::new(loc); engine.read(&mut b)?;