Skip to content

Commit f5e11b3

Browse files
committed
Have dm methods return raw values instead of Sectors
For more rawness and better encapsulation. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent 5394d9d commit f5e11b3

12 files changed

Lines changed: 116 additions & 120 deletions

File tree

src/cachedev.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ use std::fmt;
77
use std::path::PathBuf;
88
use std::str::FromStr;
99

10-
use crate::core::{
11-
DataBlocks, DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, MetaBlocks, Sectors,
12-
TargetTypeBuf, DM,
13-
};
10+
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, TargetTypeBuf, DM};
1411
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1512
use crate::result::{DmError, DmResult, ErrorEnum};
1613
use crate::shared::{
1714
device_create, device_exists, device_match, get_status_line_fields,
1815
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
1916
TargetTable,
2017
};
18+
use crate::units::{DataBlocks, MetaBlocks, Sectors};
2119

2220
const CACHE_TARGET_NAME: &str = "cache";
2321

@@ -190,7 +188,7 @@ impl fmt::Display for CacheDevTargetTable {
190188

191189
impl TargetTable for CacheDevTargetTable {
192190
fn from_raw_table(
193-
table: &[(Sectors, Sectors, TargetTypeBuf, String)],
191+
table: &[(u64, u64, TargetTypeBuf, String)],
194192
) -> DmResult<CacheDevTargetTable> {
195193
if table.len() != 1 {
196194
let err_msg = format!(
@@ -201,13 +199,13 @@ impl TargetTable for CacheDevTargetTable {
201199
}
202200
let line = table.first().expect("table.len() == 1");
203201
Ok(CacheDevTargetTable::new(
204-
line.0,
205-
line.1,
202+
Sectors(line.0),
203+
Sectors(line.1),
206204
format!("{} {}", line.2.to_string(), line.3).parse::<CacheTargetParams>()?,
207205
))
208206
}
209207

210-
fn to_raw_table(&self) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)> {
208+
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
211209
to_raw_table_unique!(self)
212210
}
213211
}

src/core/dm.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use nix::libc::c_ulong;
1111
use nix::libc::ioctl as nix_ioctl;
1212

1313
use crate::core::{
14-
DevId, Device, DeviceInfo, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid, Sectors,
15-
TargetTypeBuf,
14+
DevId, Device, DeviceInfo, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid, TargetTypeBuf,
1615
};
1716
use crate::result::{DmError, DmResult};
1817

@@ -401,7 +400,7 @@ impl DM {
401400
&self,
402401
id: &DevId,
403402
options: &DmOptions,
404-
) -> DmResult<(DeviceInfo, Vec<(Sectors, Sectors, TargetTypeBuf, String)>)> {
403+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, TargetTypeBuf, String)>)> {
405404
let mut hdr = options.to_ioctl_hdr(Some(id), DmFlags::DM_QUERY_INACTIVE_TABLE);
406405

407406
let data_out = self.do_ioctl(dmi::DM_DEV_WAIT_CMD as u8, &mut hdr, None)?;
@@ -423,14 +422,14 @@ impl DM {
423422
/// # Example
424423
///
425424
/// ```no_run
426-
/// use devicemapper::{DM, DevId, DmName, Sectors, TargetTypeBuf};
425+
/// use devicemapper::{DM, DevId, DmName, TargetTypeBuf};
427426
/// let dm = DM::new().unwrap();
428427
///
429428
/// // Create a 16MiB device (32768 512-byte sectors) that maps to /dev/sdb1
430429
/// // starting 1MiB into sdb1
431430
/// let table = vec![(
432-
/// Sectors(0),
433-
/// Sectors(32768),
431+
/// 0,
432+
/// 32768,
434433
/// TargetTypeBuf::new("linear".into()).expect("valid"),
435434
/// "/dev/sdb1 2048".into()
436435
/// )];
@@ -442,16 +441,16 @@ impl DM {
442441
pub fn table_load(
443442
&self,
444443
id: &DevId,
445-
targets: &[(Sectors, Sectors, TargetTypeBuf, String)],
444+
targets: &[(u64, u64, TargetTypeBuf, String)],
446445
) -> DmResult<DeviceInfo> {
447446
let mut targs = Vec::new();
448447

449448
// Construct targets first, since we need to know how many & size
450449
// before initializing the header.
451450
for t in targets {
452451
let mut targ: dmi::Struct_dm_target_spec = Default::default();
453-
targ.sector_start = *t.0;
454-
targ.length = *t.1;
452+
targ.sector_start = t.0;
453+
targ.length = t.1;
455454
targ.status = 0;
456455

457456
let dst: &mut [u8] = unsafe { &mut *(&mut targ.target_type[..] as *mut [u8]) };
@@ -554,10 +553,7 @@ impl DM {
554553
// will either be backwards-compatible, or will increment
555554
// DM_VERSION_MAJOR. Since calls made with a non-matching major version
556555
// will fail, this protects against callers parsing unknown formats.
557-
fn parse_table_status(
558-
count: u32,
559-
buf: &[u8],
560-
) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)> {
556+
fn parse_table_status(count: u32, buf: &[u8]) -> Vec<(u64, u64, TargetTypeBuf, String)> {
561557
let mut targets = Vec::new();
562558
if !buf.is_empty() {
563559
let mut next_off = 0;
@@ -584,8 +580,8 @@ impl DM {
584580
};
585581

586582
targets.push((
587-
Sectors(targ.sector_start),
588-
Sectors(targ.length),
583+
targ.sector_start,
584+
targ.length,
589585
TargetTypeBuf::new(target_type).expect("< sizeof target_spec"),
590586
params,
591587
));
@@ -629,7 +625,7 @@ impl DM {
629625
&self,
630626
id: &DevId,
631627
options: &DmOptions,
632-
) -> DmResult<(DeviceInfo, Vec<(Sectors, Sectors, TargetTypeBuf, String)>)> {
628+
) -> DmResult<(DeviceInfo, Vec<(u64, u64, TargetTypeBuf, String)>)> {
633629
let mut hdr = options.to_ioctl_hdr(
634630
Some(id),
635631
DmFlags::DM_NOFLUSH | DmFlags::DM_STATUS_TABLE | DmFlags::DM_QUERY_INACTIVE_TABLE,
@@ -683,13 +679,13 @@ impl DM {
683679
pub fn target_msg(
684680
&self,
685681
id: &DevId,
686-
sector: Option<Sectors>,
682+
sector: Option<u64>,
687683
msg: &str,
688684
) -> DmResult<(DeviceInfo, Option<String>)> {
689685
let mut hdr = DmOptions::new().to_ioctl_hdr(Some(id), DmFlags::empty());
690686

691687
let mut msg_struct: dmi::Struct_dm_target_msg = Default::default();
692-
msg_struct.sector = *sector.unwrap_or_default();
688+
msg_struct.sector = sector.unwrap_or_default();
693689
let mut data_in = unsafe {
694690
let ptr = &msg_struct as *const dmi::Struct_dm_target_msg as *mut u8;
695691
slice::from_raw_parts(ptr, size_of::<dmi::Struct_dm_target_msg>()).to_vec()

src/core/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,4 @@ pub use self::deviceinfo::DeviceInfo;
1919
pub use self::dm::DM;
2020
pub use self::dm_flags::{DmCookie, DmFlags};
2121
pub use self::dm_options::DmOptions;
22-
pub use self::types::{
23-
Bytes, DataBlocks, DevId, DmName, DmNameBuf, DmUuid, DmUuidBuf, MetaBlocks, Sectors,
24-
TargetType, TargetTypeBuf, SECTOR_SIZE,
25-
};
22+
pub use self::types::{DevId, DmName, DmNameBuf, DmUuid, DmUuidBuf, TargetType, TargetTypeBuf};

src/core/types.rs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,13 @@
44

55
use std::borrow::Borrow;
66
use std::fmt;
7-
use std::iter::Sum;
8-
use std::ops::{Add, Deref, Div, Mul, Rem};
9-
10-
use serde;
7+
use std::ops::Deref;
118

129
use crate::result::{DmError, DmResult};
1310

1411
use crate::core::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
1512
use crate::core::errors::ErrorKind;
1613

17-
/// disk sector size in bytes
18-
pub const SECTOR_SIZE: usize = 512;
19-
20-
/// a kernel defined block size constant for any DM meta device
21-
/// a DM meta device may store cache device or thinpool device metadata
22-
/// defined in drivers/md/persistent-data/dm-space-map-metadata.h as
23-
/// DM_SM_METADATA_BLOCK_SIZE.
24-
const META_BLOCK_SIZE: Sectors = Sectors(8);
25-
26-
/// The maximum size of a metadata device.
27-
/// defined in drivers/md/persistent-data/dm-space-map-metadata.h as
28-
/// DM_SM_METADATA_MAX_BLOCKS.
29-
/// As far as I can tell, this is not a limit on the size of a designated
30-
/// metadata device, but instead on the possible usage of that device.
31-
#[allow(dead_code)]
32-
const MAX_META_DEV_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));
33-
34-
range!(DataBlocks, "data blocks");
35-
36-
range!(MetaBlocks, "meta blocks");
37-
38-
impl MetaBlocks {
39-
/// Return the number of Sectors in the MetaBlocks.
40-
pub fn sectors(self) -> Sectors {
41-
self.0 * META_BLOCK_SIZE
42-
}
43-
}
44-
45-
range!(Bytes, "bytes");
46-
47-
impl Bytes {
48-
/// Return the number of Sectors fully contained in these bytes.
49-
pub fn sectors(self) -> Sectors {
50-
Sectors(self.0 / SECTOR_SIZE as u64)
51-
}
52-
}
53-
54-
range!(Sectors, "sectors");
55-
56-
impl Sectors {
57-
/// The number of bytes in these sectors.
58-
pub fn bytes(self) -> Bytes {
59-
Bytes(self.0 * SECTOR_SIZE as u64)
60-
}
61-
62-
/// The number of whole metablocks contained in these sectors.
63-
pub fn metablocks(self) -> MetaBlocks {
64-
MetaBlocks(self / META_BLOCK_SIZE)
65-
}
66-
}
67-
6814
/// A devicemapper name. Really just a string, but also the argument type of
6915
/// DevId::Name. Used in function arguments to indicate that the function
7016
/// takes only a name, not a devicemapper uuid.

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ mod thindev;
117117
mod thindevid;
118118
/// thinpooldev is shared space for other thin provisioned devices to use
119119
mod thinpooldev;
120+
/// representation of units used by the outer layers
121+
mod units;
120122

121123
#[cfg(test)]
122124
mod loopbacked;
@@ -130,8 +132,8 @@ pub use crate::cachedev::{
130132
};
131133
pub use crate::consts::IEC;
132134
pub use crate::core::{
133-
devnode_to_devno, Bytes, DataBlocks, DevId, Device, DmCookie, DmFlags, DmName, DmNameBuf,
134-
DmOptions, DmUuid, DmUuidBuf, MetaBlocks, Sectors, TargetType, TargetTypeBuf, DM, SECTOR_SIZE,
135+
devnode_to_devno, DevId, Device, DmCookie, DmFlags, DmName, DmNameBuf, DmOptions, DmUuid,
136+
DmUuidBuf, TargetType, TargetTypeBuf, DM,
135137
};
136138
pub use crate::lineardev::{
137139
FlakeyTargetParams, LinearDev, LinearDevTargetParams, LinearDevTargetTable, LinearTargetParams,
@@ -144,3 +146,4 @@ pub use crate::thinpooldev::{
144146
ThinPoolDev, ThinPoolNoSpacePolicy, ThinPoolStatus, ThinPoolStatusSummary, ThinPoolUsage,
145147
ThinPoolWorkingStatus,
146148
};
149+
pub use crate::units::{Bytes, DataBlocks, MetaBlocks, Sectors, SECTOR_SIZE};

src/lineardev.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ use std::fmt;
77
use std::path::PathBuf;
88
use std::str::FromStr;
99

10-
use crate::core::{
11-
DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, Sectors, TargetTypeBuf, DM,
12-
};
10+
use crate::core::{DevId, Device, DeviceInfo, DmName, DmOptions, DmUuid, TargetTypeBuf, DM};
1311
use crate::result::{DmError, DmResult, ErrorEnum};
1412
use crate::shared::{
1513
device_create, device_exists, device_match, parse_device, parse_value, DmDevice, TargetLine,
1614
TargetParams, TargetTable,
1715
};
16+
use crate::units::Sectors;
1817

1918
const FLAKEY_TARGET_NAME: &str = "flakey";
2019
const LINEAR_TARGET_NAME: &str = "linear";
@@ -325,29 +324,29 @@ impl fmt::Display for LinearDevTargetTable {
325324

326325
impl TargetTable for LinearDevTargetTable {
327326
fn from_raw_table(
328-
table: &[(Sectors, Sectors, TargetTypeBuf, String)],
327+
table: &[(u64, u64, TargetTypeBuf, String)],
329328
) -> DmResult<LinearDevTargetTable> {
330329
Ok(LinearDevTargetTable {
331330
table: table
332331
.iter()
333332
.map(|x| -> DmResult<TargetLine<LinearDevTargetParams>> {
334333
Ok(TargetLine::new(
335-
x.0,
336-
x.1,
334+
Sectors(x.0),
335+
Sectors(x.1),
337336
format!("{} {}", x.2.to_string(), x.3).parse::<LinearDevTargetParams>()?,
338337
))
339338
})
340339
.collect::<DmResult<Vec<_>>>()?,
341340
})
342341
}
343342

344-
fn to_raw_table(&self) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)> {
343+
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)> {
345344
self.table
346345
.iter()
347346
.map(|x| {
348347
(
349-
x.start,
350-
x.length,
348+
*x.start,
349+
*x.length,
351350
x.params.target_type(),
352351
x.params.param_str(),
353352
)

src/loopbacked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use nix;
1313
use tempfile::{self, TempDir};
1414

1515
use crate::consts::IEC;
16-
use crate::core::{Bytes, Sectors, SECTOR_SIZE};
1716
use crate::test_lib::clean_up;
17+
use crate::units::{Bytes, Sectors, SECTOR_SIZE};
1818

1919
/// send IOCTL via blkgetsize64
2020
ioctl_read!(blkgetsize64, 0x12, 114, u64);

src/shared.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use std::path::{Path, PathBuf};
1010
use std::str::FromStr;
1111

1212
use crate::core::{
13-
devnode_to_devno, DevId, Device, DeviceInfo, DmFlags, DmName, DmOptions, DmUuid, Sectors,
14-
TargetTypeBuf, DM,
13+
devnode_to_devno, DevId, Device, DeviceInfo, DmFlags, DmName, DmOptions, DmUuid, TargetTypeBuf,
14+
DM,
1515
};
1616
use crate::result::{DmError, DmResult, ErrorEnum};
17+
use crate::units::Sectors;
1718

1819
/// The trait for properties of the params string of TargetType
1920
pub trait TargetParams: Clone + fmt::Debug + fmt::Display + Eq + FromStr + PartialEq {
@@ -48,10 +49,10 @@ impl<T: TargetParams> TargetLine<T> {
4849

4950
pub trait TargetTable: Clone + fmt::Debug + fmt::Display + Eq + PartialEq + Sized {
5051
/// Constructs a table from a raw table returned by DM::table_status()
51-
fn from_raw_table(table: &[(Sectors, Sectors, TargetTypeBuf, String)]) -> DmResult<Self>;
52+
fn from_raw_table(table: &[(u64, u64, TargetTypeBuf, String)]) -> DmResult<Self>;
5253

5354
/// Generates a table that can be loaded by DM::table_load()
54-
fn to_raw_table(&self) -> Vec<(Sectors, Sectors, TargetTypeBuf, String)>;
55+
fn to_raw_table(&self) -> Vec<(u64, u64, TargetTypeBuf, String)>;
5556
}
5657

5758
/// A trait capturing some shared properties of DM devices.

src/shared_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ macro_rules! devnode {
3434
macro_rules! to_raw_table_unique {
3535
($s:ident) => {
3636
vec![(
37-
$s.table.start,
38-
$s.table.length,
37+
*$s.table.start,
38+
*$s.table.length,
3939
$s.table.params.target_type(),
4040
$s.table.params.param_str(),
4141
)]

0 commit comments

Comments
 (0)