2020-07-28 15:21:48 +05:30
|
|
|
use anyhow::{anyhow, Result};
|
2020-08-04 16:41:36 +05:30
|
|
|
use fixedbitset::FixedBitSet;
|
2020-07-30 14:42:51 +05:30
|
|
|
use nom::{number::complete::*, IResult};
|
2020-08-03 20:52:08 +05:30
|
|
|
use std::collections::HashMap;
|
2020-07-27 20:23:42 +05:30
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-07-30 14:42:51 +05:30
|
|
|
use std::time::Instant;
|
2020-07-29 21:08:52 +05:30
|
|
|
use threadpool::ThreadPool;
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-30 14:42:51 +05:30
|
|
|
use crate::block_manager::{AsyncIoEngine, Block, IoEngine};
|
2020-08-06 12:21:48 +05:30
|
|
|
use crate::pdata::btree::{BTreeWalker, Node, NodeVisitor, Unpack, unpack};
|
|
|
|
use crate::pdata::space_map::*;
|
2020-07-28 15:21:48 +05:30
|
|
|
use crate::thin::superblock::*;
|
2020-08-06 12:21:48 +05:30
|
|
|
use crate::checksum;
|
2020-07-29 15:42:03 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-04 16:41:36 +05:30
|
|
|
struct TopLevelVisitor<'a> {
|
|
|
|
roots: &'a mut HashMap<u32, u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NodeVisitor<u64> for TopLevelVisitor<'a> {
|
2020-08-05 12:31:02 +05:30
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<u64>) -> Result<()> {
|
2020-08-04 16:41:36 +05:30
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
} = node
|
|
|
|
{
|
|
|
|
for n in 0..keys.len() {
|
|
|
|
let k = keys[n];
|
|
|
|
let root = values[n];
|
|
|
|
self.roots.insert(k as u32, root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-07-30 14:42:51 +05:30
|
|
|
#[allow(dead_code)]
|
2020-07-28 15:21:48 +05:30
|
|
|
struct BlockTime {
|
|
|
|
block: u64,
|
|
|
|
time: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:31:02 +05:30
|
|
|
impl Unpack for BlockTime {
|
2020-08-03 19:34:59 +05:30
|
|
|
fn disk_size() -> u32 {
|
|
|
|
8
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
fn unpack(i: &[u8]) -> IResult<&[u8], BlockTime> {
|
|
|
|
let (i, n) = le_u64(i)?;
|
|
|
|
let block = n >> 24;
|
|
|
|
let time = n & ((1 << 24) - 1);
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
BlockTime {
|
|
|
|
block,
|
|
|
|
time: time as u32,
|
|
|
|
},
|
|
|
|
))
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
2020-07-28 15:21:48 +05:30
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
struct BottomLevelVisitor {}
|
2020-07-28 15:21:48 +05:30
|
|
|
|
2020-08-03 20:52:08 +05:30
|
|
|
impl NodeVisitor<BlockTime> for BottomLevelVisitor {
|
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, _node: &Node<BlockTime>) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct DeviceDetail {
|
|
|
|
mapped_blocks: u64,
|
|
|
|
transaction_id: u64,
|
|
|
|
creation_time: u32,
|
|
|
|
snapshotted_time: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:31:02 +05:30
|
|
|
impl Unpack for DeviceDetail {
|
2020-08-03 20:52:08 +05:30
|
|
|
fn disk_size() -> u32 {
|
|
|
|
24
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unpack(i: &[u8]) -> IResult<&[u8], DeviceDetail> {
|
|
|
|
let (i, mapped_blocks) = le_u64(i)?;
|
|
|
|
let (i, transaction_id) = le_u64(i)?;
|
|
|
|
let (i, creation_time) = le_u32(i)?;
|
|
|
|
let (i, snapshotted_time) = le_u32(i)?;
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
DeviceDetail {
|
|
|
|
mapped_blocks,
|
|
|
|
transaction_id,
|
|
|
|
creation_time,
|
|
|
|
snapshotted_time,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DeviceVisitor {
|
|
|
|
devs: HashMap<u32, DeviceDetail>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceVisitor {
|
|
|
|
pub fn new() -> DeviceVisitor {
|
|
|
|
DeviceVisitor {
|
|
|
|
devs: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeVisitor<DeviceDetail> for DeviceVisitor {
|
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<DeviceDetail>) -> Result<()> {
|
2020-08-04 16:41:36 +05:30
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
} = node
|
|
|
|
{
|
|
|
|
for n in 0..keys.len() {
|
|
|
|
let k = keys[n] as u32;
|
|
|
|
let v = values[n].clone();
|
2020-08-06 12:21:48 +05:30
|
|
|
self.devs.insert(k, v);
|
2020-08-04 16:41:36 +05:30
|
|
|
}
|
2020-08-03 20:52:08 +05:30
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
Ok(())
|
2020-07-28 15:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
struct IndexVisitor {
|
|
|
|
entries: Vec<IndexEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeVisitor<IndexEntry> for IndexVisitor {
|
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<IndexEntry>) -> Result<()> {
|
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
2020-08-07 20:11:21 +05:30
|
|
|
keys: _k,
|
2020-08-06 12:21:48 +05:30
|
|
|
values,
|
|
|
|
} = node {
|
2020-08-07 20:11:21 +05:30
|
|
|
for v in values {
|
2020-08-06 12:21:48 +05:30
|
|
|
// FIXME: check keys are in incremental order
|
2020-08-07 20:11:21 +05:30
|
|
|
let v = v.clone();
|
2020-08-06 12:21:48 +05:30
|
|
|
self.entries.push(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
// FIXME: move to btree
|
|
|
|
struct ValueCollector<V> {
|
|
|
|
values: Vec<(u64, V)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V> ValueCollector<V> {
|
|
|
|
fn new() -> ValueCollector<V> {
|
|
|
|
ValueCollector {
|
|
|
|
values: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: Unpack + Clone> NodeVisitor<V> for ValueCollector<V> {
|
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<V>) -> Result<()> {
|
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
} = node {
|
|
|
|
for n in 0..keys.len() {
|
|
|
|
let k = keys[n];
|
|
|
|
let v = values[n].clone();
|
|
|
|
self.values.push((k, v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
pub fn check(dev: &Path) -> Result<()> {
|
2020-08-05 12:31:02 +05:30
|
|
|
let engine = Arc::new(AsyncIoEngine::new(dev, 256)?);
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
let now = Instant::now();
|
2020-08-03 20:52:08 +05:30
|
|
|
let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?;
|
2020-07-28 15:21:48 +05:30
|
|
|
eprintln!("{:?}", sb);
|
2020-08-04 16:41:36 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// device details
|
2020-08-03 20:52:08 +05:30
|
|
|
{
|
|
|
|
let mut visitor = DeviceVisitor::new();
|
|
|
|
let mut w = BTreeWalker::new(engine.clone(), false);
|
|
|
|
w.walk(&mut visitor, sb.details_root)?;
|
|
|
|
println!("found {} devices", visitor.devs.len());
|
|
|
|
}
|
2020-08-04 16:41:36 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// mapping top level
|
2020-08-04 16:41:36 +05:30
|
|
|
let mut roots = HashMap::new();
|
2020-08-03 20:52:08 +05:30
|
|
|
{
|
2020-08-04 16:41:36 +05:30
|
|
|
let mut visitor = TopLevelVisitor { roots: &mut roots };
|
2020-08-03 20:52:08 +05:30
|
|
|
let mut w = BTreeWalker::new(engine.clone(), false);
|
|
|
|
let _result = w.walk(&mut visitor, sb.mapping_root)?;
|
|
|
|
println!("read mapping tree in {} ms", now.elapsed().as_millis());
|
|
|
|
}
|
2020-07-27 20:23:42 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// mapping bottom level
|
2020-08-04 16:41:36 +05:30
|
|
|
{
|
2020-08-06 12:21:48 +05:30
|
|
|
// FIXME: with a thread pool we need to return errors another way.
|
2020-08-04 16:41:36 +05:30
|
|
|
let nr_workers = 4;
|
|
|
|
let pool = ThreadPool::new(nr_workers);
|
2020-08-05 12:31:02 +05:30
|
|
|
let seen = Arc::new(Mutex::new(FixedBitSet::with_capacity(
|
2020-08-04 16:41:36 +05:30
|
|
|
engine.get_nr_blocks() as usize,
|
|
|
|
)));
|
|
|
|
|
|
|
|
for (thin_id, root) in roots {
|
|
|
|
let mut w = BTreeWalker::new_with_seen(engine.clone(), seen.clone(), false);
|
|
|
|
pool.execute(move || {
|
|
|
|
let mut v = BottomLevelVisitor {};
|
|
|
|
let result = w.walk(&mut v, root).expect("walk failed"); // FIXME: return error
|
|
|
|
eprintln!("checked thin_dev {} -> {:?}", thin_id, result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.join();
|
|
|
|
}
|
2020-08-06 12:21:48 +05:30
|
|
|
|
|
|
|
// data space map
|
|
|
|
{
|
|
|
|
let root = unpack::<SMRoot>(&sb.data_sm_root[0..])?;
|
|
|
|
eprintln!("data root: {:?}", root);
|
|
|
|
|
|
|
|
// overflow btree
|
|
|
|
let mut overflow: HashMap<u64, u32> = HashMap::new();
|
|
|
|
{
|
|
|
|
let mut v: ValueCollector<u32> = ValueCollector::new();
|
|
|
|
let mut w = BTreeWalker::new(engine.clone(), false);
|
|
|
|
w.walk(&mut v, root.ref_count_root)?;
|
|
|
|
|
|
|
|
for (k, v) in v.values {
|
|
|
|
overflow.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eprintln!("{} overflow entries", overflow.len());
|
|
|
|
|
|
|
|
// Bitmaps
|
|
|
|
let mut v = IndexVisitor {entries: Vec::new()};
|
|
|
|
let mut w = BTreeWalker::new(engine.clone(), false);
|
|
|
|
let _result = w.walk(&mut v, root.bitmap_root);
|
|
|
|
eprintln!("{} index entries", v.entries.len());
|
|
|
|
|
|
|
|
for i in v.entries {
|
|
|
|
let mut b = Block::new(i.blocknr);
|
|
|
|
engine.read(&mut b)?;
|
|
|
|
|
|
|
|
if checksum::metadata_block_type(&b.get_data()) != checksum::BT::BITMAP {
|
|
|
|
return Err(anyhow!("Index entry points to block ({}) that isn't a bitmap", b.loc));
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:00:00 +05:30
|
|
|
let _bitmap = unpack::<Bitmap>(b.get_data())?;
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
2020-08-04 16:41:36 +05:30
|
|
|
|
2020-06-09 13:45:00 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-29 21:08:52 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|