Skip to content

Commit

Permalink
Add a ValidatedIntegritySpec type
Browse files Browse the repository at this point in the history
Use ValidatedIntegritySpec for serialization and to pass to methods
invoked by engine's create_pool method.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Dec 20, 2024
1 parent bf85af1 commit c2f2231
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 194 deletions.
38 changes: 19 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,23 @@ 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))
.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
5 changes: 4 additions & 1 deletion src/engine/sim_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
IntegritySpec, LockedPoolsInfo, Name, PoolDevice, PoolDiff, PoolIdentifier, PoolUuid,
RenameAction, ReportType, SetUnlockAction, StartAction, StopAction, StoppedPoolInfo,
StoppedPoolsInfo, StratFilesystemDiff, UdevEngineEvent, UnlockMethod,
ValidatedIntegritySpec,
},
StratSigblockVersion,
},
Expand Down Expand Up @@ -129,13 +130,15 @@ impl Engine for SimEngine {
name: &str,
blockdev_paths: &[&Path],
encryption_info: Option<&EncryptionInfo>,
_: IntegritySpec,
integrity_spec: IntegritySpec,
) -> StratisResult<CreateAction<PoolUuid>> {
validate_name(name)?;
let name = Name::new(name.to_owned());

validate_paths(blockdev_paths)?;

ValidatedIntegritySpec::try_from(integrity_spec)?;

if let Some(key_desc) = encryption_info.and_then(|ei| ei.key_description()) {
if !self.key_handler.contains_key(key_desc) {
return Err(StratisError::Msg(format!(
Expand Down
39 changes: 18 additions & 21 deletions src/engine/strat_engine/backstore/backstore/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::{
writing::wipe_sectors,
},
types::{
ActionAvailability, BlockDevTier, DevUuid, EncryptionInfo, IntegrityTagSpec,
KeyDescription, PoolUuid, SizedKeyMemory, UnlockMethod,
ActionAvailability, BlockDevTier, DevUuid, EncryptionInfo, KeyDescription, PoolUuid,
SizedKeyMemory, UnlockMethod, ValidatedIntegritySpec,
},
},
stratis::{StratisError, StratisResult},
Expand Down Expand Up @@ -437,13 +437,11 @@ impl Backstore {
devices: UnownedDevices,
mda_data_size: MDADataSize,
encryption_info: Option<&EncryptionInfo>,
integrity_journal_size: Option<Sectors>,
integrity_tag_spec: Option<IntegrityTagSpec>,
integrity_spec: ValidatedIntegritySpec,
) -> StratisResult<Backstore> {
let data_tier = DataTier::<StratBlockDev>::new(
BlockDevMgr::<StratBlockDev>::initialize(pool_uuid, devices, mda_data_size)?,
integrity_journal_size,
integrity_tag_spec,
integrity_spec,
);

let mut backstore = Backstore {
Expand Down Expand Up @@ -1172,13 +1170,16 @@ mod tests {

use devicemapper::{CacheDevStatus, DataBlocks, DmOptions, IEC};

use crate::engine::strat_engine::{
backstore::devices::{ProcessedPathInfos, UnownedDevices},
cmd,
crypt::crypt_metadata_size,
metadata::device_identifiers,
ns::{unshare_mount_namespace, MemoryFilesystem},
tests::{crypt, loopbacked, real},
use crate::engine::{
strat_engine::{
backstore::devices::{ProcessedPathInfos, UnownedDevices},
cmd,
crypt::crypt_metadata_size,
metadata::device_identifiers,
ns::{unshare_mount_namespace, MemoryFilesystem},
tests::{crypt, loopbacked, real},
},
types::ValidatedIntegritySpec,
};

use super::*;
Expand Down Expand Up @@ -1255,8 +1256,7 @@ mod tests {
initdatadevs,
MDADataSize::default(),
None,
None,
None,
ValidatedIntegritySpec::default(),
)
.unwrap();

Expand Down Expand Up @@ -1354,8 +1354,7 @@ mod tests {
devices1,
MDADataSize::default(),
None,
None,
None,
ValidatedIntegritySpec::default(),
)
.unwrap();

Expand Down Expand Up @@ -1420,8 +1419,7 @@ mod tests {
"tang".to_string(),
json!({"url": env::var("TANG_URL").expect("TANG_URL env var required"), "stratis:tang:trust_url": true}),
))),
None,
None,
ValidatedIntegritySpec::default(),
)
.unwrap();
backstore.alloc(pool_uuid, &[Sectors(512)]).unwrap();
Expand Down Expand Up @@ -1496,8 +1494,7 @@ mod tests {
json!({"url": env::var("TANG_URL").expect("TANG_URL env var required"), "stratis:tang:trust_url": true}),
),
)),
None,
None,
ValidatedIntegritySpec::default(),
).unwrap();
cmd::udev_settle().unwrap();

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
Loading

0 comments on commit c2f2231

Please sign in to comment.