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-08 21:12:32 +05:30
|
|
|
use std::collections::BTreeMap;
|
2020-07-27 20:23:42 +05:30
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-07-29 21:08:52 +05:30
|
|
|
use threadpool::ThreadPool;
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-08-08 21:12:32 +05:30
|
|
|
use crate::checksum;
|
2020-08-10 17:26:41 +05:30
|
|
|
use crate::io_engine::{AsyncIoEngine, Block, IoEngine, SyncIoEngine};
|
2020-08-10 19:15:35 +05:30
|
|
|
use crate::pdata::btree::{btree_to_map, unpack, BTreeWalker, Node, NodeVisitor, Unpack};
|
2020-08-06 12:21:48 +05:30
|
|
|
use crate::pdata::space_map::*;
|
2020-07-28 15:21:48 +05:30
|
|
|
use crate::thin::superblock::*;
|
2020-07-29 15:42:03 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-04 16:41:36 +05:30
|
|
|
struct TopLevelVisitor<'a> {
|
2020-08-08 19:28:13 +05:30
|
|
|
roots: &'a mut BTreeMap<u32, u64>,
|
2020-08-04 16:41:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-08-08 14:24:16 +05:30
|
|
|
struct BottomLevelVisitor {
|
2020-08-08 17:59:30 +05:30
|
|
|
data_sm: Arc<Mutex<dyn SpaceMap + Send>>,
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
2020-07-28 15:21:48 +05:30
|
|
|
|
2020-08-03 20:52:08 +05:30
|
|
|
impl NodeVisitor<BlockTime> for BottomLevelVisitor {
|
2020-08-08 14:24:16 +05:30
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<BlockTime>) -> Result<()> {
|
|
|
|
// FIXME: do other checks
|
|
|
|
|
2020-08-08 21:12:32 +05:30
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys: _k,
|
|
|
|
values,
|
|
|
|
} = node
|
|
|
|
{
|
2020-08-10 17:26:41 +05:30
|
|
|
if values.len() == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut data_sm = self.data_sm.lock().unwrap();
|
2020-08-08 17:59:30 +05:30
|
|
|
|
2020-08-10 17:26:41 +05:30
|
|
|
let mut start = values[0].block;
|
|
|
|
let mut len = 1;
|
|
|
|
|
|
|
|
for n in 1..values.len() {
|
|
|
|
let block = values[n].block;
|
|
|
|
if block == start + len {
|
|
|
|
len += 1;
|
|
|
|
} else {
|
|
|
|
data_sm.inc(start, len)?;
|
|
|
|
start = block;
|
|
|
|
len = 1;
|
|
|
|
}
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
2020-08-10 17:26:41 +05:30
|
|
|
|
|
|
|
data_sm.inc(start, len)?;
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
|
|
|
|
2020-08-03 20:52:08 +05:30
|
|
|
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,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-08 21:12:32 +05:30
|
|
|
struct OverflowChecker<'a> {
|
|
|
|
data_sm: &'a dyn SpaceMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> OverflowChecker<'a> {
|
|
|
|
fn new(data_sm: &'a dyn SpaceMap) -> OverflowChecker<'a> {
|
|
|
|
OverflowChecker { data_sm }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NodeVisitor<u32> for OverflowChecker<'a> {
|
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, node: &Node<u32>) -> Result<()> {
|
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
} = node
|
|
|
|
{
|
|
|
|
for n in 0..keys.len() {
|
|
|
|
let k = keys[n];
|
|
|
|
let v = values[n];
|
|
|
|
let expected = self.data_sm.get(k)?;
|
|
|
|
if expected != v {
|
|
|
|
return Err(anyhow!("Bad reference count for data block {}. Expected {}, but space map contains {}.",
|
|
|
|
k, expected, v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
const MAX_CONCURRENT_IO: u32 = 1024;
|
|
|
|
|
2020-08-10 15:54:50 +05:30
|
|
|
pub struct ThinCheckOptions<'a> {
|
|
|
|
pub dev: &'a Path,
|
|
|
|
pub async_io: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check(opts: &ThinCheckOptions) -> Result<()> {
|
|
|
|
let engine: Arc<dyn IoEngine + Send + Sync>;
|
|
|
|
|
|
|
|
let nr_threads;
|
|
|
|
if opts.async_io {
|
|
|
|
nr_threads = std::cmp::min(4, num_cpus::get());
|
|
|
|
engine = Arc::new(AsyncIoEngine::new(opts.dev, MAX_CONCURRENT_IO)?);
|
|
|
|
} else {
|
|
|
|
eprintln!("falling back to synchronous io");
|
|
|
|
nr_threads = num_cpus::get() * 2;
|
|
|
|
engine = Arc::new(SyncIoEngine::new(opts.dev, nr_threads)?);
|
|
|
|
}
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-08-10 17:26:41 +05:30
|
|
|
// superblock
|
2020-08-03 20:52:08 +05:30
|
|
|
let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?;
|
2020-08-04 16:41:36 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// device details
|
2020-08-10 19:15:35 +05:30
|
|
|
let devs = btree_to_map::<DeviceDetail>(engine.clone(), false, sb.details_root)?;
|
|
|
|
let nr_devs = devs.len();
|
|
|
|
println!("found {} devices", nr_devs);
|
2020-08-04 16:41:36 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// mapping top level
|
2020-08-10 19:15:35 +05:30
|
|
|
let roots = btree_to_map::<u64>(engine.clone(), false, sb.mapping_root)?;
|
2020-07-27 20:23:42 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
// mapping bottom level
|
2020-08-08 21:12:32 +05:30
|
|
|
let data_sm;
|
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-10 15:14:47 +05:30
|
|
|
let nr_workers = nr_threads;
|
2020-08-04 16:41:36 +05:30
|
|
|
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,
|
|
|
|
)));
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
let root = unpack::<SMRoot>(&sb.data_sm_root[0..])?;
|
2020-08-08 21:12:32 +05:30
|
|
|
data_sm = core_sm(root.nr_blocks, nr_devs as u32);
|
2020-08-08 14:24:16 +05:30
|
|
|
|
2020-08-04 16:41:36 +05:30
|
|
|
for (thin_id, root) in roots {
|
|
|
|
let mut w = BTreeWalker::new_with_seen(engine.clone(), seen.clone(), false);
|
2020-08-08 14:24:16 +05:30
|
|
|
let data_sm = data_sm.clone();
|
2020-08-04 16:41:36 +05:30
|
|
|
pool.execute(move || {
|
2020-08-08 21:12:32 +05:30
|
|
|
let mut v = BottomLevelVisitor { data_sm };
|
2020-08-10 17:26:41 +05:30
|
|
|
|
|
|
|
// FIXME: return error
|
|
|
|
match w.walk(&mut v, root) {
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("walk failed {:?}", e);
|
|
|
|
std::process::abort();
|
|
|
|
}
|
|
|
|
Ok(result) => {
|
|
|
|
eprintln!("checked thin_dev {} -> {:?}", thin_id, result);
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 16:41:36 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.join();
|
|
|
|
}
|
2020-08-06 12:21:48 +05:30
|
|
|
|
|
|
|
// data space map
|
|
|
|
{
|
2020-08-10 19:15:35 +05:30
|
|
|
let data_sm = data_sm.lock().unwrap();
|
2020-08-06 12:21:48 +05:30
|
|
|
let root = unpack::<SMRoot>(&sb.data_sm_root[0..])?;
|
2020-08-10 17:26:41 +05:30
|
|
|
let nr_data_blocks = root.nr_blocks;
|
2020-08-06 12:21:48 +05:30
|
|
|
eprintln!("data root: {:?}", root);
|
|
|
|
|
|
|
|
// overflow btree
|
|
|
|
{
|
2020-08-08 21:12:32 +05:30
|
|
|
let mut v = OverflowChecker::new(&*data_sm);
|
2020-08-06 12:21:48 +05:30
|
|
|
let mut w = BTreeWalker::new(engine.clone(), false);
|
|
|
|
w.walk(&mut v, root.ref_count_root)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bitmaps
|
2020-08-10 19:26:39 +05:30
|
|
|
let entries = btree_to_map::<IndexEntry>(engine.clone(), false, root.bitmap_root)?;
|
|
|
|
eprintln!("{} index entries", entries.len());
|
2020-08-06 12:21:48 +05:30
|
|
|
|
2020-08-08 21:12:32 +05:30
|
|
|
let mut blocks = Vec::new();
|
2020-08-10 19:26:39 +05:30
|
|
|
for (_k, i) in &entries {
|
2020-08-08 21:12:32 +05:30
|
|
|
blocks.push(Block::new(i.blocknr));
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.read_many(&mut blocks)?;
|
|
|
|
|
2020-08-10 19:15:35 +05:30
|
|
|
let mut leaks = 0;
|
2020-08-10 17:26:41 +05:30
|
|
|
let mut fail = false;
|
2020-08-08 21:12:32 +05:30
|
|
|
let mut blocknr = 0;
|
2020-08-10 19:26:39 +05:30
|
|
|
for n in 0..entries.len() {
|
2020-08-08 21:12:32 +05:30
|
|
|
let b = &blocks[n];
|
2020-08-06 12:21:48 +05:30
|
|
|
if checksum::metadata_block_type(&b.get_data()) != checksum::BT::BITMAP {
|
2020-08-08 21:12:32 +05:30
|
|
|
return Err(anyhow!(
|
|
|
|
"Index entry points to block ({}) that isn't a bitmap",
|
|
|
|
b.loc
|
|
|
|
));
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
let bitmap = unpack::<Bitmap>(b.get_data())?;
|
2020-08-08 21:12:32 +05:30
|
|
|
for e in bitmap.entries {
|
2020-08-10 17:26:41 +05:30
|
|
|
if blocknr >= nr_data_blocks {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-08 21:12:32 +05:30
|
|
|
match e {
|
|
|
|
BitmapEntry::Small(actual) => {
|
|
|
|
let expected = data_sm.get(blocknr)?;
|
2020-08-10 19:15:35 +05:30
|
|
|
if actual == 1 && expected == 0 {
|
|
|
|
eprintln!("Data block {} leaked.", blocknr);
|
|
|
|
leaks += 1;
|
|
|
|
} else if actual != expected as u8 {
|
2020-08-10 17:26:41 +05:30
|
|
|
eprintln!("Bad reference count for data block {}. Expected {}, but space map contains {}.",
|
|
|
|
blocknr, expected, actual);
|
|
|
|
fail = true;
|
2020-08-08 21:12:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
BitmapEntry::Overflow => {
|
|
|
|
let expected = data_sm.get(blocknr)?;
|
|
|
|
if expected < 3 {
|
2020-08-10 17:26:41 +05:30
|
|
|
eprintln!("Bad reference count for data block {}. Expected {}, but space map says it's >= 3.",
|
|
|
|
blocknr, expected);
|
|
|
|
fail = true;
|
2020-08-08 21:12:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blocknr += 1;
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
|
|
|
}
|
2020-08-10 17:26:41 +05:30
|
|
|
|
2020-08-10 19:15:35 +05:30
|
|
|
if leaks > 0 {
|
|
|
|
eprintln!(
|
|
|
|
"{} data blocks have leaked. Use --auto-repair to fix.",
|
|
|
|
leaks
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:26:41 +05:30
|
|
|
if fail {
|
|
|
|
return Err(anyhow!("Inconsistent data space map"));
|
|
|
|
}
|
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
|
|
|
|
|
|
|
//------------------------------------------
|