[cache (rust)] Prepare for cache_repair

- Adapt function interfaces for repairing purpose
- Finalize the metadata in the Restorer
- Make the Restorer public
This commit is contained in:
Ming-Hung Tsai 2021-07-19 10:44:33 +08:00
parent 85581cdf5a
commit 4b9766846e
3 changed files with 55 additions and 72 deletions

View File

@ -29,6 +29,12 @@ fn main() {
.value_name("OVERRIDE_MAPPING_ROOT") .value_name("OVERRIDE_MAPPING_ROOT")
.takes_value(true), .takes_value(true),
) )
.arg(
Arg::with_name("QUIET")
.help("Suppress output messages, return only exit code.")
.short("q")
.long("quiet"),
)
// options // options
.arg( .arg(
Arg::with_name("INPUT") Arg::with_name("INPUT")

23
src/cache/dump.rs vendored
View File

@ -218,15 +218,12 @@ fn mk_context(opts: &CacheDumpOptions) -> anyhow::Result<Context> {
Ok(Context { engine }) Ok(Context { engine })
} }
fn dump_metadata( pub fn dump_metadata(
ctx: &Context, engine: Arc<dyn IoEngine + Send + Sync>,
w: &mut dyn Write, out: &mut dyn MetadataVisitor,
sb: &Superblock, sb: &Superblock,
_repair: bool, _repair: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let engine = &ctx.engine;
let mut out = xml::XmlWriter::new(w);
let xml_sb = ir::Superblock { let xml_sb = ir::Superblock {
uuid: "".to_string(), uuid: "".to_string(),
block_size: sb.data_block_size, block_size: sb.data_block_size,
@ -240,7 +237,7 @@ fn dump_metadata(
let valid_mappings = match sb.version { let valid_mappings = match sb.version {
1 => { 1 => {
let w = ArrayWalker::new(engine.clone(), false); let w = ArrayWalker::new(engine.clone(), false);
let mut emitter = format1::MappingEmitter::new(sb.cache_blocks as usize, &mut out); let mut emitter = format1::MappingEmitter::new(sb.cache_blocks as usize, out);
w.walk(&mut emitter, sb.mapping_root)?; w.walk(&mut emitter, sb.mapping_root)?;
emitter.get_valid() emitter.get_valid()
} }
@ -263,7 +260,7 @@ fn dump_metadata(
let w = ArrayWalker::new(engine.clone(), false); let w = ArrayWalker::new(engine.clone(), false);
let mut emitter = let mut emitter =
format2::MappingEmitter::new(sb.cache_blocks as usize, dirty_bits, &mut out); format2::MappingEmitter::new(sb.cache_blocks as usize, dirty_bits, out);
w.walk(&mut emitter, sb.mapping_root)?; w.walk(&mut emitter, sb.mapping_root)?;
emitter.get_valid() emitter.get_valid()
} }
@ -276,7 +273,7 @@ fn dump_metadata(
out.hints_b()?; out.hints_b()?;
{ {
let w = ArrayWalker::new(engine.clone(), false); let w = ArrayWalker::new(engine.clone(), false);
let mut emitter = HintEmitter::new(&mut out, valid_mappings); let mut emitter = HintEmitter::new(out, valid_mappings);
w.walk(&mut emitter, sb.hint_root)?; w.walk(&mut emitter, sb.hint_root)?;
} }
out.hints_e()?; out.hints_e()?;
@ -289,17 +286,17 @@ fn dump_metadata(
pub fn dump(opts: CacheDumpOptions) -> anyhow::Result<()> { pub fn dump(opts: CacheDumpOptions) -> anyhow::Result<()> {
let ctx = mk_context(&opts)?; let ctx = mk_context(&opts)?;
let engine = &ctx.engine; let sb = read_superblock(ctx.engine.as_ref(), SUPERBLOCK_LOCATION)?;
let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?;
let mut writer: Box<dyn Write>; let writer: Box<dyn Write>;
if opts.output.is_some() { if opts.output.is_some() {
writer = Box::new(BufWriter::new(File::create(opts.output.unwrap())?)); writer = Box::new(BufWriter::new(File::create(opts.output.unwrap())?));
} else { } else {
writer = Box::new(BufWriter::new(std::io::stdout())); writer = Box::new(BufWriter::new(std::io::stdout()));
} }
let mut out = xml::XmlWriter::new(writer);
dump_metadata(&ctx, &mut writer, &sb, opts.repair) dump_metadata(ctx.engine.clone(), &mut out, &sb, opts.repair)
} }
//------------------------------------------ //------------------------------------------

98
src/cache/restore.rs vendored
View File

@ -1,4 +1,4 @@
use anyhow::{anyhow, Result}; use anyhow::Result;
use std::convert::TryInto; use std::convert::TryInto;
use std::fs::OpenOptions; use std::fs::OpenOptions;
@ -56,15 +56,7 @@ fn mk_context(opts: &CacheRestoreOptions) -> anyhow::Result<Context> {
//------------------------------------------ //------------------------------------------
struct RestoreResult { pub struct Restorer<'a> {
sb: ir::Superblock,
mapping_root: u64,
dirty_root: Option<u64>,
hint_root: u64,
discard_root: u64,
}
struct Restorer<'a> {
write_batcher: &'a mut WriteBatcher, write_batcher: &'a mut WriteBatcher,
sb: Option<ir::Superblock>, sb: Option<ir::Superblock>,
mapping_builder: Option<ArrayBuilder<Mapping>>, mapping_builder: Option<ArrayBuilder<Mapping>>,
@ -78,7 +70,7 @@ struct Restorer<'a> {
} }
impl<'a> Restorer<'a> { impl<'a> Restorer<'a> {
fn new(w: &'a mut WriteBatcher) -> Restorer<'a> { pub fn new(w: &'a mut WriteBatcher) -> Restorer<'a> {
Restorer { Restorer {
write_batcher: w, write_batcher: w,
sb: None, sb: None,
@ -93,22 +85,42 @@ impl<'a> Restorer<'a> {
} }
} }
fn get_result(self) -> Result<RestoreResult> { fn finalize(&mut self) -> Result<()> {
self.write_batcher.flush()?; // build metadata space map
let metadata_sm_root = build_metadata_sm(self.write_batcher)?;
if self.sb.is_none() || self.discard_root.is_none() { let sb = self.sb.as_ref().unwrap();
return Err(anyhow!("No superblock found in xml file")); let mapping_root = self.mapping_root.as_ref().unwrap();
} let hint_root = self.hint_root.as_ref().unwrap();
if self.mapping_root.is_none() || self.hint_root.is_none() { let discard_root = self.discard_root.as_ref().unwrap();
return Err(anyhow!("No mappings or hints sections in xml file")); let sb = Superblock {
} flags: SuperblockFlags {
Ok(RestoreResult { clean_shutdown: true,
sb: self.sb.unwrap(), needs_check: false,
mapping_root: self.mapping_root.unwrap(), },
dirty_root: self.dirty_root, block: SUPERBLOCK_LOCATION,
hint_root: self.hint_root.unwrap(), version: 2,
discard_root: self.discard_root.unwrap(), policy_name: sb.policy.as_bytes().to_vec(),
}) policy_version: vec![2, 0, 0],
policy_hint_size: sb.hint_width,
metadata_sm_root,
mapping_root: *mapping_root,
dirty_root: self.dirty_root, // dirty_root is optional
hint_root: *hint_root,
discard_root: *discard_root,
discard_block_size: 0,
discard_nr_blocks: 0,
data_block_size: sb.block_size,
cache_blocks: sb.nr_cache_blocks,
compat_flags: 0,
compat_ro_flags: 0,
incompat_flags: 0,
read_hits: 0,
read_misses: 9,
write_hits: 0,
write_misses: 0,
};
write_superblock(self.write_batcher.engine.as_ref(), SUPERBLOCK_LOCATION, &sb)
} }
} }
@ -127,6 +139,7 @@ impl<'a> MetadataVisitor for Restorer<'a> {
} }
fn superblock_e(&mut self) -> Result<Visit> { fn superblock_e(&mut self) -> Result<Visit> {
self.finalize()?;
Ok(Visit::Continue) Ok(Visit::Continue)
} }
@ -252,39 +265,6 @@ pub fn restore(opts: CacheRestoreOptions) -> Result<()> {
// build cache mappings // build cache mappings
let mut restorer = Restorer::new(&mut w); let mut restorer = Restorer::new(&mut w);
xml::read(input, &mut restorer)?; xml::read(input, &mut restorer)?;
let result = restorer.get_result()?;
// build metadata space map
let metadata_sm_root = build_metadata_sm(&mut w)?;
let sb = Superblock {
flags: SuperblockFlags {
clean_shutdown: true,
needs_check: false,
},
block: SUPERBLOCK_LOCATION,
version: 2,
policy_name: result.sb.policy.as_bytes().to_vec(),
policy_version: vec![2, 0, 0],
policy_hint_size: result.sb.hint_width,
metadata_sm_root,
mapping_root: result.mapping_root,
dirty_root: result.dirty_root,
hint_root: result.hint_root,
discard_root: result.discard_root,
discard_block_size: 0,
discard_nr_blocks: 0,
data_block_size: result.sb.block_size,
cache_blocks: result.sb.nr_cache_blocks,
compat_flags: 0,
compat_ro_flags: 0,
incompat_flags: 0,
read_hits: 0,
read_misses: 9,
write_hits: 0,
write_misses: 0,
};
write_superblock(ctx.engine.as_ref(), SUPERBLOCK_LOCATION, &sb)?;
Ok(()) Ok(())
} }