Skip to content

Commit

Permalink
Use ValidatedIntegritySpec for serialization
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Dec 20, 2024
1 parent 3047996 commit 83a46d4
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 146 deletions.
46 changes: 27 additions & 19 deletions src/bin/utils/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use strum::VariantNames;
use devicemapper::Bytes;

use stratisd::engine::{
IntegrityTagSpec, DEFAULT_INTEGRITY_JOURNAL_SIZE, DEFAULT_INTEGRITY_TAG_SPEC,
IntegritySpec, IntegrityTagSpec, ValidatedIntegritySpec, DEFAULT_INTEGRITY_JOURNAL_SIZE,
DEFAULT_INTEGRITY_TAG_SPEC,
};

use crate::utils::predict_usage;
Expand Down Expand Up @@ -149,24 +150,31 @@ impl<'a> UtilCommand<'a> for StratisPredictUsage {
sub_m
.get_many::<u128>("filesystem-size")
.map(|szs| szs.map(|n| Bytes(*n)).collect::<Vec<_>>()),
sub_m
.get_one::<u64>("integrity_journal_size")
.map(|n| Bytes::from(*n))
.map(|b| {
if b % 4096u64 != Bytes(0) {
Err(format!("Value {b} is not aligned to 4096"))
} else {
Ok(b.sectors())
}
})
.transpose()?
.expect("default specified by parser"),
sub_m
.get_one::<String>("integrity_tag_spec")
.map(|sz| {
IntegrityTagSpec::try_from(sz.as_str()).expect("parser ensures valid value")
})
.expect("default specified by parser"),
ValidatedIntegritySpec::try_from(IntegritySpec {
journal_size: Some(
sub_m
.get_one::<u64>("integrity_journal_size")
.map(|n| Bytes::from(*n))
.map(|b| {
if b % 4096u64 != Bytes(0) {
Err(format!("Value {b} is not aligned to 4096"))
} else {
Ok(b)
}
})
.transpose()?
.expect("default specified by parser"),
),
tag_spec: Some(
sub_m
.get_one::<String>("integrity_tag_spec")
.map(|sz| {
IntegrityTagSpec::try_from(sz.as_str())
.expect("parser ensures valid value")
})
.expect("default specified by parser"),
),
})?,
LevelFilter::from_str(
matches
.get_one::<String>("log-level")
Expand Down
14 changes: 5 additions & 9 deletions src/bin/utils/predict_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use serde_json::{json, Value};
use devicemapper::{Bytes, Sectors};

use stratisd::engine::{
crypt_metadata_size, integrity_meta_space, IntegrityTagSpec, ThinPoolSizeParams, BDA,
DEFAULT_INTEGRITY_BLOCK_SIZE,
crypt_metadata_size, integrity_meta_space, ThinPoolSizeParams, ValidatedIntegritySpec, BDA,
};

// 2^FS_SIZE_START_POWER is the logical size of the smallest Stratis
Expand Down Expand Up @@ -166,17 +165,15 @@ pub fn predict_filesystem_usage(

fn predict_pool_metadata_usage(
device_sizes: Vec<Sectors>,
journal_size: Sectors,
tag_spec: IntegrityTagSpec,
integrity_spec: ValidatedIntegritySpec,
) -> Result<Sectors, Box<dyn Error>> {
let stratis_metadata_alloc = BDA::default().extended_size().sectors();
let stratis_avail_sizes = device_sizes
.iter()
.map(|&s| {
info!("Total size of device: {:}", s);

let integrity_deduction =
integrity_meta_space(s, journal_size, DEFAULT_INTEGRITY_BLOCK_SIZE, tag_spec);
let integrity_deduction = integrity_meta_space(s, integrity_spec);
info!(
"Deduction for stratis metadata: {:}",
stratis_metadata_alloc
Expand Down Expand Up @@ -213,8 +210,7 @@ pub fn predict_pool_usage(
overprovisioned: bool,
device_sizes: Vec<Bytes>,
filesystem_sizes: Option<Vec<Bytes>>,
journal_size: Sectors,
tag_spec: IntegrityTagSpec,
integrity_spec: ValidatedIntegritySpec,
log_level: LevelFilter,
) -> Result<(), Box<dyn Error>> {
Builder::new().filter(None, log_level).init();
Expand All @@ -226,7 +222,7 @@ pub fn predict_pool_usage(
let device_sizes = device_sizes.iter().map(|s| s.sectors()).collect::<Vec<_>>();
let total_size: Sectors = device_sizes.iter().cloned().sum();

let non_metadata_size = predict_pool_metadata_usage(device_sizes, journal_size, tag_spec)?;
let non_metadata_size = predict_pool_metadata_usage(device_sizes, integrity_spec)?;

let size_params = ThinPoolSizeParams::new(non_metadata_size)?;
let total_non_data = 2usize * size_params.meta_size() + size_params.mdv_size();
Expand Down
4 changes: 2 additions & 2 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub use self::{
crypt_metadata_size, get_dm, get_dm_init, integrity_meta_space, register_clevis_token,
set_up_crypt_logging, unshare_mount_namespace, StaticHeader, StaticHeaderResult,
StratEngine, StratKeyActions, ThinPoolSizeParams, BDA, CLEVIS_TANG_TRUST_URL,
DEFAULT_INTEGRITY_BLOCK_SIZE, DEFAULT_INTEGRITY_JOURNAL_SIZE, DEFAULT_INTEGRITY_TAG_SPEC,
},
structures::{AllLockReadGuard, ExclusiveGuard, SharedGuard, Table},
types::{
Expand All @@ -27,7 +26,8 @@ pub use self::{
PoolUuid, PropChangeAction, RenameAction, ReportType, SetCreateAction, SetDeleteAction,
SetUnlockAction, StartAction, StopAction, StoppedPoolInfo, StoppedPoolsInfo,
StratBlockDevDiff, StratFilesystemDiff, StratPoolDiff, StratSigblockVersion, StratisUuid,
ThinPoolDiff, ToDisplay, UdevEngineEvent, UnlockMethod,
ThinPoolDiff, ToDisplay, UdevEngineEvent, UnlockMethod, ValidatedIntegritySpec,
DEFAULT_INTEGRITY_JOURNAL_SIZE, DEFAULT_INTEGRITY_TAG_SPEC,
},
};

Expand Down
3 changes: 1 addition & 2 deletions src/engine/strat_engine/backstore/backstore/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@ impl Backstore {
) -> StratisResult<Backstore> {
let data_tier = DataTier::<StratBlockDev>::new(
BlockDevMgr::<StratBlockDev>::initialize(pool_uuid, devices, mda_data_size)?,
integrity_spec.journal_size(),
integrity_spec.tag_spec(),
integrity_spec,
);

let mut backstore = Backstore {
Expand Down
38 changes: 15 additions & 23 deletions src/engine/strat_engine/backstore/blockdev/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use crate::{
types::BDAResult,
},
types::{
Compare, DevUuid, DevicePath, Diff, IntegrityTagSpec, PoolUuid, StateDiff,
StratBlockDevDiff, StratSigblockVersion,
Compare, DevUuid, DevicePath, Diff, PoolUuid, StateDiff, StratBlockDevDiff,
StratSigblockVersion, ValidatedIntegritySpec,
},
},
stratis::{StratisError, StratisResult},
Expand All @@ -49,14 +49,15 @@ use crate::{
/// The result is divisible by 8 sectors.
pub fn integrity_meta_space(
total_space: Sectors,
journal_size: Sectors,
block_size: Bytes,
tag_spec: IntegrityTagSpec,
integrity_spec: ValidatedIntegritySpec,
) -> Sectors {
Bytes(4096).sectors()
+ journal_size
+ integrity_spec.journal_size
+ Bytes::from(
(*((total_space.bytes() / block_size) * tag_spec.as_bytes_ceil()) + 4095) & !4095,
(*((total_space.bytes() / integrity_spec.block_size)
* integrity_spec.tag_spec.as_bytes_ceil())
+ 4095)
& !4095,
)
.sectors()
}
Expand Down Expand Up @@ -220,12 +221,7 @@ impl StratBlockDev {
///
/// This will also extend integrity metadata reservations according to the new
/// size of the device.
pub fn grow(
&mut self,
integrity_journal_size: Sectors,
integrity_block_size: Bytes,
integrity_tag_spec: IntegrityTagSpec,
) -> StratisResult<bool> {
pub fn grow(&mut self, integrity_spec: ValidatedIntegritySpec) -> StratisResult<bool> {
let size = BlockdevSize::new(Self::scan_blkdev_size(self.devnode())?);
let metadata_size = self.bda.dev_size();
match size.cmp(&metadata_size) {
Expand All @@ -252,16 +248,12 @@ impl StratBlockDev {
self.bda.header = h;
self.used.increase_size(size.sectors());

let integrity_grow = integrity_meta_space(
size.sectors(),
integrity_journal_size,
integrity_block_size,
integrity_tag_spec,
) - self
.integrity_meta_allocs
.iter()
.map(|(_, len)| *len)
.sum::<Sectors>();
let integrity_grow = integrity_meta_space(size.sectors(), integrity_spec)
- self
.integrity_meta_allocs
.iter()
.map(|(_, len)| *len)
.sum::<Sectors>();
self.alloc_int_meta_back(integrity_grow);

Ok(true)
Expand Down
14 changes: 5 additions & 9 deletions src/engine/strat_engine/backstore/blockdevmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use crate::{
serde_structs::{BaseBlockDevSave, Recordable},
shared::bds_to_bdas,
},
types::{DevUuid, EncryptionInfo, IntegrityTagSpec, Name, PoolEncryptionInfo, PoolUuid},
types::{
DevUuid, EncryptionInfo, Name, PoolEncryptionInfo, PoolUuid, ValidatedIntegritySpec,
},
},
stratis::{StratisError, StratisResult},
};
Expand Down Expand Up @@ -246,20 +248,14 @@ impl BlockDevMgr<v2::StratBlockDev> {
pub fn grow(
&mut self,
dev: DevUuid,
integrity_journal_size: Sectors,
integrity_block_size: Bytes,
integrity_tag_spec: IntegrityTagSpec,
integrity_spec: ValidatedIntegritySpec,
) -> StratisResult<bool> {
let bd = self
.block_devs
.iter_mut()
.find(|bd| bd.uuid() == dev)
.ok_or_else(|| StratisError::Msg(format!("Block device with UUID {dev} not found")))?;
bd.grow(
integrity_journal_size,
integrity_block_size,
integrity_tag_spec,
)
bd.grow(integrity_spec)
}

#[cfg(test)]
Expand Down
60 changes: 17 additions & 43 deletions src/engine/strat_engine/backstore/data_tier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#[cfg(test)]
use std::collections::HashSet;

use devicemapper::{Bytes, Sectors, IEC};
use devicemapper::Sectors;

use crate::{
engine::{
Expand All @@ -27,28 +27,20 @@ use crate::{
},
types::BDARecordResult,
},
types::{BlockDevTier, DevUuid, IntegrityTagSpec, Name, PoolUuid},
types::{BlockDevTier, DevUuid, Name, PoolUuid, ValidatedIntegritySpec},
},
stratis::StratisResult,
};

pub const DEFAULT_INTEGRITY_JOURNAL_SIZE: Bytes = Bytes(128 * IEC::Mi as u128);
pub const DEFAULT_INTEGRITY_BLOCK_SIZE: Bytes = Bytes(4 * IEC::Ki as u128);
pub const DEFAULT_INTEGRITY_TAG_SPEC: IntegrityTagSpec = IntegrityTagSpec::B512;

/// Handles the lowest level, base layer of this tier.
#[derive(Debug)]
pub struct DataTier<B> {
/// Manages the individual block devices
pub(super) block_mgr: BlockDevMgr<B>,
/// The list of segments granted by block_mgr and used by dm_device
pub(super) segments: AllocatedAbove,
/// Integrity journal size.
integrity_journal_size: Option<Sectors>,
/// Integrity block size.
integrity_block_size: Option<Bytes>,
/// Integrity tag spec.
integrity_tag_spec: Option<IntegrityTagSpec>,
/// Integrity spec
integrity_spec: Option<ValidatedIntegritySpec>,
}

impl DataTier<v1::StratBlockDev> {
Expand All @@ -62,9 +54,7 @@ impl DataTier<v1::StratBlockDev> {
DataTier {
block_mgr,
segments: AllocatedAbove { inner: vec![] },
integrity_journal_size: None,
integrity_block_size: None,
integrity_tag_spec: None,
integrity_spec: None,
}
}

Expand Down Expand Up @@ -124,30 +114,21 @@ impl DataTier<v2::StratBlockDev> {
/// WARNING: metadata changing event
pub fn new(
mut block_mgr: BlockDevMgr<v2::StratBlockDev>,
integrity_journal_size: Option<Sectors>,
integrity_tag_spec: Option<IntegrityTagSpec>,
integrity_spec: ValidatedIntegritySpec,
) -> DataTier<v2::StratBlockDev> {
let integrity_journal_size =
integrity_journal_size.unwrap_or_else(|| DEFAULT_INTEGRITY_JOURNAL_SIZE.sectors());
let integrity_block_size = DEFAULT_INTEGRITY_BLOCK_SIZE;
let integrity_tag_spec = integrity_tag_spec.unwrap_or(DEFAULT_INTEGRITY_TAG_SPEC);
for (_, bd) in block_mgr.blockdevs_mut() {
// NOTE: over-allocates integrity metadata slightly. Some of the
// total size of the device will not make use of the integrity
// metadata.
bd.alloc_int_meta_back(integrity_meta_space(
bd.total_size().sectors(),
integrity_journal_size,
integrity_block_size,
integrity_tag_spec,
integrity_spec,
));
}
DataTier {
block_mgr,
segments: AllocatedAbove { inner: vec![] },
integrity_journal_size: Some(integrity_journal_size),
integrity_block_size: Some(integrity_block_size),
integrity_tag_spec: Some(integrity_tag_spec),
integrity_spec: Some(integrity_spec),
}
}

Expand Down Expand Up @@ -176,9 +157,7 @@ impl DataTier<v2::StratBlockDev> {
for bd in bds {
bd.alloc_int_meta_back(integrity_meta_space(
bd.total_size().sectors(),
self.integrity_journal_size.expect("Must be some in V2"),
self.integrity_block_size.expect("Must be some in V2"),
self.integrity_tag_spec.expect("Must be some in V2"),
self.integrity_spec.expect("Must be some in V2"),
));
}
Ok(uuids)
Expand Down Expand Up @@ -214,12 +193,8 @@ impl DataTier<v2::StratBlockDev> {
}

pub fn grow(&mut self, dev: DevUuid) -> StratisResult<bool> {
self.block_mgr.grow(
dev,
self.integrity_journal_size.expect("Must be Some in V2"),
self.integrity_block_size.expect("Must be Some in V2"),
self.integrity_tag_spec.expect("Must be Some in V2"),
)
self.block_mgr
.grow(dev, self.integrity_spec.expect("Must be Some in V2"))
}
}

Expand Down Expand Up @@ -249,9 +224,7 @@ where
Ok(DataTier {
block_mgr,
segments,
integrity_journal_size: data_tier_save.integrity_journal_size,
integrity_block_size: data_tier_save.integrity_block_size,
integrity_tag_spec: data_tier_save.integrity_tag_spec,
integrity_spec: data_tier_save.integrity_spec,
})
}

Expand Down Expand Up @@ -342,9 +315,7 @@ where
allocs: vec![self.segments.record()],
devs: self.block_mgr.record(),
},
integrity_journal_size: self.integrity_journal_size,
integrity_block_size: self.integrity_block_size,
integrity_tag_spec: self.integrity_tag_spec,
integrity_spec: self.integrity_spec,
}
}
}
Expand Down Expand Up @@ -483,7 +454,10 @@ mod tests {
)
.unwrap();

let mut data_tier = DataTier::<blockdev::v2::StratBlockDev>::new(mgr, None, None);
let mut data_tier = DataTier::<blockdev::v2::StratBlockDev>::new(
mgr,
ValidatedIntegritySpec::default(),
);
data_tier.invariant();

// A data_tier w/ some devices but nothing allocated
Expand Down
3 changes: 0 additions & 3 deletions src/engine/strat_engine/backstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ mod shared;

pub use self::{
blockdev::v2::integrity_meta_space,
data_tier::{
DEFAULT_INTEGRITY_BLOCK_SIZE, DEFAULT_INTEGRITY_JOURNAL_SIZE, DEFAULT_INTEGRITY_TAG_SPEC,
},
devices::{find_stratis_devs_by_uuid, get_devno_from_path, ProcessedPathInfos, UnownedDevices},
};

Expand Down
Loading

0 comments on commit 83a46d4

Please sign in to comment.