1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::fmt;
use std::path::PathBuf;
use std::process::Command;
use super::consts::IEC;
use super::device::Device;
use super::deviceinfo::DeviceInfo;
use super::dm::{DM, DevId, DmFlags, DmName};
use super::lineardev::LinearDev;
use super::result::{DmResult, DmError, ErrorEnum};
use super::segment::Segment;
use super::shared::{DmDevice, device_create, device_exists, table_reload};
use super::types::{DataBlocks, MetaBlocks, Sectors, TargetLine};
#[cfg(test)]
use std::path::Path;
#[cfg(test)]
use super::loopbacked::devnode_to_devno;
#[allow(dead_code)]
const MIN_DATA_BLOCK_SIZE: Sectors = Sectors(128);
#[allow(dead_code)]
const MAX_DATA_BLOCK_SIZE: Sectors = Sectors(2 * IEC::Mi);
#[allow(dead_code)]
const MIN_RECOMMENDED_METADATA_SIZE: Sectors = Sectors(4 * IEC::Ki);
#[allow(dead_code)]
const MAX_RECOMMENDED_METADATA_SIZE: Sectors = Sectors(32 * IEC::Mi);
pub struct ThinPoolDev {
dev_info: Box<DeviceInfo>,
meta_dev: LinearDev,
data_dev: LinearDev,
data_block_size: Sectors,
low_water_mark: DataBlocks,
}
impl fmt::Debug for ThinPoolDev {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.name())
}
}
impl DmDevice for ThinPoolDev {
fn device(&self) -> Device {
device!(self)
}
fn devnode(&self) -> PathBuf {
devnode!(self)
}
fn name(&self) -> &DmName {
name!(self)
}
fn size(&self) -> Sectors {
self.data_dev.size()
}
fn teardown(self, dm: &DM) -> DmResult<()> {
dm.device_remove(&DevId::Name(self.name()), DmFlags::empty())?;
self.data_dev.teardown(dm)?;
self.meta_dev.teardown(dm)?;
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
pub struct ThinPoolBlockUsage {
pub used_meta: MetaBlocks,
pub total_meta: MetaBlocks,
pub used_data: DataBlocks,
pub total_data: DataBlocks,
}
#[derive(Debug, Clone, Copy)]
pub enum ThinPoolStatus {
Good(ThinPoolWorkingStatus, ThinPoolBlockUsage),
Fail,
}
#[derive(Debug, Clone, Copy)]
pub enum ThinPoolWorkingStatus {
Good,
ReadOnly,
OutOfSpace,
NeedsCheck,
}
impl ThinPoolDev {
pub fn new(name: &DmName,
dm: &DM,
data_block_size: Sectors,
low_water_mark: DataBlocks,
meta: LinearDev,
data: LinearDev)
-> DmResult<ThinPoolDev> {
let dev_info = if device_exists(dm, name)? {
let id = DevId::Name(name);
Box::new(dm.device_status(&id)?)
} else {
let table =
ThinPoolDev::dm_table(data.size(), data_block_size, low_water_mark, &meta, &data);
Box::new(device_create(dm, name, &table)?)
};
DM::wait_for_dm();
Ok(ThinPoolDev {
dev_info: dev_info,
meta_dev: meta,
data_dev: data,
data_block_size: data_block_size,
low_water_mark: low_water_mark,
})
}
pub fn meta_dev(&self) -> &LinearDev {
&self.meta_dev
}
pub fn data_dev(&self) -> &LinearDev {
&self.data_dev
}
pub fn data_block_size(&self) -> Sectors {
self.data_block_size
}
pub fn setup(name: &DmName,
dm: &DM,
data_block_size: Sectors,
low_water_mark: DataBlocks,
meta: LinearDev,
data: LinearDev)
-> DmResult<ThinPoolDev> {
if !device_exists(dm, name)? &&
!Command::new("thin_check")
.arg("-q")
.arg(&meta.devnode())
.status()?
.success() {
return Err(DmError::Dm(ErrorEnum::CheckFailed(meta, data),
"thin_check failed, run thin_repair".into()));
}
ThinPoolDev::new(name, dm, data_block_size, low_water_mark, meta, data)
}
fn dm_table(length: Sectors,
data_block_size: Sectors,
low_water_mark: DataBlocks,
meta: &LinearDev,
data: &LinearDev)
-> Vec<TargetLine> {
let params = format!("{} {} {} {} 1 skip_block_zeroing",
meta.device(),
data.device(),
*data_block_size,
*low_water_mark);
vec![(Sectors::default(), length, "thin-pool".to_owned(), params)]
}
pub fn message(&self, dm: &DM, message: &str) -> DmResult<()> {
dm.target_msg(&DevId::Name(self.name()), Sectors(0), message)?;
Ok(())
}
pub fn status(&self, dm: &DM) -> DmResult<ThinPoolStatus> {
let (_, mut status) = dm.table_status(&DevId::Name(self.name()), DmFlags::empty())?;
assert_eq!(status.len(),
1,
"Kernel must return 1 line from thin pool status");
let status_line = status.pop().expect("assertion above holds").3;
if status_line.starts_with("Fail") {
return Ok(ThinPoolStatus::Fail);
}
let status_vals = status_line.split(' ').collect::<Vec<_>>();
assert!(status_vals.len() >= 8,
"Kernel must return at least 8 values from thin pool status");
let usage = {
let meta_vals = status_vals[1].split('/').collect::<Vec<_>>();
let data_vals = status_vals[2].split('/').collect::<Vec<_>>();
ThinPoolBlockUsage {
used_meta: MetaBlocks(meta_vals[0]
.parse::<u64>()
.expect("used_meta value must be valid")),
total_meta: MetaBlocks(meta_vals[1]
.parse::<u64>()
.expect("total_meta value must be valid")),
used_data: DataBlocks(data_vals[0]
.parse::<u64>()
.expect("used_data value must be valid")),
total_data: DataBlocks(data_vals[1]
.parse::<u64>()
.expect("total_data value must be valid")),
}
};
match status_vals[7] {
"-" => {}
"needs_check" => {
return Ok(ThinPoolStatus::Good(ThinPoolWorkingStatus::NeedsCheck, usage))
}
_ => panic!("Kernel returned unexpected 8th value in thin pool status"),
}
match status_vals[4] {
"rw" => Ok(ThinPoolStatus::Good(ThinPoolWorkingStatus::Good, usage)),
"ro" => Ok(ThinPoolStatus::Good(ThinPoolWorkingStatus::ReadOnly, usage)),
"out_of_data_space" => {
Ok(ThinPoolStatus::Good(ThinPoolWorkingStatus::OutOfSpace, usage))
}
_ => panic!("Kernel returned unexpected 5th value in thin pool status"),
}
}
pub fn extend_meta(&mut self, dm: &DM, new_segs: Vec<Segment>) -> DmResult<()> {
self.meta_dev.extend(new_segs)?;
table_reload(dm,
&DevId::Name(self.name()),
&ThinPoolDev::dm_table(self.data_dev.size(),
self.data_block_size,
self.low_water_mark,
&self.meta_dev,
&self.data_dev))?;
Ok(())
}
pub fn extend_data(&mut self, dm: &DM, new_segs: Vec<Segment>) -> DmResult<()> {
self.data_dev.extend(new_segs)?;
table_reload(dm,
&DevId::Name(self.name()),
&ThinPoolDev::dm_table(self.data_dev.size(),
self.data_block_size,
self.low_water_mark,
&self.meta_dev,
&self.data_dev))?;
Ok(())
}
}
#[cfg(test)]
pub fn minimal_thinpool(dm: &DM, path: &Path) -> ThinPoolDev {
let dev = Device::from(devnode_to_devno(path).unwrap());
let meta = LinearDev::setup(DmName::new("meta").expect("valid format"),
dm,
vec![Segment::new(dev, Sectors(0), MIN_RECOMMENDED_METADATA_SIZE)])
.unwrap();
let data = LinearDev::setup(DmName::new("data").expect("valid format"),
dm,
vec![Segment::new(dev,
MIN_RECOMMENDED_METADATA_SIZE,
MIN_DATA_BLOCK_SIZE)])
.unwrap();
ThinPoolDev::new(DmName::new("pool").expect("valid format"),
dm,
MIN_DATA_BLOCK_SIZE,
DataBlocks(1),
meta,
data)
.unwrap()
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::super::loopbacked::test_with_spec;
use super::*;
fn test_minimum_values(paths: &[&Path]) -> () {
assert!(paths.len() >= 1);
let dm = DM::new().unwrap();
let tp = minimal_thinpool(&dm, paths[0]);
match tp.status(&dm).unwrap() {
ThinPoolStatus::Good(ThinPoolWorkingStatus::Good, tpbu) => {
assert!(tpbu.used_meta > MetaBlocks(0));
assert_eq!(tpbu.total_meta, tp.meta_dev().size().metablocks());
assert_eq!(tpbu.used_data, DataBlocks(0));
assert_eq!(tpbu.total_data,
DataBlocks(tp.data_dev().size() / tp.data_block_size()));
}
_ => assert!(false),
}
tp.teardown(&dm).unwrap();
}
#[test]
fn loop_test_basic() {
test_with_spec(1, test_minimum_values);
}
}