Skip to content

Commit d65b66c

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 69ff4d4 commit d65b66c

File tree

4 files changed

+61
-38
lines changed

4 files changed

+61
-38
lines changed

src/commands/thin_dump.rs

+13-4
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

+13
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

+1
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

+34-34
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,58 +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-
if let Err(_e) = v.visit(&path, &kr, &header, &keys, &values) {
202-
return Err(anyhow!("couldn't emit leaf node"));
203-
}
204-
}
196+
} => v
197+
.visit(&path, &kr, &header, &keys, &values)
198+
.context(OutputError),
205199
}
206-
207-
Ok(())
208200
}
209201

210202
fn read_for<T>(engine: Arc<dyn IoEngine>, blocks: &[u64], mut t: T) -> Result<()>
@@ -216,22 +208,27 @@ where
216208
.read_many(cs)
217209
.map_err(|_e| anyhow!("read_many failed"))?
218210
{
219-
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)?;
220213
}
221214
}
222215

223216
Ok(())
224217
}
225218

226-
fn emit_leaves(engine: Arc<dyn IoEngine>, out: &mut dyn MetadataVisitor, ls: &[u64]) -> Result<()> {
227-
let mut v = MappingVisitor::new(out);
219+
fn emit_leaves(
220+
engine: Arc<dyn IoEngine>,
221+
out: &mut dyn MetadataVisitor,
222+
leaves: &[u64],
223+
) -> Result<()> {
224+
let v = MappingVisitor::new(out);
228225
let proc = |b| {
229-
emit_leaf(&mut v, &b)?;
226+
emit_leaf(&v, &b)?;
230227
Ok(())
231228
};
232229

233-
read_for(engine, ls, proc)?;
234-
v.end_walk().map_err(|_| anyhow!("failed to emit leaves"))
230+
read_for(engine, leaves, proc)?;
231+
v.end_walk().context(OutputError)
235232
}
236233

237234
fn emit_entries(
@@ -252,7 +249,7 @@ fn emit_entries(
252249
leaves.clear();
253250
}
254251
let str = format!("{}", id);
255-
out.ref_shared(&str)?;
252+
out.ref_shared(&str).context(OutputError)?;
256253
}
257254
}
258255
}
@@ -281,12 +278,13 @@ pub fn dump_metadata(
281278
nr_data_blocks: data_root.nr_blocks,
282279
metadata_snap: None,
283280
};
284-
out.superblock_b(&out_sb)?;
281+
out.superblock_b(&out_sb).context(OutputError)?;
285282

286283
for d in &md.defs {
287-
out.def_shared_b(&format!("{}", d.def_id))?;
284+
out.def_shared_b(&format!("{}", d.def_id))
285+
.context(OutputError)?;
288286
emit_entries(engine.clone(), out, &d.map.entries)?;
289-
out.def_shared_e()?;
287+
out.def_shared_e().context(OutputError)?;
290288
}
291289

292290
for dev in &md.devs {
@@ -297,12 +295,12 @@ pub fn dump_metadata(
297295
creation_time: dev.detail.creation_time,
298296
snap_time: dev.detail.snapshotted_time,
299297
};
300-
out.device_b(&device)?;
298+
out.device_b(&device).context(OutputError)?;
301299
emit_entries(engine.clone(), out, &dev.map.entries)?;
302-
out.device_e()?;
300+
out.device_e().context(OutputError)?;
303301
}
304-
out.superblock_e()?;
305-
out.eof()?;
302+
out.superblock_e().context(OutputError)?;
303+
out.eof().context(OutputError)?;
306304

307305
Ok(())
308306
}
@@ -324,11 +322,13 @@ pub fn dump(opts: ThinDumpOptions) -> Result<()> {
324322
read_superblock(ctx.engine.as_ref(), SUPERBLOCK_LOCATION)
325323
.and_then(|sb| sb.overrides(&opts.overrides))?
326324
};
325+
327326
let md = build_metadata(ctx.engine.clone(), &sb)?;
328327
let md = optimise_metadata(md)?;
329328

330329
let writer: Box<dyn Write> = if opts.output.is_some() {
331-
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))
332332
} else {
333333
Box::new(BufWriter::new(std::io::stdout()))
334334
};

0 commit comments

Comments
 (0)