-
Notifications
You must be signed in to change notification settings - Fork 55
/
engine.rs
450 lines (383 loc) · 14.5 KB
/
engine.rs
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::{clone::Clone, collections::HashMap, convert::TryFrom, path::Path};
use serde_json::Value;
use devicemapper::DmNameBuf;
use crate::{
engine::{
engine::{Eventable, KeyActions},
event::get_engine_listener_list,
shared::create_pool_idempotent_or_err,
strat_engine::{
cmd::verify_binaries,
devlinks,
dm::{get_dm, get_dm_init},
keys::StratKeyActions,
liminal::{find_all, LiminalDevices},
names::{validate_name, KeyDescription},
pool::StratPool,
},
structures::Table,
types::{CreateAction, DeleteAction, DevUuid, RenameAction, ReportType, SetUnlockAction},
Engine, EngineEvent, Name, Pool, PoolUuid, Report,
},
stratis::{ErrorEnum, StratisError, StratisResult},
};
const REQUIRED_DM_MINOR_VERSION: u32 = 37;
#[derive(Debug)]
pub struct StratEngine {
pools: Table<StratPool>,
// Maps pool UUIDs to information about sets of devices that are
// associated with that UUID but have not been converted into a pool.
liminal_devices: LiminalDevices,
// Maps name of DM devices we are watching to the most recent event number
// we've handled for each
watched_dev_last_event_nrs: HashMap<DmNameBuf, u32>,
// Handler for key operations
key_handler: StratKeyActions,
}
impl StratEngine {
/// Setup a StratEngine.
/// 1. Verify the existence of Stratis /dev directory.
/// 2. Setup all the pools belonging to the engine.
/// a. Places any devices which belong to a pool, but are not complete
/// in the incomplete pools data structure.
///
/// Returns an error if the kernel doesn't support required DM features.
/// Returns an error if there was an error reading device nodes.
/// Returns an error if the binaries on which it depends can not be found.
pub fn initialize() -> StratisResult<StratEngine> {
let dm = get_dm_init()?;
verify_binaries()?;
let minor_dm_version = dm.version()?.1;
if minor_dm_version < REQUIRED_DM_MINOR_VERSION {
let err_msg = format!(
"Requires DM minor version {} but kernel only supports {}",
REQUIRED_DM_MINOR_VERSION, minor_dm_version
);
return Err(StratisError::Engine(ErrorEnum::Error, err_msg));
}
let mut liminal_devices = LiminalDevices::default();
let mut pools = Table::default();
for (pool_name, pool_uuid, pool) in liminal_devices.setup_pools(find_all()?) {
pools.insert(pool_name, pool_uuid, pool);
}
Ok(StratEngine {
pools,
liminal_devices,
watched_dev_last_event_nrs: HashMap::new(),
key_handler: StratKeyActions,
})
}
/// Recursively remove all devicemapper devices in all pools.
/// Do not remove the dm-crypt devices that comprise the backstore.
#[cfg(test)]
pub fn teardown(self) -> StratisResult<()> {
let mut untorndown_pools = Vec::new();
for (_, uuid, mut pool) in self.pools {
pool.teardown()
.unwrap_or_else(|_| untorndown_pools.push(uuid));
}
if untorndown_pools.is_empty() {
Ok(())
} else {
let err_msg = format!(
"Failed to teardown already set up pools: {:?}",
untorndown_pools
);
Err(StratisError::Engine(ErrorEnum::Error, err_msg))
}
}
}
impl<'a> Into<Value> for &'a StratEngine {
// Precondition: (&StratPool).into() pattern matches Value::Object(_)
// Precondition: (&LiminalDevices).into() pattern matches Value::Object(_)
fn into(self) -> Value {
let json = json!({
"pools": Value::Array(
self.pools.iter()
.map(|(name, uuid, pool)| {
let mut json = json!({
"uuid": Value::from(uuid.to_simple_ref().to_string()),
"name": Value::from(name.to_string()),
});
if let Value::Object(ref mut map) = json {
map.extend(
if let Value::Object(map) = <&StratPool as Into<Value>>::into(pool) {
map.into_iter()
} else {
unreachable!("StratPool conversion returns a JSON object");
}
);
} else {
unreachable!("json!() always creates a JSON object")
}
json
})
.collect()
),
});
if let (Value::Object(mut j), Value::Object(map)) = (
json,
<&LiminalDevices as Into<Value>>::into(&self.liminal_devices),
) {
j.extend(map.into_iter());
Value::Object(j)
} else {
unreachable!("json!() and LiminalDevices::into() always return JSON object");
}
}
}
impl Report for StratEngine {
fn get_report(&self, report_type: ReportType) -> Value {
match report_type {
ReportType::ErroredPoolDevices => (&self.liminal_devices).into(),
ReportType::EngineState => self.into(),
}
}
}
impl Engine for StratEngine {
fn handle_event(&mut self, event: &libudev::Event) -> Option<(Name, PoolUuid, &mut dyn Pool)> {
if let Some((pool_uuid, pool_name, pool)) =
self.liminal_devices.block_evaluate(&self.pools, event)
{
self.pools.insert(pool_name.clone(), pool_uuid, pool);
Some((
pool_name,
pool_uuid,
self.pools
.get_mut_by_uuid(pool_uuid)
.expect("just_inserted")
.1 as &mut dyn Pool,
))
} else {
None
}
}
fn create_pool(
&mut self,
name: &str,
blockdev_paths: &[&Path],
redundancy: Option<u16>,
key_desc: Option<String>,
) -> StratisResult<CreateAction<PoolUuid>> {
let redundancy = calculate_redundancy!(redundancy);
validate_name(name)?;
match self.pools.get_by_name(name) {
Some((_, pool)) => create_pool_idempotent_or_err(pool, name, blockdev_paths),
None => {
if blockdev_paths.is_empty() {
Err(StratisError::Engine(
ErrorEnum::Invalid,
"At least one blockdev is required to create a pool.".to_string(),
))
} else {
let key_description = match key_desc {
Some(desc) => Some(KeyDescription::try_from(desc)?),
None => None,
};
let (uuid, pool) = StratPool::initialize(
name,
blockdev_paths,
redundancy,
key_description.as_ref(),
)?;
let name = Name::new(name.to_owned());
self.pools.insert(name, uuid, pool);
Ok(CreateAction::Created(uuid))
}
}
}
}
fn destroy_pool(&mut self, uuid: PoolUuid) -> StratisResult<DeleteAction<PoolUuid>> {
if let Some((_, pool)) = self.pools.get_by_uuid(uuid) {
if pool.has_filesystems() {
return Err(StratisError::Engine(
ErrorEnum::Busy,
"filesystems remaining on pool".into(),
));
};
} else {
return Ok(DeleteAction::Identity);
}
let (pool_name, mut pool) = self
.pools
.remove_by_uuid(uuid)
.expect("Must succeed since self.pools.get_by_uuid() returned a value");
if let Err(err) = pool.destroy() {
self.pools.insert(pool_name, uuid, pool);
Err(err)
} else {
Ok(DeleteAction::Deleted(uuid))
}
}
fn rename_pool(
&mut self,
uuid: PoolUuid,
new_name: &str,
) -> StratisResult<RenameAction<PoolUuid>> {
validate_name(new_name)?;
let old_name = rename_pool_pre_idem!(self; uuid; new_name);
let (_, mut pool) = self
.pools
.remove_by_uuid(uuid)
.expect("Must succeed since self.pools.get_by_uuid() returned a value");
let new_name = Name::new(new_name.to_owned());
if let Err(err) = pool.write_metadata(&new_name) {
self.pools.insert(old_name, uuid, pool);
Err(err)
} else {
get_engine_listener_list().notify(&EngineEvent::PoolRenamed {
dbus_path: pool.get_dbus_path(),
from: &*old_name,
to: &*new_name,
});
self.pools.insert(new_name.clone(), uuid, pool);
if let Err(e) = devlinks::pool_renamed(&old_name) {
warn!("Pool rename symlink action failed: {}", e)
};
Ok(RenameAction::Renamed(uuid))
}
}
fn unlock_pool(&mut self, pool_uuid: PoolUuid) -> StratisResult<SetUnlockAction<DevUuid>> {
let unlocked = self.liminal_devices.unlock_pool(&self.pools, pool_uuid)?;
Ok(SetUnlockAction::new(unlocked))
}
fn get_pool(&self, uuid: PoolUuid) -> Option<(Name, &dyn Pool)> {
get_pool!(self; uuid)
}
fn get_mut_pool(&mut self, uuid: PoolUuid) -> Option<(Name, &mut dyn Pool)> {
get_mut_pool!(self; uuid)
}
fn locked_pools(&self) -> HashMap<PoolUuid, KeyDescription> {
self.liminal_devices.locked_pools()
}
fn configure_simulator(&mut self, _denominator: u32) -> StratisResult<()> {
Ok(()) // we're not the simulator and not configurable, so just say ok
}
fn pools(&self) -> Vec<(Name, PoolUuid, &dyn Pool)> {
self.pools
.iter()
.map(|(name, uuid, pool)| (name.clone(), *uuid, pool as &dyn Pool))
.collect()
}
fn pools_mut(&mut self) -> Vec<(Name, PoolUuid, &mut dyn Pool)> {
self.pools
.iter_mut()
.map(|(name, uuid, pool)| (name.clone(), *uuid, pool as &mut dyn Pool))
.collect()
}
fn get_eventable(&self) -> Option<&'static dyn Eventable> {
Some(get_dm())
}
fn evented(&mut self) -> StratisResult<()> {
let device_list: HashMap<_, _> = get_dm()
.list_devices()?
.into_iter()
.map(|(dm_name, _, event_nr)| {
(
dm_name,
event_nr.expect("Supported DM versions always provide a value"),
)
})
.collect();
for (pool_name, pool_uuid, pool) in &mut self.pools {
for dm_name in pool.get_eventing_dev_names(*pool_uuid) {
if device_list.get(&dm_name) > self.watched_dev_last_event_nrs.get(&dm_name) {
pool.event_on(*pool_uuid, pool_name, &dm_name)?;
}
}
}
self.watched_dev_last_event_nrs = device_list;
Ok(())
}
fn get_key_handler(&self) -> &dyn KeyActions {
&self.key_handler as &dyn KeyActions
}
fn get_key_handler_mut(&mut self) -> &mut dyn KeyActions {
&mut self.key_handler as &mut dyn KeyActions
}
}
#[cfg(test)]
mod test {
use crate::engine::strat_engine::tests::{loopbacked, real};
use crate::engine::types::EngineAction;
use super::*;
/// Verify that a pool rename causes the pool metadata to get the new name.
fn test_pool_rename(paths: &[&Path]) {
let mut engine = StratEngine::initialize().unwrap();
let name1 = "name1";
let uuid1 = engine
.create_pool(name1, paths, None, None)
.unwrap()
.changed()
.unwrap();
let name2 = "name2";
let action = engine.rename_pool(uuid1, name2).unwrap();
assert_eq!(action, RenameAction::Renamed(uuid1));
engine.teardown().unwrap();
let engine = StratEngine::initialize().unwrap();
let pool_name: String = engine.get_pool(uuid1).unwrap().0.to_owned();
assert_eq!(pool_name, name2);
}
#[test]
fn loop_test_pool_rename() {
loopbacked::test_with_spec(
&loopbacked::DeviceLimits::Range(1, 3, None),
test_pool_rename,
);
}
#[test]
fn real_test_pool_rename() {
real::test_with_spec(
&real::DeviceLimits::AtLeast(1, None, None),
test_pool_rename,
);
}
/// Test engine setup.
/// 1. Create two pools.
/// 2. Verify that both exist.
/// 3. Teardown the engine.
/// 4. Initialize the engine.
/// 5. Verify that pools can be found again.
/// 6. Teardown the engine
/// 7. Initialize the engine one more time.
/// 8. Verify that both pools are found.
fn test_setup(paths: &[&Path]) {
assert!(paths.len() > 1);
let (paths1, paths2) = paths.split_at(paths.len() / 2);
let mut engine = StratEngine::initialize().unwrap();
let name1 = "name1";
let uuid1 = engine
.create_pool(name1, paths1, None, None)
.unwrap()
.changed()
.unwrap();
let name2 = "name2";
let uuid2 = engine
.create_pool(name2, paths2, None, None)
.unwrap()
.changed()
.unwrap();
assert!(engine.get_pool(uuid1).is_some());
assert!(engine.get_pool(uuid2).is_some());
engine.teardown().unwrap();
let engine = StratEngine::initialize().unwrap();
assert!(engine.get_pool(uuid1).is_some());
assert!(engine.get_pool(uuid2).is_some());
engine.teardown().unwrap();
let engine = StratEngine::initialize().unwrap();
assert!(engine.get_pool(uuid1).is_some());
assert!(engine.get_pool(uuid2).is_some());
engine.teardown().unwrap();
}
#[test]
fn loop_test_setup() {
loopbacked::test_with_spec(&loopbacked::DeviceLimits::Range(2, 3, None), test_setup);
}
#[test]
fn real_test_setup() {
real::test_with_spec(&real::DeviceLimits::AtLeast(2, None, None), test_setup);
}
}