Skip to content

Commit 6d22c60

Browse files
committed
[thin_dump] Prompt for repair on input errors
thin_dump should display the repairing hint if there's any error in the input metadata rather than the output process. A context object thus is added to the returned error for callers to determine the error type. Note that a broken pipe error (EPIPE) is treated as an output error since the Rust std library ignores SIGPIPE by default [1][2]. [1] rust-lang/rust#13158 [2] rust-lang/rust#62569
1 parent 069f87a commit 6d22c60

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

src/commands/thin_dump.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::sync::Arc;
88

99
use crate::commands::utils::*;
1010
use crate::commands::Command;
11+
use crate::dump_utils::OutputError;
1112
use crate::report::*;
1213
use crate::thin::dump::{dump, ThinDumpOptions};
1314
use crate::thin::metadata_repair::SuperblockOverrides;
@@ -128,9 +129,17 @@ impl<'a> Command<'a> for ThinDumpCommand {
128129
},
129130
};
130131

131-
dump(opts).map_err(|reason| {
132-
report.fatal(&format!("{}", reason));
133-
std::io::Error::from_raw_os_error(libc::EPERM)
134-
})
132+
if let Err(e) = dump(opts) {
133+
if !e.is::<OutputError>() {
134+
report.fatal(&format!("{:?}", e));
135+
report.fatal(
136+
"metadata contains errors (run thin_check for details).\n\
137+
perhaps you wanted to run with --repair ?",
138+
);
139+
}
140+
return Err(std::io::Error::from_raw_os_error(libc::EPERM));
141+
}
142+
143+
Ok(())
135144
}
136145
}

src/dump_utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//------------------------------------------
2+
3+
// A wrapper for callers to determine the error type
4+
#[derive(Debug)]
5+
pub struct OutputError;
6+
7+
impl std::fmt::Display for OutputError {
8+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9+
write!(f, "output error")
10+
}
11+
}
12+
13+
//------------------------------------------

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod cache;
1919
pub mod checksum;
2020
pub mod commands;
2121
pub mod copier;
22+
pub mod dump_utils;
2223
pub mod era;
2324
pub mod file_utils;
2425
pub mod grid_layout;

src/thin/dump.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use anyhow::{anyhow, Result};
1+
use anyhow::{anyhow, Context, Result};
22
use std::fs::File;
33
use std::io::BufWriter;
44
use std::io::Write;
55
use std::path::Path;
66
use std::sync::{Arc, Mutex};
77

88
use crate::checksum;
9+
use crate::dump_utils::*;
910
use crate::io_engine::{AsyncIoEngine, Block, IoEngine, SyncIoEngine};
1011
use crate::pdata::btree::{self, *};
1112
use crate::pdata::btree_walker::*;
@@ -153,56 +154,49 @@ pub struct ThinDumpOptions<'a> {
153154
pub overrides: SuperblockOverrides,
154155
}
155156

156-
struct Context {
157+
struct ThinDumpContext {
157158
report: Arc<Report>,
158159
engine: Arc<dyn IoEngine + Send + Sync>,
159160
}
160161

161-
fn mk_context(opts: &ThinDumpOptions) -> Result<Context> {
162+
fn mk_context(opts: &ThinDumpOptions) -> Result<ThinDumpContext> {
162163
let engine: Arc<dyn IoEngine + Send + Sync> = if opts.async_io {
163164
Arc::new(AsyncIoEngine::new(opts.input, MAX_CONCURRENT_IO, false)?)
164165
} else {
165166
let nr_threads = std::cmp::max(8, num_cpus::get() * 2);
166167
Arc::new(SyncIoEngine::new(opts.input, nr_threads, false)?)
167168
};
168169

169-
Ok(Context {
170+
Ok(ThinDumpContext {
170171
report: opts.report.clone(),
171172
engine,
172173
})
173174
}
174175

175176
//------------------------------------------
176177

177-
fn emit_leaf(v: &mut MappingVisitor, b: &Block) -> Result<()> {
178+
fn emit_leaf(v: &MappingVisitor, b: &Block) -> Result<()> {
178179
use Node::*;
179180
let path = Vec::new();
180181
let kr = KeyRange::new();
181182

182183
let bt = checksum::metadata_block_type(b.get_data());
183184
if bt != checksum::BT::NODE {
184-
return Err(anyhow!(format!(
185-
"checksum failed for node {}, {:?}",
186-
b.loc, bt
187-
)));
185+
return Err(anyhow!("checksum failed for node {}, {:?}", b.loc, bt));
188186
}
189187

190188
let node = unpack_node::<BlockTime>(&path, b.get_data(), true, true)?;
191189

192190
match node {
193-
Internal { .. } => {
194-
return Err(anyhow!("not a leaf"));
195-
}
191+
Internal { .. } => Err(anyhow!("block {} is not a leaf", b.loc)),
196192
Leaf {
197193
header,
198194
keys,
199195
values,
200-
} => {
201-
v.visit(&path, &kr, &header, &keys, &values)?;
202-
}
196+
} => v
197+
.visit(&path, &kr, &header, &keys, &values)
198+
.context(OutputError),
203199
}
204-
205-
Ok(())
206200
}
207201

208202
fn read_for<T>(engine: Arc<dyn IoEngine>, blocks: &[u64], mut t: T) -> Result<()>
@@ -214,7 +208,8 @@ where
214208
.read_many(cs)
215209
.map_err(|_e| anyhow!("read_many failed"))?
216210
{
217-
t(b.map_err(|_e| anyhow!("read of individual block failed"))?)?;
211+
let blk = b.map_err(|_e| anyhow!("read of individual block failed"))?;
212+
t(blk)?;
218213
}
219214
}
220215

@@ -226,15 +221,14 @@ fn emit_leaves(
226221
out: &mut dyn MetadataVisitor,
227222
leaves: &[u64],
228223
) -> Result<()> {
229-
let mut v = MappingVisitor::new(out);
224+
let v = MappingVisitor::new(out);
230225
let proc = |b| {
231-
emit_leaf(&mut v, &b)?;
226+
emit_leaf(&v, &b)?;
232227
Ok(())
233228
};
234229

235230
read_for(engine, leaves, proc)?;
236-
v.end_walk()
237-
.map_err(|e| anyhow!("failed to emit leaves: {}", e))
231+
v.end_walk().context(OutputError)
238232
}
239233

240234
fn emit_entries(
@@ -255,7 +249,7 @@ fn emit_entries(
255249
leaves.clear();
256250
}
257251
let str = format!("{}", id);
258-
out.ref_shared(&str)?;
252+
out.ref_shared(&str).context(OutputError)?;
259253
}
260254
}
261255
}
@@ -284,12 +278,13 @@ pub fn dump_metadata(
284278
nr_data_blocks: data_root.nr_blocks,
285279
metadata_snap: None,
286280
};
287-
out.superblock_b(&out_sb)?;
281+
out.superblock_b(&out_sb).context(OutputError)?;
288282

289283
for d in &md.defs {
290-
out.def_shared_b(&format!("{}", d.def_id))?;
284+
out.def_shared_b(&format!("{}", d.def_id))
285+
.context(OutputError)?;
291286
emit_entries(engine.clone(), out, &d.map.entries)?;
292-
out.def_shared_e()?;
287+
out.def_shared_e().context(OutputError)?;
293288
}
294289

295290
for dev in &md.devs {
@@ -300,12 +295,12 @@ pub fn dump_metadata(
300295
creation_time: dev.detail.creation_time,
301296
snap_time: dev.detail.snapshotted_time,
302297
};
303-
out.device_b(&device)?;
298+
out.device_b(&device).context(OutputError)?;
304299
emit_entries(engine.clone(), out, &dev.map.entries)?;
305-
out.device_e()?;
300+
out.device_e().context(OutputError)?;
306301
}
307-
out.superblock_e()?;
308-
out.eof()?;
302+
out.superblock_e().context(OutputError)?;
303+
out.eof().context(OutputError)?;
309304

310305
Ok(())
311306
}
@@ -327,11 +322,13 @@ pub fn dump(opts: ThinDumpOptions) -> Result<()> {
327322
read_superblock(ctx.engine.as_ref(), SUPERBLOCK_LOCATION)
328323
.and_then(|sb| sb.overrides(&opts.overrides))?
329324
};
325+
330326
let md = build_metadata(ctx.engine.clone(), &sb)?;
331327
let md = optimise_metadata(md)?;
332328

333329
let writer: Box<dyn Write> = if opts.output.is_some() {
334-
Box::new(BufWriter::new(File::create(opts.output.unwrap())?))
330+
let f = File::create(opts.output.unwrap()).context(OutputError)?;
331+
Box::new(BufWriter::new(f))
335332
} else {
336333
Box::new(BufWriter::new(std::io::stdout()))
337334
};

0 commit comments

Comments
 (0)