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
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use super::device::{Device, devnode_to_devno};
use super::deviceinfo::DeviceInfo;
use super::dm::{DM, DmFlags};
use super::result::{DmError, DmResult, ErrorEnum};
use super::types::{DevId, DmName, DmUuid, Sectors, TargetTypeBuf};
pub trait TargetParams
: Clone + fmt::Debug + fmt::Display + Eq + FromStr + PartialEq {
fn param_str(&self) -> String;
fn target_type(&self) -> TargetTypeBuf;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TargetLine<T: TargetParams> {
pub start: Sectors,
pub length: Sectors,
pub params: T,
}
impl<T: TargetParams> TargetLine<T> {
pub fn new(start: Sectors, length: Sectors, params: T) -> TargetLine<T> {
TargetLine {
start,
length,
params,
}
}
}
pub trait TargetTable: Clone + fmt::Debug + Eq + PartialEq + Sized {
fn from_raw_table(table: &[(Sectors, Sectors, TargetTypeBuf, String)]) -> DmResult<Self>;
fn to_raw_table(&self) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)>;
}
pub trait DmDevice<T: TargetTable> {
fn device(&self) -> Device;
fn devnode(&self) -> PathBuf;
fn equivalent_tables(left: &T, right: &T) -> DmResult<bool>;
fn load_table(dm: &DM, id: &DevId) -> DmResult<T> {
let (_, table) = dm.table_status(id, DmFlags::DM_STATUS_TABLE)?;
T::from_raw_table(&table)
}
fn name(&self) -> &DmName;
fn resume(&mut self, dm: &DM) -> DmResult<()> {
dm.device_suspend(&DevId::Name(self.name()), DmFlags::empty())?;
Ok(())
}
fn size(&self) -> Sectors;
fn suspend(&mut self, dm: &DM) -> DmResult<()> {
dm.device_suspend(&DevId::Name(self.name()), DmFlags::DM_SUSPEND)?;
Ok(())
}
fn table(&self) -> &T;
fn table_load(&self, dm: &DM, table: &T) -> DmResult<()> {
dm.table_load(&DevId::Name(self.name()), &table.to_raw_table())?;
Ok(())
}
fn teardown(self, dm: &DM) -> DmResult<()>;
fn uuid(&self) -> Option<&DmUuid>;
}
pub fn message<T: TargetTable, D: DmDevice<T>>(dm: &DM, target: &D, msg: &str) -> DmResult<()> {
dm.target_msg(&DevId::Name(target.name()), None, msg)?;
Ok(())
}
pub fn device_create<T: TargetTable>(dm: &DM,
name: &DmName,
uuid: Option<&DmUuid>,
table: &T)
-> DmResult<DeviceInfo> {
dm.device_create(name, uuid, DmFlags::empty())?;
let id = DevId::Name(name);
let dev_info = match dm.table_load(&id, &table.to_raw_table()) {
Err(e) => {
dm.device_remove(&id, DmFlags::empty())?;
return Err(e);
}
Ok(dev_info) => dev_info,
};
dm.device_suspend(&id, DmFlags::empty())?;
Ok(dev_info)
}
pub fn device_match<T: TargetTable, D: DmDevice<T>>(dm: &DM,
dev: &D,
uuid: Option<&DmUuid>)
-> DmResult<()> {
let kernel_table = D::load_table(dm, &DevId::Name(dev.name()))?;
let device_table = dev.table();
if !D::equivalent_tables(&kernel_table, device_table)? {
let err_msg = format!("Specified new table \"{:?}\" does not match kernel table \"{:?}\"",
device_table,
kernel_table);
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
}
if dev.uuid() != uuid {
let err_msg = format!("Specified uuid \"{:?}\" does not match kernel uuuid \"{:?}\"",
uuid,
dev.uuid());
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
}
Ok(())
}
pub fn device_exists(dm: &DM, name: &DmName) -> DmResult<bool> {
Ok(dm.list_devices()
.map(|l| l.iter().any(|&(ref n, _, _)| n.as_ref() == name))?)
}
pub fn parse_device(val: &str) -> DmResult<Device> {
let device = if val.starts_with('/') {
devnode_to_devno(Path::new(val))?
.ok_or_else(|| {
DmError::Dm(ErrorEnum::Invalid,
format!("failed to parse device number from \"{}\"", val))
})?
.into()
} else {
val.parse::<Device>()?
};
Ok(device)
}