wip
This commit is contained in:
parent
0ab509578f
commit
29d56f62a5
55
src/bin/thin_check.cc
Normal file
55
src/bin/thin_check.cc
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
extern crate clap;
|
||||||
|
extern crate thinp;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
|
use std::process;
|
||||||
|
use thinp::file_utils;
|
||||||
|
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let parser = App::new("thin_check")
|
||||||
|
.version(thinp::version::TOOLS_VERSION)
|
||||||
|
.about("Validates thin provisioning metadata on a device or file.")
|
||||||
|
.arg(Arg::with_name("QUIET")
|
||||||
|
.help("Suppress output messages, return only exit code.")
|
||||||
|
.short("q")
|
||||||
|
.long("quiet")
|
||||||
|
.value_name("QUIET"))
|
||||||
|
.arg(Arg::with_name("SB_ONLY")
|
||||||
|
.help("Only check the superblock.")
|
||||||
|
.long("super-block-only")
|
||||||
|
.value_name("SB_ONLY"))
|
||||||
|
.arg(Arg::with_name("ignore-non-fatal-errors")
|
||||||
|
.help("Only return a non-zero exit code if a fatal error is found.")
|
||||||
|
.long("ignore-non-fatal-errors")
|
||||||
|
.value_name("IGNORE_NON_FATAL"))
|
||||||
|
.arg(Arg::with_name("clear-needs-check-flag")
|
||||||
|
.help("Clears the 'needs_check' flag in the superblock")
|
||||||
|
.long("clear-needs-check")
|
||||||
|
.value_name("CLEAR_NEEDS_CHECK"))
|
||||||
|
.arg(Arg::with_name("OVERRIDE_MAPPING_ROOT")
|
||||||
|
.help("Specify a mapping root to use")
|
||||||
|
.long("override-mapping-root")
|
||||||
|
.value_name("OVERRIDE_MAPPING_ROOT")
|
||||||
|
.takes_value(true))
|
||||||
|
.arg(Arg::with_name("METADATA_SNAPSHOT")
|
||||||
|
.help("Check the metadata snapshot on a live pool")
|
||||||
|
.short("m")
|
||||||
|
.long("metadata-snapshot")
|
||||||
|
.value_name("METADATA_SNAPSHOT"))
|
||||||
|
|
||||||
|
let matches = parser.get_matches();
|
||||||
|
let input_file = matches.value_of("INPUT").unwrap();
|
||||||
|
let output_file = matches.value_of("OUTPUT").unwrap();
|
||||||
|
|
||||||
|
if !file_utils::file_exists(input_file) {
|
||||||
|
eprintln!("Couldn't find input file '{}'.", &input_file);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(reason) = thinp::pack::toplevel::unpack(&input_file, &output_file) {
|
||||||
|
println!("Application error: {}", reason);
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
@ -6,8 +6,8 @@ use std::fs::File;
|
|||||||
|
|
||||||
pub const BLOCK_SIZE: usize = 4096;
|
pub const BLOCK_SIZE: usize = 4096;
|
||||||
|
|
||||||
#[repr(align(4096))]
|
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
|
pub loc: u64,
|
||||||
pub data: [u8; BLOCK_SIZE as usize],
|
pub data: [u8; BLOCK_SIZE as usize],
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ impl BlockManager {
|
|||||||
|
|
||||||
fn read_block(&mut self, b: u64) -> io::Result<Block>
|
fn read_block(&mut self, b: u64) -> io::Result<Block>
|
||||||
{
|
{
|
||||||
let mut buf = Block {data: [0; BLOCK_SIZE]};
|
let mut buf = Block {loc: b, data: [0; BLOCK_SIZE]};
|
||||||
|
|
||||||
self.input.seek(io::SeekFrom::Start(b * (BLOCK_SIZE as u64)))?;
|
self.input.seek(io::SeekFrom::Start(b * (BLOCK_SIZE as u64)))?;
|
||||||
self.input.read_exact(&mut buf.data)?;
|
self.input.read_exact(&mut buf.data)?;
|
||||||
|
45
src/checksum.rs
Normal file
45
src/checksum.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
use crc32c::crc32c;
|
||||||
|
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
const BLOCK_SIZE: u64 = 4096;
|
||||||
|
const MAGIC: u64 = 0xa537a0aa6309ef77;
|
||||||
|
const SUPERBLOCK_CSUM_XOR: u32 = 160774;
|
||||||
|
const BITMAP_CSUM_XOR: u32 = 240779;
|
||||||
|
const INDEX_CSUM_XOR: u32 = 160478;
|
||||||
|
const BTREE_CSUM_XOR: u32 = 121107;
|
||||||
|
|
||||||
|
fn checksum(buf: &[u8]) -> u32 {
|
||||||
|
crc32c(&buf[4..]) ^ 0xffffffff
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub enum BT {
|
||||||
|
SUPERBLOCK,
|
||||||
|
NODE,
|
||||||
|
INDEX,
|
||||||
|
BITMAP,
|
||||||
|
UNKNOWN,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn metadata_block_type(buf: &[u8]) -> BT {
|
||||||
|
if buf.len() != BLOCK_SIZE as usize {
|
||||||
|
return BT::UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The checksum is always stored in the first u32 of the buffer.
|
||||||
|
let mut rdr = Cursor::new(buf);
|
||||||
|
let sum_on_disk = rdr.read_u32::<LittleEndian>().unwrap();
|
||||||
|
let csum = checksum(buf);
|
||||||
|
let btype = csum ^ sum_on_disk;
|
||||||
|
|
||||||
|
match btype {
|
||||||
|
SUPERBLOCK_CSUM_XOR => BT::SUPERBLOCK,
|
||||||
|
BTREE_CSUM_XOR => BT::NODE,
|
||||||
|
BITMAP_CSUM_XOR => BT::BITMAP,
|
||||||
|
INDEX_CSUM_XOR => BT::INDEX,
|
||||||
|
_ => BT::UNKNOWN,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,3 +19,5 @@ pub mod check;
|
|||||||
pub mod file_utils;
|
pub mod file_utils;
|
||||||
pub mod pack;
|
pub mod pack;
|
||||||
pub mod version;
|
pub mod version;
|
||||||
|
pub mod thin;
|
||||||
|
pub mod checksum;
|
||||||
|
@ -8,7 +8,6 @@ use std::{
|
|||||||
fs::OpenOptions,
|
fs::OpenOptions,
|
||||||
io,
|
io,
|
||||||
io::prelude::*,
|
io::prelude::*,
|
||||||
io::Cursor,
|
|
||||||
io::Write,
|
io::Write,
|
||||||
ops::DerefMut,
|
ops::DerefMut,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
@ -20,14 +19,11 @@ use std::sync::mpsc::{sync_channel, Receiver};
|
|||||||
|
|
||||||
use crate::file_utils;
|
use crate::file_utils;
|
||||||
use crate::pack::node_encode::*;
|
use crate::pack::node_encode::*;
|
||||||
|
use crate::checksum::*;
|
||||||
|
|
||||||
const BLOCK_SIZE: u64 = 4096;
|
const BLOCK_SIZE: u64 = 4096;
|
||||||
const MAGIC: u64 = 0xa537a0aa6309ef77;
|
const MAGIC: u64 = 0xa537a0aa6309ef77;
|
||||||
const PACK_VERSION: u64 = 3;
|
const PACK_VERSION: u64 = 3;
|
||||||
const SUPERBLOCK_CSUM_XOR: u32 = 160774;
|
|
||||||
const BITMAP_CSUM_XOR: u32 = 240779;
|
|
||||||
const INDEX_CSUM_XOR: u32 = 160478;
|
|
||||||
const BTREE_CSUM_XOR: u32 = 121107;
|
|
||||||
|
|
||||||
fn shuffle<T>(v: &mut Vec<T>) {
|
fn shuffle<T>(v: &mut Vec<T>) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
@ -209,39 +205,6 @@ where
|
|||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checksum(buf: &[u8]) -> u32 {
|
|
||||||
crc32c::crc32c(&buf[4..]) ^ 0xffffffff
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
|
||||||
enum BT {
|
|
||||||
SUPERBLOCK,
|
|
||||||
NODE,
|
|
||||||
INDEX,
|
|
||||||
BITMAP,
|
|
||||||
UNKNOWN,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn metadata_block_type(buf: &[u8]) -> BT {
|
|
||||||
if buf.len() != BLOCK_SIZE as usize {
|
|
||||||
return BT::UNKNOWN;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The checksum is always stored in the first u32 of the buffer.
|
|
||||||
let mut rdr = Cursor::new(buf);
|
|
||||||
let sum_on_disk = rdr.read_u32::<LittleEndian>().unwrap();
|
|
||||||
let csum = checksum(buf);
|
|
||||||
let btype = csum ^ sum_on_disk;
|
|
||||||
|
|
||||||
match btype {
|
|
||||||
SUPERBLOCK_CSUM_XOR => BT::SUPERBLOCK,
|
|
||||||
BTREE_CSUM_XOR => BT::NODE,
|
|
||||||
BITMAP_CSUM_XOR => BT::BITMAP,
|
|
||||||
INDEX_CSUM_XOR => BT::INDEX,
|
|
||||||
_ => BT::UNKNOWN,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check<T>(r: &PResult<T>) {
|
fn check<T>(r: &PResult<T>) {
|
||||||
match r {
|
match r {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
1
src/thin/mod.rs
Normal file
1
src/thin/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
mod superblock;
|
60
src/thin/superblock.rs
Normal file
60
src/thin/superblock.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use crate::block_manager::*;
|
||||||
|
use crate::checksum::*;
|
||||||
|
|
||||||
|
const SPACE_MAP_ROOT_SIZE: usize = 128;
|
||||||
|
|
||||||
|
pub struct Superblock {
|
||||||
|
block: u64,
|
||||||
|
uuid: String,
|
||||||
|
version: u32,
|
||||||
|
time: u32,
|
||||||
|
transaction_id: u64,
|
||||||
|
metadata_snap: u64,
|
||||||
|
data_sm_root: [u8; SPACE_MAP_ROOT_SIZE],
|
||||||
|
metadata_sn_root: [u8; SPACE_MAP_ROOT_SIZE],
|
||||||
|
mapping_root: u64,
|
||||||
|
details_root: u64,
|
||||||
|
data_block_size: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum CheckSeverity {
|
||||||
|
Fatal,
|
||||||
|
NonFatal,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CheckError {
|
||||||
|
fn severity(&self) -> CheckSeverity;
|
||||||
|
fn block(&self) -> u64;
|
||||||
|
fn sub_errors(&self) -> Vec<Box<dyn CheckError>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ErrorType {
|
||||||
|
BadChecksum,
|
||||||
|
BadBlockType(&'static str),
|
||||||
|
BadBlock(u64),
|
||||||
|
BadVersion(u32),
|
||||||
|
MetadataSnapOutOfBounds(u64),
|
||||||
|
MappingRootOutOfBounds(u64),
|
||||||
|
DetailsRootOutOfBounds(u64),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SuperblockError {
|
||||||
|
severity: CheckSeverity,
|
||||||
|
kind: ErrorType,
|
||||||
|
}
|
||||||
|
|
||||||
|
use SuperblockDamage::*;
|
||||||
|
|
||||||
|
//------------------------------
|
||||||
|
|
||||||
|
pub fn check_type(b: &Block) -> Result<(), Box<dyn CheckError>> {
|
||||||
|
match metadata_block_type(&b.data[0..]) {
|
||||||
|
SUPERBLOCK => Ok(()),
|
||||||
|
NODE => Err(Box::new(BadBlockType("BTree Node"))),
|
||||||
|
INDEX => Err(Box::new(BadBlockType("Space Map Index"))),
|
||||||
|
BITMAP => Err(Box::new(BadBlockType("Space Map Bitmap"))),
|
||||||
|
UNKNOWN => Err(Box::new(BadChecksum)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------
|
Loading…
x
Reference in New Issue
Block a user