Merge branch '2020-06-23-thin-remap'
This commit is contained in:
commit
67622f3c76
54
Cargo.lock
generated
54
Cargo.lock
generated
@ -21,6 +21,11 @@ dependencies = [
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.4.12"
|
||||
@ -100,6 +105,11 @@ dependencies = [
|
||||
"regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.14"
|
||||
@ -240,6 +250,14 @@ dependencies = [
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quick-xml"
|
||||
version = "0.18.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quickcheck"
|
||||
version = "0.9.2"
|
||||
@ -306,6 +324,11 @@ dependencies = [
|
||||
"rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.3.9"
|
||||
@ -322,6 +345,14 @@ name = "regex-syntax"
|
||||
version = "0.6.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "remove_dir_all"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
@ -368,6 +399,19 @@ dependencies = [
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"remove_dir_all 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
@ -380,9 +424,11 @@ dependencies = [
|
||||
name = "thinp"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"crc32c 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fixedbitset 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"flate2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"nix 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -390,9 +436,11 @@ dependencies = [
|
||||
"num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quick-xml 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quickcheck 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quickcheck_macros 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -456,6 +504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2e7343e7fc9de883d1b0341e0b13970f764c14101234857d2ddafa1cb1cac2"
|
||||
"checksum aho-corasick 0.7.10 (registry+https://github.com/rust-lang/crates.io-index)" = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
|
||||
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
||||
"checksum anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f"
|
||||
"checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
|
||||
"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
@ -467,6 +516,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum crc32c 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "77ba37ef26c12988c1cee882d522d65e1d5d2ad8c3864665b88ee92767ed84c5"
|
||||
"checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1"
|
||||
"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||
"checksum fixedbitset 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2fc4fcacf5cd3681968f6524ea159383132937739c6c40dabab9e37ed515911b"
|
||||
"checksum flate2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2cfff41391129e0a856d6d822600b8d71179d46879e310417eb9c762eb178b42"
|
||||
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
"checksum hermit-abi 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
|
||||
@ -484,6 +534,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
|
||||
"checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
|
||||
"checksum proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa"
|
||||
"checksum quick-xml 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3cc440ee4802a86e357165021e3e255a9143724da31db1e2ea540214c96a0f82"
|
||||
"checksum quickcheck 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f"
|
||||
"checksum quickcheck_macros 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "608c156fd8e97febc07dc9c2e2c80bf74cfc6ef26893eae3daf8bc2bc94a4b7f"
|
||||
"checksum quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea"
|
||||
@ -491,8 +542,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||
"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||
"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||
"checksum regex 1.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
|
||||
"checksum regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)" = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
|
||||
"checksum remove_dir_all 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
|
||||
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
@ -500,6 +553,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3"
|
||||
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
"checksum syn 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)" = "93a56fabc59dce20fe48b6c832cc249c713e7ed88fa28b0ee0a3bfcaae5fe4e2"
|
||||
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
||||
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||
"checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||
"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
|
||||
|
@ -6,15 +6,19 @@ edition = "2018"
|
||||
license = "GPL3"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
byteorder = "1.3"
|
||||
clap = "2.33"
|
||||
crc32c = "0.4"
|
||||
flate2 = "1.0"
|
||||
fixedbitset = "0.3"
|
||||
libc = "0.2.71"
|
||||
quick-xml = "0.18"
|
||||
nix = "0.17"
|
||||
nom = "5.1"
|
||||
num_cpus = "1.13"
|
||||
rand = "0.7"
|
||||
tempfile = "3.1"
|
||||
num-traits = "0.2"
|
||||
num-derive = "0.3"
|
||||
|
||||
|
@ -2,6 +2,7 @@ extern crate clap;
|
||||
extern crate thinp;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
use thinp::file_utils;
|
||||
|
||||
@ -23,11 +24,11 @@ fn main() {
|
||||
.takes_value(true));
|
||||
|
||||
let matches = parser.get_matches();
|
||||
let input_file = matches.value_of("INPUT").unwrap();
|
||||
let output_file = matches.value_of("OUTPUT").unwrap();
|
||||
let input_file = Path::new(matches.value_of("INPUT").unwrap());
|
||||
let output_file = Path::new(matches.value_of("OUTPUT").unwrap());
|
||||
|
||||
if !file_utils::file_exists(input_file) {
|
||||
eprintln!("Couldn't find input file '{}'.", &input_file);
|
||||
if !file_utils::file_exists(&input_file) {
|
||||
eprintln!("Couldn't find input file '{}'.", &input_file.display());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ extern crate clap;
|
||||
extern crate thinp;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
use thinp::file_utils;
|
||||
|
||||
@ -11,25 +12,29 @@ fn main() {
|
||||
let parser = App::new("thin_metadata_unpack")
|
||||
.version(thinp::version::TOOLS_VERSION)
|
||||
.about("Unpack a compressed file of thin metadata.")
|
||||
.arg(Arg::with_name("INPUT")
|
||||
.arg(
|
||||
Arg::with_name("INPUT")
|
||||
.help("Specify thinp metadata binary device/file")
|
||||
.required(true)
|
||||
.short("i")
|
||||
.value_name("DEV")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("OUTPUT")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("OUTPUT")
|
||||
.help("Specify packed output file")
|
||||
.required(true)
|
||||
.short("o")
|
||||
.value_name("FILE")
|
||||
.takes_value(true));
|
||||
.takes_value(true),
|
||||
);
|
||||
|
||||
let matches = parser.get_matches();
|
||||
let input_file = matches.value_of("INPUT").unwrap();
|
||||
let output_file = matches.value_of("OUTPUT").unwrap();
|
||||
let input_file = Path::new(matches.value_of("INPUT").unwrap());
|
||||
let output_file = Path::new(matches.value_of("OUTPUT").unwrap());
|
||||
|
||||
if !file_utils::file_exists(input_file) {
|
||||
eprintln!("Couldn't find input file '{}'.", &input_file);
|
||||
eprintln!("Couldn't find input file '{}'.", &input_file.display());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
74
src/bin/thin_shrink.rs
Normal file
74
src/bin/thin_shrink.rs
Normal file
@ -0,0 +1,74 @@
|
||||
extern crate clap;
|
||||
extern crate thinp;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
use thinp::file_utils;
|
||||
|
||||
fn main() {
|
||||
let parser = App::new("thin_shrink")
|
||||
.version(thinp::version::TOOLS_VERSION)
|
||||
.about("Rewrite xml metadata and move data in an inactive pool.")
|
||||
.arg(
|
||||
Arg::with_name("INPUT")
|
||||
.help("Specify thinp metadata xml file")
|
||||
.required(true)
|
||||
.long("input")
|
||||
.value_name("INPUT")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("OUTPUT")
|
||||
.help("Specify output xml file")
|
||||
.required(true)
|
||||
.long("output")
|
||||
.value_name("OUTPUT")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("DATA")
|
||||
.help("Specify pool data device where data will be moved")
|
||||
.required(true)
|
||||
.long("data")
|
||||
.value_name("DATA")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("NOCOPY")
|
||||
.help("Skip the copying of data, useful for benchmarking")
|
||||
.required(false)
|
||||
.long("no-copy")
|
||||
.value_name("NOCOPY")
|
||||
.takes_value(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("SIZE")
|
||||
.help("Specify new size for the pool (in data blocks)")
|
||||
.required(true)
|
||||
.long("nr-blocks")
|
||||
.value_name("SIZE")
|
||||
.takes_value(true),
|
||||
);
|
||||
|
||||
let matches = parser.get_matches();
|
||||
|
||||
// FIXME: check these look like xml
|
||||
let input_file = Path::new(matches.value_of("INPUT").unwrap());
|
||||
let output_file = Path::new(matches.value_of("OUTPUT").unwrap());
|
||||
let size = matches.value_of("SIZE").unwrap().parse::<u64>().unwrap();
|
||||
let data_file = Path::new(matches.value_of("DATA").unwrap());
|
||||
let do_copy = !matches.is_present("NOCOPY");
|
||||
|
||||
if !file_utils::file_exists(input_file) {
|
||||
eprintln!("Couldn't find input file '{}'.", input_file.display());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if let Err(reason) =
|
||||
thinp::shrink::toplevel::shrink(&input_file, &output_file, &data_file, size, do_copy)
|
||||
{
|
||||
println!("Application error: {}\n", reason);
|
||||
exit(1);
|
||||
}
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
use nix::sys::stat;
|
||||
use nix::sys::stat::{FileStat, SFlag};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::fs::File;
|
||||
use std::io::{Seek, Write};
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::path::Path;
|
||||
use tempfile::tempfile;
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
@ -11,15 +14,13 @@ fn check_bits(mode: u32, flag: &SFlag) -> bool {
|
||||
}
|
||||
|
||||
pub fn is_file_or_blk(info: FileStat) -> bool {
|
||||
check_bits(info.st_mode, &stat::SFlag::S_IFBLK) ||
|
||||
check_bits(info.st_mode, &stat::SFlag::S_IFREG)
|
||||
check_bits(info.st_mode, &stat::SFlag::S_IFBLK)
|
||||
|| check_bits(info.st_mode, &stat::SFlag::S_IFREG)
|
||||
}
|
||||
|
||||
pub fn file_exists(path: &str) -> bool {
|
||||
pub fn file_exists(path: &Path) -> bool {
|
||||
match stat::stat(path) {
|
||||
Ok(info) => {
|
||||
is_file_or_blk(info)
|
||||
}
|
||||
Ok(info) => is_file_or_blk(info),
|
||||
_ => {
|
||||
// FIXME: assuming all errors indicate the file doesn't
|
||||
// exist.
|
||||
@ -39,19 +40,19 @@ pub fn fail<T>(msg: &str) -> io::Result<T> {
|
||||
Err(e)
|
||||
}
|
||||
|
||||
fn get_device_size(path: &str) -> io::Result<u64> {
|
||||
fn get_device_size(path: &Path) -> io::Result<u64> {
|
||||
let file = File::open(path)?;
|
||||
let fd = file.as_raw_fd();
|
||||
let mut cap = 0u64;
|
||||
unsafe {
|
||||
match ioctl_blkgetsize64(fd, &mut cap) {
|
||||
Ok(_) => {Ok(cap)}
|
||||
_ => {fail("BLKGETSIZE64 ioctl failed")}
|
||||
Ok(_) => Ok(cap),
|
||||
_ => fail("BLKGETSIZE64 ioctl failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_size(path: &str) -> io::Result<u64> {
|
||||
pub fn file_size(path: &Path) -> io::Result<u64> {
|
||||
match stat::stat(path) {
|
||||
Ok(info) => {
|
||||
if check_bits(info.st_mode, &SFlag::S_IFREG) {
|
||||
@ -62,10 +63,38 @@ pub fn file_size(path: &str) -> io::Result<u64> {
|
||||
fail("not a regular file or block device")
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
fail("stat failed")
|
||||
}
|
||||
_ => fail("stat failed"),
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
fn set_size<W: Write + Seek>(w: &mut W, nr_bytes: u64) -> io::Result<()> {
|
||||
let zeroes: Vec<u8> = vec![0; 1];
|
||||
|
||||
if nr_bytes > 0 {
|
||||
w.seek(io::SeekFrom::Start(nr_bytes - 1))?;
|
||||
w.write_all(&zeroes)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn temp_file_sized(nr_bytes: u64) -> io::Result<std::fs::File> {
|
||||
let mut file = tempfile()?;
|
||||
set_size(&mut file, nr_bytes)?;
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
pub fn create_sized_file(path: &Path, nr_bytes: u64) -> io::Result<std::fs::File> {
|
||||
let mut file = OpenOptions::new()
|
||||
.read(false)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
set_size(&mut file, nr_bytes)?;
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
|
@ -1,3 +1,4 @@
|
||||
extern crate anyhow;
|
||||
extern crate byteorder;
|
||||
extern crate crc32c;
|
||||
extern crate flate2;
|
||||
@ -18,4 +19,6 @@ pub mod block_manager;
|
||||
pub mod check;
|
||||
pub mod file_utils;
|
||||
pub mod pack;
|
||||
pub mod shrink;
|
||||
pub mod thin;
|
||||
pub mod version;
|
||||
|
@ -6,6 +6,7 @@ use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::{
|
||||
error::Error,
|
||||
fs::OpenOptions,
|
||||
path::Path,
|
||||
io,
|
||||
io::prelude::*,
|
||||
io::Cursor,
|
||||
@ -67,7 +68,7 @@ fn mk_chunk_vecs(nr_blocks: u64, nr_jobs: u64) -> Vec<Vec<(u64, u64)>> {
|
||||
vs
|
||||
}
|
||||
|
||||
pub fn pack(input_file: &str, output_file: &str) -> Result<(), Box<dyn Error>> {
|
||||
pub fn pack(input_file: &Path, output_file: &Path) -> Result<(), Box<dyn Error>> {
|
||||
let nr_blocks = get_nr_blocks(&input_file)?;
|
||||
let nr_jobs = std::cmp::max(1, std::cmp::min(num_cpus::get() as u64, nr_blocks / 128));
|
||||
let chunk_vecs = mk_chunk_vecs(nr_blocks, nr_jobs);
|
||||
@ -192,7 +193,7 @@ where
|
||||
r.read_u64::<LittleEndian>()
|
||||
}
|
||||
|
||||
fn get_nr_blocks(path: &str) -> io::Result<u64> {
|
||||
fn get_nr_blocks(path: &Path) -> io::Result<u64> {
|
||||
let len = file_utils::file_size(path)?;
|
||||
Ok(len / (BLOCK_SIZE as u64))
|
||||
}
|
||||
@ -306,7 +307,7 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unpack(input_file: &str, output_file: &str) -> Result<(), Box<dyn Error>> {
|
||||
pub fn unpack(input_file: &Path, output_file: &Path) -> Result<(), Box<dyn Error>> {
|
||||
let mut input = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(false)
|
||||
|
61
src/shrink/copier.rs
Normal file
61
src/shrink/copier.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use anyhow::Result;
|
||||
use std::fs::OpenOptions;
|
||||
use std::path::Path;
|
||||
use std::io::{Seek, SeekFrom, Write, Read};
|
||||
//use std::os::unix::fs::OpenOptionsExt;
|
||||
|
||||
pub type Sector = u64;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Region {
|
||||
pub src: Sector,
|
||||
pub dest: Sector,
|
||||
pub len: Sector,
|
||||
}
|
||||
|
||||
|
||||
fn copy_step<W>(file: &mut W, src_byte: u64, dest_byte: u64, len: usize) -> Result<()>
|
||||
where
|
||||
W: Write + Seek + Read,
|
||||
{
|
||||
let mut buf = vec![0; len];
|
||||
file.seek(SeekFrom::Start(src_byte))?;
|
||||
file.read_exact(&mut buf)?;
|
||||
file.seek(SeekFrom::Start(dest_byte))?;
|
||||
file.write_all(&buf)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_region<W>(file: &mut W, r: &Region) -> Result<()>
|
||||
where
|
||||
W: Write + Seek + Read,
|
||||
{
|
||||
const MAX_BYTES: Sector = 1024 * 1024 * 64;
|
||||
|
||||
let src_bytes = r.src * 512;
|
||||
let dest_bytes = r.dest * 512;
|
||||
let len_bytes = r.len * 512;
|
||||
let mut written = 0;
|
||||
while written != len_bytes {
|
||||
let step = u64::min(len_bytes - written, MAX_BYTES);
|
||||
copy_step(file, src_bytes + written, dest_bytes + written, step as usize)?;
|
||||
written += step;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn copy(path: &Path, regions: &[Region]) -> Result<()> {
|
||||
let mut input = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
//.custom_flags(libc::O_DIRECT)
|
||||
.open(path)?;
|
||||
|
||||
for r in regions {
|
||||
eprintln!("copying {:?}", r);
|
||||
copy_region(&mut input, r)?;
|
||||
}
|
||||
input.flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
3
src/shrink/mod.rs
Normal file
3
src/shrink/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod toplevel;
|
||||
|
||||
mod copier;
|
528
src/shrink/toplevel.rs
Normal file
528
src/shrink/toplevel.rs
Normal file
@ -0,0 +1,528 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use fixedbitset::FixedBitSet;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::shrink::copier::{self, Region};
|
||||
use crate::thin::xml::{self, Visit};
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Pass1 {
|
||||
// FIXME: Inefficient, use a range_set of some description
|
||||
allocated_blocks: FixedBitSet,
|
||||
|
||||
nr_blocks: u64,
|
||||
|
||||
/// High blocks are beyond the new, reduced end of the pool. These
|
||||
/// will need to be moved.
|
||||
nr_high_blocks: u64,
|
||||
block_size: Option<u64>,
|
||||
}
|
||||
|
||||
impl Pass1 {
|
||||
fn new(nr_blocks: u64) -> Pass1 {
|
||||
Pass1 {
|
||||
allocated_blocks: FixedBitSet::with_capacity(0),
|
||||
nr_blocks,
|
||||
nr_high_blocks: 0,
|
||||
block_size: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl xml::MetadataVisitor for Pass1 {
|
||||
fn superblock_b(&mut self, sb: &xml::Superblock) -> Result<Visit> {
|
||||
self.allocated_blocks.grow(sb.nr_data_blocks as usize);
|
||||
self.block_size = Some(sb.data_block_size as u64);
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn superblock_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_b(&mut self, _d: &xml::Device) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn map(&mut self, m: &xml::Map) -> Result<Visit> {
|
||||
for i in m.data_begin..(m.data_begin + m.len) {
|
||||
if i > self.nr_blocks {
|
||||
self.nr_high_blocks += 1;
|
||||
}
|
||||
self.allocated_blocks.insert(i as usize);
|
||||
}
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
// Writes remapped xml
|
||||
struct Pass2<W: Write> {
|
||||
writer: xml::XmlWriter<W>,
|
||||
nr_blocks: u64,
|
||||
remaps: Vec<(BlockRange, BlockRange)>,
|
||||
}
|
||||
|
||||
impl<W: Write> Pass2<W> {
|
||||
fn new(w: W, nr_blocks: u64, remaps: Vec<(BlockRange, BlockRange)>) -> Pass2<W> {
|
||||
Pass2 {
|
||||
writer: xml::XmlWriter::new(w),
|
||||
nr_blocks,
|
||||
remaps,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> xml::MetadataVisitor for Pass2<W> {
|
||||
fn superblock_b(&mut self, sb: &xml::Superblock) -> Result<Visit> {
|
||||
self.writer.superblock_b(sb)
|
||||
}
|
||||
|
||||
fn superblock_e(&mut self) -> Result<Visit> {
|
||||
self.writer.superblock_e()
|
||||
}
|
||||
|
||||
fn device_b(&mut self, d: &xml::Device) -> Result<Visit> {
|
||||
self.writer.device_b(d)
|
||||
}
|
||||
|
||||
fn device_e(&mut self) -> Result<Visit> {
|
||||
self.writer.device_e()
|
||||
}
|
||||
|
||||
fn map(&mut self, m: &xml::Map) -> Result<Visit> {
|
||||
if m.data_begin + m.len < self.nr_blocks {
|
||||
// no remapping needed.
|
||||
self.writer.map(m)?;
|
||||
} else {
|
||||
let r = m.data_begin..(m.data_begin + m.len);
|
||||
let remaps = remap(&r, &self.remaps);
|
||||
let mut written = 0;
|
||||
|
||||
for r in remaps {
|
||||
self.writer.map(&xml::Map {
|
||||
thin_begin: m.thin_begin + written,
|
||||
data_begin: r.start,
|
||||
time: m.time,
|
||||
len: range_len(&r),
|
||||
})?;
|
||||
written += range_len(&r);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<Visit> {
|
||||
self.writer.eof()
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
type BlockRange = std::ops::Range<u64>;
|
||||
|
||||
fn bits_to_ranges(bits: &FixedBitSet) -> Vec<BlockRange> {
|
||||
let mut ranges = Vec::new();
|
||||
let mut start = None;
|
||||
|
||||
for i in 0..bits.len() {
|
||||
match (bits[i], start) {
|
||||
(false, None) => {}
|
||||
(true, None) => {
|
||||
start = Some((i as u64, 1));
|
||||
}
|
||||
(false, Some((b, len))) => {
|
||||
ranges.push(b..(b + len));
|
||||
start = None;
|
||||
}
|
||||
(true, Some((b, len))) => {
|
||||
start = Some((b, len + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((b, len)) = start {
|
||||
ranges.push(b..(b + len));
|
||||
}
|
||||
|
||||
ranges
|
||||
}
|
||||
|
||||
// Splits the ranges into those below threshold, and those equal or
|
||||
// above threshold below threshold, and those equal or above threshold
|
||||
fn ranges_split(ranges: &[BlockRange], threshold: u64) -> (Vec<BlockRange>, Vec<BlockRange>) {
|
||||
use std::ops::Range;
|
||||
|
||||
let mut below = Vec::new();
|
||||
let mut above = Vec::new();
|
||||
for r in ranges {
|
||||
match r {
|
||||
Range { start, end } if *end <= threshold => below.push(*start..*end),
|
||||
Range { start, end } if *start < threshold => {
|
||||
below.push(*start..threshold);
|
||||
above.push(threshold..*end);
|
||||
}
|
||||
Range { start, end } => above.push(*start..*end),
|
||||
}
|
||||
}
|
||||
(below, above)
|
||||
}
|
||||
|
||||
fn negate_ranges(ranges: &[BlockRange], upper_limit: u64) -> Vec<BlockRange> {
|
||||
use std::ops::Range;
|
||||
|
||||
let mut result = Vec::new();
|
||||
let mut cursor = 0;
|
||||
|
||||
for r in ranges {
|
||||
match r {
|
||||
Range { start, end } if cursor < *start => {
|
||||
result.push(cursor..*start);
|
||||
cursor = *end;
|
||||
}
|
||||
Range { start: _, end } => {
|
||||
cursor = *end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cursor < upper_limit {
|
||||
result.push(cursor..upper_limit);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn range_len(r: &BlockRange) -> u64 {
|
||||
r.end - r.start
|
||||
}
|
||||
|
||||
fn ranges_total(rs: &[BlockRange]) -> u64 {
|
||||
rs.iter().fold(0, |sum, r| sum + range_len(r))
|
||||
}
|
||||
|
||||
// Assumes there is enough space to remap.
|
||||
fn build_remaps(ranges: Vec<BlockRange>, free: Vec<BlockRange>) -> Vec<(BlockRange, BlockRange)> {
|
||||
use std::cmp::Ordering;
|
||||
|
||||
let mut remap = Vec::new();
|
||||
let mut range_iter = ranges.into_iter();
|
||||
let mut free_iter = free.into_iter();
|
||||
|
||||
let mut r_ = range_iter.next();
|
||||
let mut f_ = free_iter.next();
|
||||
|
||||
while let (Some(r), Some(f)) = (r_, f_) {
|
||||
let rlen = range_len(&r);
|
||||
let flen = range_len(&f);
|
||||
|
||||
match rlen.cmp(&flen) {
|
||||
Ordering::Less => {
|
||||
// range fits into the free chunk
|
||||
remap.push((r, f.start..(f.start + rlen)));
|
||||
f_ = Some((f.start + rlen)..f.end);
|
||||
r_ = range_iter.next();
|
||||
}
|
||||
Ordering::Equal => {
|
||||
remap.push((r, f));
|
||||
r_ = range_iter.next();
|
||||
f_ = free_iter.next();
|
||||
}
|
||||
Ordering::Greater => {
|
||||
remap.push((r.start..(r.start + flen), f));
|
||||
r_ = Some((r.start + flen)..r.end);
|
||||
f_ = free_iter.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
remap
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_remaps() {
|
||||
struct Test {
|
||||
ranges: Vec<BlockRange>,
|
||||
free: Vec<BlockRange>,
|
||||
result: Vec<(BlockRange, BlockRange)>,
|
||||
}
|
||||
|
||||
let tests = vec![
|
||||
Test {
|
||||
ranges: vec![],
|
||||
free: vec![],
|
||||
result: vec![],
|
||||
},
|
||||
Test {
|
||||
ranges: vec![],
|
||||
free: vec![0..100],
|
||||
result: vec![],
|
||||
},
|
||||
Test {
|
||||
ranges: vec![1000..1002],
|
||||
free: vec![0..100],
|
||||
result: vec![(1000..1002, 0..2)],
|
||||
},
|
||||
Test {
|
||||
ranges: vec![1000..1002, 1100..1110],
|
||||
free: vec![0..100],
|
||||
result: vec![(1000..1002, 0..2), (1100..1110, 2..12)],
|
||||
},
|
||||
Test {
|
||||
ranges: vec![100..120],
|
||||
free: vec![0..5, 20..23, 30..50],
|
||||
result: vec![(100..105, 0..5), (105..108, 20..23), (108..120, 30..42)],
|
||||
},
|
||||
];
|
||||
|
||||
for t in tests {
|
||||
assert_eq!(build_remaps(t.ranges, t.free), t.result);
|
||||
}
|
||||
}
|
||||
|
||||
fn overlaps(r1: &BlockRange, r2: &BlockRange, index: usize) -> Option<usize> {
|
||||
if r1.start >= r2.end {
|
||||
return None;
|
||||
}
|
||||
|
||||
if r2.start >= r1.end {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(index)
|
||||
}
|
||||
|
||||
// Finds the index of the first entry that overlaps r.
|
||||
fn find_first(r: &BlockRange, remaps: &[(BlockRange, BlockRange)]) -> Option<usize> {
|
||||
if remaps.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
match remaps.binary_search_by_key(&r.start, |(from, _)| from.start) {
|
||||
Ok(n) => Some(n),
|
||||
Err(n) => {
|
||||
if n == 0 {
|
||||
let (from, _) = &remaps[n];
|
||||
overlaps(&r, &from, n)
|
||||
} else if n == remaps.len() {
|
||||
let (from, _) = &remaps[n - 1];
|
||||
overlaps(&r, from, n - 1)
|
||||
} else {
|
||||
// Need to check the previous entry
|
||||
let (from, _) = &remaps[n - 1];
|
||||
overlaps(&r, &from, n - 1).or_else(|| {
|
||||
let (from, _) = &remaps[n];
|
||||
overlaps(&r, &from, n)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_empty(r: &BlockRange) -> bool {
|
||||
r.start == r.end
|
||||
}
|
||||
|
||||
// remaps must be in sorted order by from.start.
|
||||
fn remap(r: &BlockRange, remaps: &[(BlockRange, BlockRange)]) -> Vec<BlockRange> {
|
||||
let mut remap = Vec::new();
|
||||
let mut r = r.start..r.end;
|
||||
|
||||
if let Some(index) = find_first(&r, &remaps) {
|
||||
let mut index = index;
|
||||
loop {
|
||||
let (from, to) = &remaps[index];
|
||||
|
||||
// There may be a prefix that doesn't overlap with 'from'
|
||||
if r.start < from.start {
|
||||
let len = u64::min(range_len(&r), from.start - r.start);
|
||||
remap.push(r.start..(r.start + len));
|
||||
r = (r.start + len)..r.end;
|
||||
|
||||
if is_empty(&r) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let to = (to.start + (r.start - from.start))..to.end;
|
||||
let from = r.start..from.end;
|
||||
let rlen = range_len(&r);
|
||||
let flen = range_len(&from);
|
||||
|
||||
let len = u64::min(rlen, flen);
|
||||
remap.push(to.start..(to.start + len));
|
||||
|
||||
r = (r.start + len)..r.end;
|
||||
if is_empty(&r) {
|
||||
break;
|
||||
}
|
||||
|
||||
if len == flen {
|
||||
index += 1;
|
||||
}
|
||||
|
||||
if index == remaps.len() {
|
||||
remap.push(r.start..r.end);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
remap.push(r.start..r.end);
|
||||
}
|
||||
|
||||
remap
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn remap_test() {
|
||||
struct Test {
|
||||
remaps: Vec<(BlockRange, BlockRange)>,
|
||||
input: BlockRange,
|
||||
output: Vec<BlockRange>,
|
||||
}
|
||||
|
||||
let tests = [
|
||||
Test {
|
||||
remaps: vec![],
|
||||
input: 0..1,
|
||||
output: vec![0..1],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![],
|
||||
input: 100..1000,
|
||||
output: vec![100..1000],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 0..5,
|
||||
output: vec![0..5],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 10..20,
|
||||
output: vec![110..120],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 5..15,
|
||||
output: vec![5..10, 110..115],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 5..25,
|
||||
output: vec![5..10, 110..120, 20..25],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 15..25,
|
||||
output: vec![115..120, 20..25],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120)],
|
||||
input: 25..35,
|
||||
output: vec![25..35],
|
||||
},
|
||||
Test {
|
||||
remaps: vec![(10..20, 110..120), (30..40, 230..240)],
|
||||
input: 0..50,
|
||||
output: vec![0..10, 110..120, 20..30, 230..240, 40..50],
|
||||
},
|
||||
];
|
||||
|
||||
for t in &tests {
|
||||
let rs = remap(&t.input, &t.remaps);
|
||||
assert_eq!(rs, t.output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build_copy_regions(remaps: &[(BlockRange, BlockRange)], block_size: u64) -> Vec<Region> {
|
||||
let mut rs = Vec::new();
|
||||
|
||||
for (from, to) in remaps {
|
||||
rs.push(Region {
|
||||
src: from.start * block_size,
|
||||
dest: to.start * block_size,
|
||||
len: range_len(&from) * block_size,
|
||||
});
|
||||
}
|
||||
|
||||
rs
|
||||
}
|
||||
|
||||
fn process_xml<MV: xml::MetadataVisitor>(input_path: &Path, pass: &mut MV) -> Result<()> {
|
||||
let input = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(false)
|
||||
.custom_flags(libc::O_EXCL)
|
||||
.open(input_path)?;
|
||||
|
||||
xml::read(input, pass)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn shrink(
|
||||
input_path: &Path,
|
||||
output_path: &Path,
|
||||
data_path: &Path,
|
||||
nr_blocks: u64,
|
||||
do_copy: bool,
|
||||
) -> Result<()> {
|
||||
let mut pass1 = Pass1::new(nr_blocks);
|
||||
eprint!("Reading xml...");
|
||||
process_xml(input_path, &mut pass1)?;
|
||||
eprintln!("done");
|
||||
eprintln!("{} blocks need moving", pass1.nr_high_blocks);
|
||||
|
||||
let ranges = bits_to_ranges(&pass1.allocated_blocks);
|
||||
let (below, above) = ranges_split(&ranges, nr_blocks);
|
||||
|
||||
let free = negate_ranges(&below, nr_blocks);
|
||||
let free_blocks = ranges_total(&free);
|
||||
eprintln!("{} free blocks.", free_blocks);
|
||||
|
||||
if free_blocks < pass1.nr_high_blocks {
|
||||
return Err(anyhow!("Insufficient space"));
|
||||
}
|
||||
|
||||
let remaps = build_remaps(above, free);
|
||||
|
||||
if do_copy {
|
||||
let regions = build_copy_regions(&remaps, pass1.block_size.unwrap() as u64);
|
||||
copier::copy(data_path, ®ions)?;
|
||||
} else {
|
||||
eprintln!("skipping copy");
|
||||
}
|
||||
|
||||
let output = OpenOptions::new()
|
||||
.read(false)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(output_path)?;
|
||||
let mut pass2 = Pass2::new(output, nr_blocks, remaps);
|
||||
eprint!("writing new xml...");
|
||||
process_xml(input_path, &mut pass2)?;
|
||||
eprintln!("done.");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//---------------------------------------
|
1
src/thin/mod.rs
Normal file
1
src/thin/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod xml;
|
413
src/thin/xml.rs
Normal file
413
src/thin/xml.rs
Normal file
@ -0,0 +1,413 @@
|
||||
use anyhow::Result;
|
||||
use std::{borrow::Cow, fmt::Display, io::prelude::*, io::BufReader, io::Write};
|
||||
|
||||
use quick_xml::events::attributes::Attribute;
|
||||
use quick_xml::events::{BytesEnd, BytesStart, Event};
|
||||
use quick_xml::{Reader, Writer};
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Superblock {
|
||||
pub uuid: String,
|
||||
pub time: u64,
|
||||
pub transaction: u64,
|
||||
pub flags: Option<u32>,
|
||||
pub version: Option<u32>,
|
||||
pub data_block_size: u32,
|
||||
pub nr_data_blocks: u64,
|
||||
pub metadata_snap: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Device {
|
||||
pub dev_id: u32,
|
||||
pub mapped_blocks: u64,
|
||||
pub transaction: u64,
|
||||
pub creation_time: u64,
|
||||
pub snap_time: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Map {
|
||||
pub thin_begin: u64,
|
||||
pub data_begin: u64,
|
||||
pub time: u32,
|
||||
pub len: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Visit {
|
||||
Continue,
|
||||
Stop,
|
||||
}
|
||||
|
||||
pub trait MetadataVisitor {
|
||||
fn superblock_b(&mut self, sb: &Superblock) -> Result<Visit>;
|
||||
fn superblock_e(&mut self) -> Result<Visit>;
|
||||
|
||||
fn device_b(&mut self, d: &Device) -> Result<Visit>;
|
||||
fn device_e(&mut self) -> Result<Visit>;
|
||||
|
||||
fn map(&mut self, m: &Map) -> Result<Visit>;
|
||||
|
||||
fn eof(&mut self) -> Result<Visit>;
|
||||
}
|
||||
|
||||
pub struct XmlWriter<W: Write> {
|
||||
w: Writer<W>,
|
||||
}
|
||||
|
||||
impl<W: Write> XmlWriter<W> {
|
||||
pub fn new(w: W) -> XmlWriter<W> {
|
||||
XmlWriter {
|
||||
w: Writer::new_with_indent(w, 0x20, 2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn mk_attr_<'a, T: Display>(n: T) -> Cow<'a, [u8]> {
|
||||
let str = format!("{}", n);
|
||||
Cow::Owned(str.into_bytes())
|
||||
}
|
||||
|
||||
fn mk_attr<T: Display>(key: &[u8], value: T) -> Attribute {
|
||||
Attribute {
|
||||
key,
|
||||
value: mk_attr_(value),
|
||||
}
|
||||
}
|
||||
|
||||
const XML_VERSION: u32 = 2;
|
||||
|
||||
impl<W: Write> MetadataVisitor for XmlWriter<W> {
|
||||
fn superblock_b(&mut self, sb: &Superblock) -> Result<Visit> {
|
||||
let tag = b"superblock";
|
||||
let mut elem = BytesStart::owned(tag.to_vec(), tag.len());
|
||||
elem.push_attribute(mk_attr(b"uuid", sb.uuid.clone()));
|
||||
elem.push_attribute(mk_attr(b"time", sb.time));
|
||||
elem.push_attribute(mk_attr(b"transaction", sb.transaction));
|
||||
if let Some(flags) = sb.flags {
|
||||
// FIXME: is this really a nr?
|
||||
elem.push_attribute(mk_attr(b"flags", flags));
|
||||
}
|
||||
|
||||
elem.push_attribute(mk_attr(b"version", XML_VERSION));
|
||||
elem.push_attribute(mk_attr(b"data_block_size", sb.data_block_size));
|
||||
elem.push_attribute(mk_attr(b"nr_data_blocks", sb.nr_data_blocks));
|
||||
|
||||
if let Some(snap) = sb.metadata_snap {
|
||||
elem.push_attribute(mk_attr(b"metadata_snap", snap));
|
||||
}
|
||||
|
||||
self.w.write_event(Event::Start(elem))?;
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn superblock_e(&mut self) -> Result<Visit> {
|
||||
self.w
|
||||
.write_event(Event::End(BytesEnd::borrowed(b"superblock")))?;
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_b(&mut self, d: &Device) -> Result<Visit> {
|
||||
let tag = b"device";
|
||||
let mut elem = BytesStart::owned(tag.to_vec(), tag.len());
|
||||
elem.push_attribute(mk_attr(b"dev_id", d.dev_id));
|
||||
elem.push_attribute(mk_attr(b"mapped_blocks", d.mapped_blocks));
|
||||
elem.push_attribute(mk_attr(b"transaction", d.transaction));
|
||||
elem.push_attribute(mk_attr(b"creation_time", d.creation_time));
|
||||
elem.push_attribute(mk_attr(b"snap_time", d.snap_time));
|
||||
self.w.write_event(Event::Start(elem))?;
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_e(&mut self) -> Result<Visit> {
|
||||
self.w
|
||||
.write_event(Event::End(BytesEnd::borrowed(b"device")))?;
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn map(&mut self, m: &Map) -> Result<Visit> {
|
||||
match m.len {
|
||||
1 => {
|
||||
let tag = b"single_mapping";
|
||||
let mut elem = BytesStart::owned(tag.to_vec(), tag.len());
|
||||
elem.push_attribute(mk_attr(b"origin_block", m.thin_begin));
|
||||
elem.push_attribute(mk_attr(b"data_block", m.data_begin));
|
||||
elem.push_attribute(mk_attr(b"time", m.time));
|
||||
self.w.write_event(Event::Empty(elem))?;
|
||||
}
|
||||
_ => {
|
||||
let tag = b"range_mapping";
|
||||
let mut elem = BytesStart::owned(tag.to_vec(), tag.len());
|
||||
elem.push_attribute(mk_attr(b"origin_begin", m.thin_begin));
|
||||
elem.push_attribute(mk_attr(b"data_begin", m.data_begin));
|
||||
elem.push_attribute(mk_attr(b"length", m.len));
|
||||
elem.push_attribute(mk_attr(b"time", m.time));
|
||||
self.w.write_event(Event::Empty(elem))?;
|
||||
}
|
||||
}
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<Visit> {
|
||||
let w = self.w.inner();
|
||||
w.flush()?;
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
// FIXME: nasty unwraps
|
||||
fn string_val(kv: &Attribute) -> String {
|
||||
let v = kv.unescaped_value().unwrap();
|
||||
let bytes = v.to_vec();
|
||||
String::from_utf8(bytes).unwrap()
|
||||
}
|
||||
|
||||
// FIXME: there's got to be a way of doing this without copying the string
|
||||
fn u64_val(kv: &Attribute) -> Result<u64> {
|
||||
let n = string_val(kv).parse::<u64>()?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn u32_val(kv: &Attribute) -> Result<u32> {
|
||||
let n = string_val(kv).parse::<u32>()?;
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn bad_attr<T>(_tag: &str, _attr: &[u8]) -> Result<T> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
fn missing_attr<T>(_tag: &str, _attr: &str) -> Result<T> {
|
||||
todo!();
|
||||
}
|
||||
|
||||
fn check_attr<T>(tag: &str, name: &str, maybe_v: Option<T>) -> Result<T> {
|
||||
match maybe_v {
|
||||
None => missing_attr(tag, name),
|
||||
Some(v) => Ok(v),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_superblock(e: &BytesStart) -> Result<Superblock> {
|
||||
let mut uuid: Option<String> = None;
|
||||
let mut time: Option<u64> = None;
|
||||
let mut transaction: Option<u64> = None;
|
||||
let mut flags: Option<u32> = None;
|
||||
let mut version: Option<u32> = None;
|
||||
let mut data_block_size: Option<u32> = None;
|
||||
let mut nr_data_blocks: Option<u64> = None;
|
||||
let mut metadata_snap: Option<u64> = None;
|
||||
|
||||
for a in e.attributes() {
|
||||
let kv = a.unwrap();
|
||||
match kv.key {
|
||||
b"uuid" => uuid = Some(string_val(&kv)),
|
||||
b"time" => time = Some(u64_val(&kv)?),
|
||||
b"transaction" => transaction = Some(u64_val(&kv)?),
|
||||
b"flags" => flags = Some(u32_val(&kv)?),
|
||||
b"version" => version = Some(u32_val(&kv)?),
|
||||
b"data_block_size" => data_block_size = Some(u32_val(&kv)?),
|
||||
b"nr_data_blocks" => nr_data_blocks = Some(u64_val(&kv)?),
|
||||
b"metadata_snap" => metadata_snap = Some(u64_val(&kv)?),
|
||||
_ => return bad_attr("superblock", kv.key),
|
||||
}
|
||||
}
|
||||
|
||||
let tag = "superblock";
|
||||
|
||||
Ok(Superblock {
|
||||
uuid: check_attr(tag, "uuid", uuid)?,
|
||||
time: check_attr(tag, "time", time)?,
|
||||
transaction: check_attr(tag, "transaction", transaction)?,
|
||||
flags,
|
||||
version,
|
||||
data_block_size: check_attr(tag, "data_block_size", data_block_size)?,
|
||||
nr_data_blocks: check_attr(tag, "nr_data_blocks", nr_data_blocks)?,
|
||||
metadata_snap,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_device(e: &BytesStart) -> Result<Device> {
|
||||
let mut dev_id: Option<u32> = None;
|
||||
let mut mapped_blocks: Option<u64> = None;
|
||||
let mut transaction: Option<u64> = None;
|
||||
let mut creation_time: Option<u64> = None;
|
||||
let mut snap_time: Option<u64> = None;
|
||||
|
||||
for a in e.attributes() {
|
||||
let kv = a.unwrap();
|
||||
match kv.key {
|
||||
b"dev_id" => dev_id = Some(u32_val(&kv)?),
|
||||
b"mapped_blocks" => mapped_blocks = Some(u64_val(&kv)?),
|
||||
b"transaction" => transaction = Some(u64_val(&kv)?),
|
||||
b"creation_time" => creation_time = Some(u64_val(&kv)?),
|
||||
b"snap_time" => snap_time = Some(u64_val(&kv)?),
|
||||
_ => return bad_attr("device", kv.key),
|
||||
}
|
||||
}
|
||||
|
||||
let tag = "device";
|
||||
|
||||
Ok(Device {
|
||||
dev_id: check_attr(tag, "dev_id", dev_id)?,
|
||||
mapped_blocks: check_attr(tag, "mapped_blocks", mapped_blocks)?,
|
||||
transaction: check_attr(tag, "transaction", transaction)?,
|
||||
creation_time: check_attr(tag, "creation_time", creation_time)?,
|
||||
snap_time: check_attr(tag, "snap_time", snap_time)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_single_map(e: &BytesStart) -> Result<Map> {
|
||||
let mut thin_begin: Option<u64> = None;
|
||||
let mut data_begin: Option<u64> = None;
|
||||
let mut time: Option<u32> = None;
|
||||
|
||||
for a in e.attributes() {
|
||||
let kv = a.unwrap();
|
||||
match kv.key {
|
||||
b"origin_block" => thin_begin = Some(u64_val(&kv)?),
|
||||
b"data_block" => data_begin = Some(u64_val(&kv)?),
|
||||
b"time" => time = Some(u32_val(&kv)?),
|
||||
_ => return bad_attr("single_mapping", kv.key),
|
||||
}
|
||||
}
|
||||
|
||||
let tag = "single_mapping";
|
||||
|
||||
Ok(Map {
|
||||
thin_begin: check_attr(tag, "origin_block", thin_begin)?,
|
||||
data_begin: check_attr(tag, "data_block", data_begin)?,
|
||||
time: check_attr(tag, "time", time)?,
|
||||
len: 1,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_range_map(e: &BytesStart) -> Result<Map> {
|
||||
let mut thin_begin: Option<u64> = None;
|
||||
let mut data_begin: Option<u64> = None;
|
||||
let mut time: Option<u32> = None;
|
||||
let mut length: Option<u64> = None;
|
||||
|
||||
for a in e.attributes() {
|
||||
let kv = a.unwrap();
|
||||
match kv.key {
|
||||
b"origin_begin" => thin_begin = Some(u64_val(&kv)?),
|
||||
b"data_begin" => data_begin = Some(u64_val(&kv)?),
|
||||
b"time" => time = Some(u32_val(&kv)?),
|
||||
b"length" => length = Some(u64_val(&kv)?),
|
||||
_ => return bad_attr("range_mapping", kv.key),
|
||||
}
|
||||
}
|
||||
|
||||
let tag = "range_mapping";
|
||||
|
||||
Ok(Map {
|
||||
thin_begin: check_attr(tag, "origin_begin", thin_begin)?,
|
||||
data_begin: check_attr(tag, "data_begin", data_begin)?,
|
||||
time: check_attr(tag, "time", time)?,
|
||||
len: check_attr(tag, "length", length)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_event<R, M>(reader: &mut Reader<R>, buf: &mut Vec<u8>, visitor: &mut M) -> Result<Visit>
|
||||
where
|
||||
R: Read + BufRead,
|
||||
M: MetadataVisitor,
|
||||
{
|
||||
match reader.read_event(buf) {
|
||||
Ok(Event::Start(ref e)) => match e.name() {
|
||||
b"superblock" => visitor.superblock_b(&parse_superblock(e)?),
|
||||
b"device" => visitor.device_b(&parse_device(e)?),
|
||||
_ => todo!(),
|
||||
},
|
||||
Ok(Event::End(ref e)) => match e.name() {
|
||||
b"superblock" => visitor.superblock_e(),
|
||||
b"device" => visitor.device_e(),
|
||||
_ => todo!(),
|
||||
},
|
||||
Ok(Event::Empty(ref e)) => match e.name() {
|
||||
b"single_mapping" => visitor.map(&parse_single_map(e)?),
|
||||
b"range_mapping" => visitor.map(&parse_range_map(e)?),
|
||||
_ => todo!(),
|
||||
},
|
||||
Ok(Event::Text(_)) => Ok(Visit::Continue),
|
||||
Ok(Event::Comment(_)) => Ok(Visit::Continue),
|
||||
Ok(Event::Eof) => {
|
||||
visitor.eof()?;
|
||||
Ok(Visit::Stop)
|
||||
}
|
||||
Ok(_) => todo!(),
|
||||
|
||||
// FIXME: don't panic!
|
||||
Err(e) => panic!("error parsing xml {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read<R, M>(input: R, visitor: &mut M) -> Result<()>
|
||||
where
|
||||
R: Read,
|
||||
M: MetadataVisitor,
|
||||
{
|
||||
let input = BufReader::new(input);
|
||||
let mut reader = Reader::from_reader(input);
|
||||
|
||||
reader.trim_text(true);
|
||||
let mut buf = Vec::new();
|
||||
|
||||
loop {
|
||||
match handle_event(&mut reader, &mut buf, visitor)? {
|
||||
Visit::Continue => {}
|
||||
Visit::Stop => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
|
||||
struct SBVisitor {
|
||||
superblock: Option<Superblock>,
|
||||
}
|
||||
|
||||
impl MetadataVisitor for SBVisitor {
|
||||
fn superblock_b(&mut self, sb: &Superblock) -> Result<Visit> {
|
||||
self.superblock = Some(sb.clone());
|
||||
Ok(Visit::Stop)
|
||||
}
|
||||
|
||||
fn superblock_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_b(&mut self, _d: &Device) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
fn device_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn map(&mut self, _m: &Map) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Stop)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_superblock<R>(input: R) -> Result<Superblock>
|
||||
where
|
||||
R: Read,
|
||||
{
|
||||
let mut v = SBVisitor {superblock: None};
|
||||
read(input, &mut v)?;
|
||||
Ok(v.superblock.unwrap())
|
||||
}
|
||||
|
||||
//---------------------------------------
|
934
tests/thin_shrink.rs
Normal file
934
tests/thin_shrink.rs
Normal file
@ -0,0 +1,934 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use rand::prelude::*;
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
|
||||
use std::ops::Range;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tempfile::tempdir;
|
||||
|
||||
use thinp::file_utils;
|
||||
use thinp::thin::xml::{self, Visit};
|
||||
|
||||
//------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ThinBlock {
|
||||
thin_id: u32,
|
||||
thin_block: u64,
|
||||
data_block: u64,
|
||||
block_size: usize,
|
||||
}
|
||||
|
||||
struct ThinReadRef {
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
struct ThinWriteRef<'a, W: Write + Seek> {
|
||||
file: &'a mut W,
|
||||
block_byte: u64,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl ThinBlock {
|
||||
fn read_ref<R: Read + Seek>(&self, r: &mut R) -> Result<ThinReadRef> {
|
||||
let mut rr = ThinReadRef {
|
||||
data: vec![0; self.block_size * 512],
|
||||
};
|
||||
let byte = self.data_block * (self.block_size as u64) * 512;
|
||||
r.seek(SeekFrom::Start(byte))?;
|
||||
r.read_exact(&mut rr.data)?;
|
||||
Ok(rr)
|
||||
}
|
||||
|
||||
fn zero_ref<'a, W: Write + Seek>(&self, w: &'a mut W) -> ThinWriteRef<'a, W> {
|
||||
ThinWriteRef {
|
||||
file: w,
|
||||
block_byte: self.data_block * (self.block_size as u64) * 512,
|
||||
data: vec![0; self.block_size * 512],
|
||||
}
|
||||
}
|
||||
|
||||
//fn write_ref<'a, W>(&self, w: &'a mut W) -> Result<ThinWriteRef<'a, W>>
|
||||
//where
|
||||
//W: Read + Write + Seek,
|
||||
//{
|
||||
//let mut data = vec![0; self.block_size];
|
||||
//w.seek(SeekFrom::Start(self.data_block * (self.block_size as u64)))?;
|
||||
//w.read_exact(&mut data[0..])?;
|
||||
//
|
||||
//let wr = ThinWriteRef {
|
||||
//file: w,
|
||||
//block_byte: self.data_block * (self.block_size as u64),
|
||||
//data: vec![0; self.block_size],
|
||||
//};
|
||||
//
|
||||
//Ok(wr)
|
||||
//}
|
||||
}
|
||||
|
||||
impl<'a, W: Write + Seek> Drop for ThinWriteRef<'a, W> {
|
||||
fn drop(&mut self) {
|
||||
// FIXME: We shouldn't panic in a drop function, so any IO
|
||||
// errors will have to make their way back to the user
|
||||
// another way (eg, via a flush() method).
|
||||
self.file.seek(SeekFrom::Start(self.block_byte)).unwrap();
|
||||
self.file.write_all(&self.data).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
trait ThinVisitor {
|
||||
fn thin_block(&mut self, tb: &ThinBlock) -> Result<()>;
|
||||
}
|
||||
|
||||
struct ThinXmlVisitor<'a, V: ThinVisitor> {
|
||||
inner: &'a mut V,
|
||||
block_size: Option<u32>,
|
||||
thin_id: Option<u32>,
|
||||
}
|
||||
|
||||
impl<'a, V: ThinVisitor> xml::MetadataVisitor for ThinXmlVisitor<'a, V> {
|
||||
fn superblock_b(&mut self, sb: &xml::Superblock) -> Result<Visit> {
|
||||
self.block_size = Some(sb.data_block_size);
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn superblock_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_b(&mut self, d: &xml::Device) -> Result<Visit> {
|
||||
self.thin_id = Some(d.dev_id);
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn device_e(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn map(&mut self, m: &xml::Map) -> Result<Visit> {
|
||||
for i in 0..m.len {
|
||||
let block = ThinBlock {
|
||||
thin_id: self.thin_id.unwrap(),
|
||||
thin_block: m.thin_begin + i,
|
||||
data_block: m.data_begin + i,
|
||||
block_size: self.block_size.unwrap() as usize,
|
||||
};
|
||||
self.inner.thin_block(&block)?;
|
||||
}
|
||||
Ok(Visit::Continue)
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<Visit> {
|
||||
Ok(Visit::Stop)
|
||||
}
|
||||
}
|
||||
|
||||
fn thin_visit<R, M>(input: R, visitor: &mut M) -> Result<()>
|
||||
where
|
||||
R: Read,
|
||||
M: ThinVisitor,
|
||||
{
|
||||
let mut xml_visitor = ThinXmlVisitor {
|
||||
inner: visitor,
|
||||
block_size: None,
|
||||
thin_id: None,
|
||||
};
|
||||
|
||||
xml::read(input, &mut xml_visitor)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
// To test thin_shrink we'd like to stamp a known pattern across the
|
||||
// provisioned areas of the thins in the pool, do the shrink, verify
|
||||
// the patterns.
|
||||
|
||||
// A simple linear congruence generator used to create the data to
|
||||
// go into the thin blocks.
|
||||
struct Generator {
|
||||
x: u64,
|
||||
a: u64,
|
||||
c: u64,
|
||||
}
|
||||
|
||||
impl Generator {
|
||||
fn new() -> Generator {
|
||||
Generator {
|
||||
x: 0,
|
||||
a: 6364136223846793005,
|
||||
c: 1442695040888963407,
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) {
|
||||
self.x = self.a.wrapping_mul(self.x).wrapping_add(self.c)
|
||||
}
|
||||
|
||||
fn fill_buffer(&mut self, seed: u64, bytes: &mut [u8]) -> Result<()> {
|
||||
self.x = seed;
|
||||
|
||||
assert!(bytes.len() % 8 == 0);
|
||||
let nr_words = bytes.len() / 8;
|
||||
let mut out = Cursor::new(bytes);
|
||||
|
||||
for _ in 0..nr_words {
|
||||
out.write_u64::<LittleEndian>(self.x)?;
|
||||
self.step();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_buffer(&mut self, seed: u64, bytes: &[u8]) -> Result<bool> {
|
||||
self.x = seed;
|
||||
|
||||
assert!(bytes.len() % 8 == 0);
|
||||
let nr_words = bytes.len() / 8;
|
||||
let mut input = Cursor::new(bytes);
|
||||
|
||||
for _ in 0..nr_words {
|
||||
let w = input.read_u64::<LittleEndian>()?;
|
||||
if w != self.x {
|
||||
eprintln!("{} != {}", w, self.x);
|
||||
return Ok(false);
|
||||
}
|
||||
self.step();
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
struct Stamper<'a, W: Write + Seek> {
|
||||
data_file: &'a mut W,
|
||||
seed: u64,
|
||||
}
|
||||
|
||||
impl<'a, W: Write + Seek> Stamper<'a, W> {
|
||||
fn new(w: &'a mut W, seed: u64) -> Stamper<'a, W> {
|
||||
Stamper { data_file: w, seed }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W: Write + Seek> ThinVisitor for Stamper<'a, W> {
|
||||
fn thin_block(&mut self, b: &ThinBlock) -> Result<()> {
|
||||
let mut wr = b.zero_ref(self.data_file);
|
||||
let mut gen = Generator::new();
|
||||
gen.fill_buffer(self.seed ^ (b.thin_id as u64) ^ b.thin_block, &mut wr.data)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
struct Verifier<'a, R: Read + Seek> {
|
||||
data_file: &'a mut R,
|
||||
seed: u64,
|
||||
}
|
||||
|
||||
impl<'a, R: Read + Seek> Verifier<'a, R> {
|
||||
fn new(r: &'a mut R, seed: u64) -> Verifier<'a, R> {
|
||||
Verifier { data_file: r, seed }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: Read + Seek> ThinVisitor for Verifier<'a, R> {
|
||||
fn thin_block(&mut self, b: &ThinBlock) -> Result<()> {
|
||||
let rr = b.read_ref(self.data_file)?;
|
||||
let mut gen = Generator::new();
|
||||
if !gen.verify_buffer(self.seed ^ (b.thin_id as u64) ^ b.thin_block, &rr.data)? {
|
||||
return Err(anyhow!("data verify failed for {:?}", b));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
fn mk_path(dir: &Path, file: &str) -> PathBuf {
|
||||
let mut p = PathBuf::new();
|
||||
p.push(dir);
|
||||
p.push(PathBuf::from(file));
|
||||
p
|
||||
}
|
||||
|
||||
fn generate_xml(path: &Path, g: &mut dyn Scenario) -> Result<()> {
|
||||
let xml_out = OpenOptions::new()
|
||||
.read(false)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(path)?;
|
||||
let mut w = xml::XmlWriter::new(xml_out);
|
||||
|
||||
g.generate_xml(&mut w)
|
||||
}
|
||||
|
||||
fn create_data_file(data_path: &Path, xml_path: &Path) -> Result<()> {
|
||||
let input = OpenOptions::new().read(true).write(false).open(xml_path)?;
|
||||
|
||||
let sb = xml::read_superblock(input)?;
|
||||
let nr_blocks = sb.nr_data_blocks as u64;
|
||||
let block_size = sb.data_block_size as u64 * 512;
|
||||
|
||||
let _file = file_utils::create_sized_file(data_path, nr_blocks * block_size)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stamp(xml_path: &Path, data_path: &Path, seed: u64) -> Result<()> {
|
||||
let mut data = OpenOptions::new()
|
||||
.read(false)
|
||||
.write(true)
|
||||
.open(&data_path)?;
|
||||
let xml = OpenOptions::new().read(true).write(false).open(&xml_path)?;
|
||||
|
||||
let mut stamper = Stamper::new(&mut data, seed);
|
||||
thin_visit(xml, &mut stamper)
|
||||
}
|
||||
|
||||
fn verify(xml_path: &Path, data_path: &Path, seed: u64) -> Result<()> {
|
||||
let mut data = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(false)
|
||||
.open(&data_path)?;
|
||||
let xml = OpenOptions::new().read(true).write(false).open(&xml_path)?;
|
||||
|
||||
let mut verifier = Verifier::new(&mut data, seed);
|
||||
thin_visit(xml, &mut verifier)
|
||||
}
|
||||
|
||||
trait Scenario {
|
||||
fn generate_xml(&mut self, v: &mut dyn xml::MetadataVisitor) -> Result<()>;
|
||||
fn get_new_nr_blocks(&self) -> u64;
|
||||
}
|
||||
|
||||
fn test_shrink(scenario: &mut dyn Scenario) -> Result<()> {
|
||||
let dir = tempdir()?;
|
||||
let xml_before = mk_path(dir.path(), "before.xml");
|
||||
let xml_after = mk_path(dir.path(), "after.xml");
|
||||
let data_path = mk_path(dir.path(), "metadata.bin");
|
||||
|
||||
generate_xml(&xml_before, scenario)?;
|
||||
create_data_file(&data_path, &xml_before)?;
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let seed = rng.gen::<u64>();
|
||||
|
||||
stamp(&xml_before, &data_path, seed)?;
|
||||
verify(&xml_before, &data_path, seed)?;
|
||||
|
||||
let new_nr_blocks = scenario.get_new_nr_blocks();
|
||||
thinp::shrink::toplevel::shrink(&xml_before, &xml_after, &data_path, new_nr_blocks, true)?;
|
||||
|
||||
verify(&xml_after, &data_path, seed)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
fn common_sb(nr_blocks: u64) -> xml::Superblock {
|
||||
xml::Superblock {
|
||||
uuid: "".to_string(),
|
||||
time: 0,
|
||||
transaction: 0,
|
||||
flags: None,
|
||||
version: None,
|
||||
data_block_size: 32,
|
||||
nr_data_blocks: nr_blocks,
|
||||
metadata_snap: None,
|
||||
}
|
||||
}
|
||||
|
||||
struct EmptyPoolS {}
|
||||
|
||||
impl Scenario for EmptyPoolS {
|
||||
fn generate_xml(&mut self, v: &mut dyn xml::MetadataVisitor) -> Result<()> {
|
||||
v.superblock_b(&common_sb(1024))?;
|
||||
v.superblock_e()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_new_nr_blocks(&self) -> u64 {
|
||||
512
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_empty_pool() -> Result<()> {
|
||||
let mut s = EmptyPoolS {};
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
struct SingleThinS {
|
||||
offset: u64,
|
||||
len: u64,
|
||||
old_nr_data_blocks: u64,
|
||||
new_nr_data_blocks: u64,
|
||||
}
|
||||
|
||||
impl SingleThinS {
|
||||
fn new(offset: u64, len: u64, old_nr_data_blocks: u64, new_nr_data_blocks: u64) -> Self {
|
||||
SingleThinS {
|
||||
offset,
|
||||
len,
|
||||
old_nr_data_blocks,
|
||||
new_nr_data_blocks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Scenario for SingleThinS {
|
||||
fn generate_xml(&mut self, v: &mut dyn xml::MetadataVisitor) -> Result<()> {
|
||||
v.superblock_b(&common_sb(self.old_nr_data_blocks))?;
|
||||
v.device_b(&xml::Device {
|
||||
dev_id: 0,
|
||||
mapped_blocks: self.len,
|
||||
transaction: 0,
|
||||
creation_time: 0,
|
||||
snap_time: 0,
|
||||
})?;
|
||||
v.map(&xml::Map {
|
||||
thin_begin: 0,
|
||||
data_begin: self.offset,
|
||||
time: 0,
|
||||
len: self.len,
|
||||
})?;
|
||||
v.device_e()?;
|
||||
v.superblock_e()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_new_nr_blocks(&self) -> u64 {
|
||||
self.new_nr_data_blocks
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_single_no_move_1() -> Result<()> {
|
||||
let mut s = SingleThinS::new(0, 1024, 2048, 1280);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_single_no_move_2() -> Result<()> {
|
||||
let mut s = SingleThinS::new(100, 1024, 2048, 1280);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_single_no_move_3() -> Result<()> {
|
||||
let mut s = SingleThinS::new(1024, 1024, 2048, 2048);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_single_partial_move() -> Result<()> {
|
||||
let mut s = SingleThinS::new(1024, 1024, 2048, 1280);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_single_total_move() -> Result<()> {
|
||||
let mut s = SingleThinS::new(2048, 1024, 1024 + 2048, 1280);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_insufficient_space() -> Result<()> {
|
||||
let mut s = SingleThinS::new(0, 2048, 3000, 1280);
|
||||
match test_shrink(&mut s) {
|
||||
Ok(_) => Err(anyhow!("Shrink unexpectedly succeeded")),
|
||||
Err(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
struct FragmentedS {
|
||||
nr_thins: u32,
|
||||
thin_size: u64,
|
||||
old_nr_data_blocks: u64,
|
||||
new_nr_data_blocks: u64,
|
||||
}
|
||||
|
||||
impl FragmentedS {
|
||||
fn new(nr_thins: u32, thin_size: u64) -> Self {
|
||||
let old_size = (nr_thins as u64) * thin_size;
|
||||
FragmentedS {
|
||||
nr_thins,
|
||||
thin_size,
|
||||
old_nr_data_blocks: (nr_thins as u64) * thin_size,
|
||||
new_nr_data_blocks: old_size * 3 / 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ThinRun {
|
||||
thin_id: u32,
|
||||
thin_begin: u64,
|
||||
len: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy)]
|
||||
struct MappedRun {
|
||||
thin_id: u32,
|
||||
thin_begin: u64,
|
||||
data_begin: u64,
|
||||
len: u64,
|
||||
}
|
||||
|
||||
fn mk_runs(thin_id: u32, total_len: u64, run_len: std::ops::Range<u64>) -> Vec<ThinRun> {
|
||||
let mut runs = Vec::new();
|
||||
let mut b = 0u64;
|
||||
while b < total_len {
|
||||
let len = u64::min(
|
||||
total_len - b,
|
||||
thread_rng().gen_range(run_len.start, run_len.end),
|
||||
);
|
||||
runs.push(ThinRun {
|
||||
thin_id: thin_id,
|
||||
thin_begin: b,
|
||||
len,
|
||||
});
|
||||
b += len;
|
||||
}
|
||||
runs
|
||||
}
|
||||
|
||||
impl Scenario for FragmentedS {
|
||||
fn generate_xml(&mut self, v: &mut dyn xml::MetadataVisitor) -> Result<()> {
|
||||
// Allocate each thin fully, in runs between 1 and 16.
|
||||
let mut runs = Vec::new();
|
||||
for thin in 0..self.nr_thins {
|
||||
runs.append(&mut mk_runs(thin, self.thin_size, 1..17));
|
||||
}
|
||||
|
||||
// Shuffle
|
||||
runs.shuffle(&mut rand::thread_rng());
|
||||
|
||||
// map across the data
|
||||
let mut maps = Vec::new();
|
||||
let mut b = 0;
|
||||
for r in &runs {
|
||||
maps.push(MappedRun {
|
||||
thin_id: r.thin_id,
|
||||
thin_begin: r.thin_begin,
|
||||
data_begin: b,
|
||||
len: r.len,
|
||||
});
|
||||
b += r.len;
|
||||
}
|
||||
|
||||
// drop half the mappings, which leaves us free runs
|
||||
let mut dropped = Vec::new();
|
||||
for i in 0..maps.len() {
|
||||
if i % 2 == 0 {
|
||||
dropped.push(maps[i].clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Unshuffle. This isn't strictly necc. but makes the xml
|
||||
// more readable.
|
||||
use std::cmp::Ordering;
|
||||
maps.sort_by(|&l, &r| match l.thin_id.cmp(&r.thin_id) {
|
||||
Ordering::Equal => l.thin_begin.cmp(&r.thin_begin),
|
||||
o => o,
|
||||
});
|
||||
|
||||
// write the xml
|
||||
v.superblock_b(&common_sb(self.old_nr_data_blocks))?;
|
||||
for thin in 0..self.nr_thins {
|
||||
v.device_b(&xml::Device {
|
||||
dev_id: thin,
|
||||
mapped_blocks: self.thin_size,
|
||||
transaction: 0,
|
||||
creation_time: 0,
|
||||
snap_time: 0,
|
||||
})?;
|
||||
|
||||
for m in &dropped {
|
||||
if m.thin_id != thin {
|
||||
continue;
|
||||
}
|
||||
|
||||
v.map(&xml::Map {
|
||||
thin_begin: m.thin_begin,
|
||||
data_begin: m.data_begin,
|
||||
time: 0,
|
||||
len: m.len,
|
||||
})?;
|
||||
}
|
||||
|
||||
v.device_e()?;
|
||||
}
|
||||
v.superblock_e()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_new_nr_blocks(&self) -> u64 {
|
||||
self.new_nr_data_blocks
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_fragmented_thin_1() -> Result<()> {
|
||||
let mut s = FragmentedS::new(1, 2048);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_fragmented_thin_2() -> Result<()> {
|
||||
let mut s = FragmentedS::new(2, 2048);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_fragmented_thin_8() -> Result<()> {
|
||||
let mut s = FragmentedS::new(2, 2048);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_fragmented_thin_64() -> Result<()> {
|
||||
let mut s = FragmentedS::new(2, 2048);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
struct Allocator {
|
||||
runs: VecDeque<Range<u64>>,
|
||||
}
|
||||
|
||||
impl Allocator {
|
||||
fn new_shuffled(total_len: u64, run_len: Range<u64>) -> Allocator {
|
||||
let mut runs = Vec::new();
|
||||
|
||||
let mut b = 0u64;
|
||||
while b < total_len {
|
||||
let len = u64::min(
|
||||
total_len - b,
|
||||
thread_rng().gen_range(run_len.start, run_len.end),
|
||||
);
|
||||
runs.push(b..(b + len));
|
||||
b += len;
|
||||
}
|
||||
|
||||
runs.shuffle(&mut thread_rng());
|
||||
let runs: VecDeque<Range<u64>> = runs.iter().map(|r| r.clone()).collect();
|
||||
Allocator { runs }
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.runs.is_empty()
|
||||
}
|
||||
|
||||
fn alloc(&mut self, len: u64) -> Result<Vec<Range<u64>>> {
|
||||
let mut len = len;
|
||||
let mut runs = Vec::new();
|
||||
|
||||
while len > 0 {
|
||||
let r = self.runs.pop_front();
|
||||
|
||||
if r.is_none() {
|
||||
return Err(anyhow!("could not allocate; out of space"));
|
||||
}
|
||||
|
||||
let mut r = r.unwrap();
|
||||
let rlen = r.end - r.start;
|
||||
if len < rlen {
|
||||
runs.push(r.start..(r.start + len));
|
||||
|
||||
// We need to push something back.
|
||||
self.runs.push_front((r.start + len)..r.end);
|
||||
len = 0;
|
||||
} else {
|
||||
runs.push(r.start..r.end);
|
||||
len -= rlen;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(runs)
|
||||
}
|
||||
}
|
||||
|
||||
// Having explicitly unmapped regions makes it easier to
|
||||
// apply snapshots.
|
||||
#[derive(Clone)]
|
||||
enum Run {
|
||||
Mapped { data_begin: u64, len: u64 },
|
||||
UnMapped { len: u64 },
|
||||
}
|
||||
|
||||
impl Run {
|
||||
fn len(&self) -> u64 {
|
||||
match self {
|
||||
Run::Mapped {
|
||||
data_begin: _data_begin,
|
||||
len,
|
||||
} => *len,
|
||||
Run::UnMapped { len } => *len,
|
||||
}
|
||||
}
|
||||
|
||||
fn split(&self, n: u64) -> (Option<Run>, Option<Run>) {
|
||||
if n == 0 {
|
||||
return (None, Some(self.clone()));
|
||||
} else {
|
||||
if self.len() <= n {
|
||||
return (Some(self.clone()), None);
|
||||
} else {
|
||||
match self {
|
||||
Run::Mapped { data_begin, len } => (
|
||||
Some(Run::Mapped {
|
||||
data_begin: *data_begin,
|
||||
len: n,
|
||||
}),
|
||||
Some(Run::Mapped {
|
||||
data_begin: data_begin + n,
|
||||
len: len - n,
|
||||
}),
|
||||
),
|
||||
Run::UnMapped { len } => (
|
||||
Some(Run::UnMapped { len: n }),
|
||||
Some(Run::UnMapped { len: len - n }),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ThinDev {
|
||||
thin_id: u32,
|
||||
dev_size: u64,
|
||||
runs: Vec<Run>,
|
||||
}
|
||||
|
||||
impl ThinDev {
|
||||
fn emit(&self, v: &mut dyn xml::MetadataVisitor) -> Result<()> {
|
||||
v.device_b(&xml::Device {
|
||||
dev_id: self.thin_id,
|
||||
mapped_blocks: self.dev_size,
|
||||
transaction: 0,
|
||||
creation_time: 0,
|
||||
snap_time: 0,
|
||||
})?;
|
||||
|
||||
let mut b = 0;
|
||||
for r in &self.runs {
|
||||
match r {
|
||||
Run::Mapped { data_begin, len } => {
|
||||
v.map(&xml::Map {
|
||||
thin_begin: b,
|
||||
data_begin: *data_begin,
|
||||
time: 0,
|
||||
len: *len,
|
||||
})?;
|
||||
b += len;
|
||||
}
|
||||
Run::UnMapped { len } => {
|
||||
b += len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
v.device_e()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum SnapRunType {
|
||||
Same,
|
||||
Diff,
|
||||
Hole,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SnapRun(SnapRunType, u64);
|
||||
|
||||
fn mk_origin(thin_id: u32, total_len: u64, allocator: &mut Allocator) -> Result<ThinDev> {
|
||||
let mut runs = Vec::new();
|
||||
let mut b = 0;
|
||||
while b < total_len {
|
||||
let len = u64::min(thread_rng().gen_range(16, 64), total_len - b);
|
||||
match thread_rng().gen_range(0, 2) {
|
||||
0 => {
|
||||
for data in allocator.alloc(len)? {
|
||||
assert!(data.end >= data.start);
|
||||
runs.push(Run::Mapped {
|
||||
data_begin: data.start,
|
||||
len: data.end - data.start,
|
||||
});
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
runs.push(Run::UnMapped { len });
|
||||
}
|
||||
_ => {
|
||||
return Err(anyhow!("bad value returned from rng"));
|
||||
}
|
||||
};
|
||||
|
||||
b += len;
|
||||
}
|
||||
|
||||
Ok(ThinDev {
|
||||
thin_id,
|
||||
dev_size: total_len,
|
||||
runs,
|
||||
})
|
||||
}
|
||||
|
||||
fn mk_snap_mapping(
|
||||
total_len: u64,
|
||||
run_len: Range<u64>,
|
||||
same_percent: usize,
|
||||
diff_percent: usize,
|
||||
) -> Vec<SnapRun> {
|
||||
let mut runs = Vec::new();
|
||||
|
||||
let mut b = 0u64;
|
||||
while b < total_len {
|
||||
let len = u64::min(
|
||||
total_len - b,
|
||||
thread_rng().gen_range(run_len.start, run_len.end),
|
||||
);
|
||||
|
||||
let n = thread_rng().gen_range(0, 100);
|
||||
|
||||
if n < same_percent {
|
||||
runs.push(SnapRun(SnapRunType::Same, len));
|
||||
} else if n < diff_percent {
|
||||
runs.push(SnapRun(SnapRunType::Diff, len));
|
||||
} else {
|
||||
runs.push(SnapRun(SnapRunType::Hole, len));
|
||||
}
|
||||
|
||||
b += len;
|
||||
}
|
||||
|
||||
runs
|
||||
}
|
||||
|
||||
fn split_runs(mut n: u64, runs: &Vec<Run>) -> (Vec<Run>, Vec<Run>) {
|
||||
let mut before = Vec::new();
|
||||
let mut after = Vec::new();
|
||||
|
||||
for r in runs {
|
||||
match r.split(n) {
|
||||
(Some(lhs), None) => {
|
||||
before.push(lhs);
|
||||
}
|
||||
(Some(lhs), Some(rhs)) => {
|
||||
before.push(lhs);
|
||||
after.push(rhs);
|
||||
}
|
||||
(None, Some(rhs)) => {
|
||||
after.push(rhs);
|
||||
}
|
||||
(None, None) => {}
|
||||
}
|
||||
n -= r.len();
|
||||
}
|
||||
|
||||
(before, after)
|
||||
}
|
||||
|
||||
fn apply_snap_runs(
|
||||
origin: &Vec<Run>,
|
||||
snap: &Vec<SnapRun>,
|
||||
allocator: &mut Allocator,
|
||||
) -> Result<Vec<Run>> {
|
||||
let mut origin = origin.clone();
|
||||
let mut runs = Vec::new();
|
||||
|
||||
for SnapRun(st, slen) in snap {
|
||||
let (os, rest) = split_runs(*slen, &origin);
|
||||
match st {
|
||||
SnapRunType::Same => {
|
||||
for o in os {
|
||||
runs.push(o);
|
||||
}
|
||||
}
|
||||
SnapRunType::Diff => {
|
||||
for data in allocator.alloc(*slen)? {
|
||||
runs.push(Run::Mapped {
|
||||
data_begin: data.start,
|
||||
len: data.end - data.start,
|
||||
});
|
||||
}
|
||||
}
|
||||
SnapRunType::Hole => {
|
||||
runs.push(Run::UnMapped { len: *slen });
|
||||
}
|
||||
}
|
||||
|
||||
origin = rest;
|
||||
}
|
||||
|
||||
Ok(runs)
|
||||
}
|
||||
|
||||
// Snapshots share mappings, not neccessarily the entire ranges.
|
||||
struct SnapS {
|
||||
len: u64,
|
||||
nr_snaps: u32,
|
||||
|
||||
// Snaps will differ from the origin by this percentage
|
||||
percent_change: usize,
|
||||
old_nr_data_blocks: u64,
|
||||
new_nr_data_blocks: u64,
|
||||
}
|
||||
|
||||
impl SnapS {
|
||||
fn new(len: u64, nr_snaps: u32, percent_change: usize) -> Self {
|
||||
let delta = len * (nr_snaps as u64) * (percent_change as u64) / 100;
|
||||
let old_nr_data_blocks = len + 3 * delta;
|
||||
let new_nr_data_blocks = len + 2 * delta;
|
||||
|
||||
SnapS {
|
||||
len,
|
||||
nr_snaps,
|
||||
percent_change,
|
||||
old_nr_data_blocks,
|
||||
new_nr_data_blocks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Scenario for SnapS {
|
||||
fn generate_xml(&mut self, v: &mut dyn xml::MetadataVisitor) -> Result<()> {
|
||||
let mut allocator = Allocator::new_shuffled(self.old_nr_data_blocks, 64..512);
|
||||
let origin = mk_origin(0, self.len, &mut allocator)?;
|
||||
|
||||
v.superblock_b(&common_sb(self.old_nr_data_blocks))?;
|
||||
origin.emit(v)?;
|
||||
v.superblock_e()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_new_nr_blocks(&self) -> u64 {
|
||||
self.new_nr_data_blocks
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shrink_identical_snap() -> Result<()> {
|
||||
let mut s = SnapS::new(1024, 1, 0);
|
||||
test_shrink(&mut s)
|
||||
}
|
||||
|
||||
//------------------------------------
|
Loading…
Reference in New Issue
Block a user