Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry picks types #3680

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/engine/strat_engine/backstore/backstore/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
backstore::{
backstore::InternalBackstore,
blockdev::{v1::StratBlockDev, InternalBlockDev},
blockdevmgr::BlockDevMgr,
blockdevmgr::{BlockDevMgr, BlockDevMgrMetaSize, BlockDevMgrSize},
cache_tier::CacheTier,
data_tier::DataTier,
devices::UnownedDevices,
Expand Down Expand Up @@ -125,11 +125,11 @@ impl InternalBackstore for Backstore {
}

fn datatier_usable_size(&self) -> Sectors {
self.data_tier.usable_size()
self.data_tier.usable_size().sectors()
}

fn available_in_backstore(&self) -> Sectors {
self.data_tier.usable_size() - self.next
self.data_tier.usable_size().sectors() - self.next
}

fn alloc(
Expand Down Expand Up @@ -513,7 +513,7 @@ impl Backstore {
}

/// The current size of all the blockdevs in the data tier.
pub fn datatier_size(&self) -> Sectors {
pub fn datatier_size(&self) -> BlockDevMgrSize {
self.data_tier.size()
}

Expand Down Expand Up @@ -605,7 +605,7 @@ impl Backstore {

/// The number of sectors in the backstore given up to Stratis metadata
/// on devices in the data tier.
pub fn datatier_metadata_size(&self) -> Sectors {
pub fn datatier_metadata_size(&self) -> BlockDevMgrMetaSize {
self.data_tier.metadata_size()
}

Expand Down
16 changes: 10 additions & 6 deletions src/engine/strat_engine/backstore/backstore/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ use crate::{
engine::{
strat_engine::{
backstore::{
backstore::InternalBackstore, blockdev::v2::StratBlockDev,
blockdevmgr::BlockDevMgr, cache_tier::CacheTier, data_tier::DataTier,
devices::UnownedDevices, shared::BlockSizeSummary,
backstore::InternalBackstore,
blockdev::v2::StratBlockDev,
blockdevmgr::{BlockDevMgr, BlockDevMgrSize},
cache_tier::CacheTier,
data_tier::DataTier,
devices::UnownedDevices,
shared::BlockSizeSummary,
},
crypt::{crypt_metadata_size, handle::v2::CryptHandle, interpret_clevis_config},
dm::{get_dm, list_of_backstore_devices, remove_optional_devices, DEVICEMAPPER_PATH},
Expand Down Expand Up @@ -170,7 +174,7 @@ impl InternalBackstore for Backstore {
}

fn datatier_usable_size(&self) -> Sectors {
self.datatier_size() - self.datatier_metadata_size()
self.datatier_size().sectors() - self.datatier_metadata_size()
}

fn available_in_backstore(&self) -> Sectors {
Expand Down Expand Up @@ -750,7 +754,7 @@ impl Backstore {
}

/// The current size of all the blockdevs in the data tier.
pub fn datatier_size(&self) -> Sectors {
pub fn datatier_size(&self) -> BlockDevMgrSize {
self.data_tier.size()
}

Expand Down Expand Up @@ -857,7 +861,7 @@ impl Backstore {

/// Metadata size on the data tier, including crypt metadata space.
pub fn datatier_metadata_size(&self) -> Sectors {
self.datatier_crypt_meta_size() + self.data_tier.metadata_size()
self.datatier_crypt_meta_size() + self.data_tier.metadata_size().sectors()
}

/// Write the given data to the data tier's devices.
Expand Down
151 changes: 123 additions & 28 deletions src/engine/strat_engine/backstore/blockdevmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,87 @@ impl TimeStamp {
}
}

/// Type for the block dev mgr size.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrSize(pub Sectors);

impl BlockDevMgrSize {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrSize
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrSize(blockdevs.iter().map(|b| b.total_size().sectors()).sum())
}
}

/// All metadata allocated to individual block devs in the backstore.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrMetaSize(pub Sectors);

impl BlockDevMgrMetaSize {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrMetaSize
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrMetaSize(blockdevs.iter().map(|bd| bd.metadata_size()).sum())
}
}

/// All space that has not been allocated for any purpose.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrAvailSpace(pub Sectors);

impl BlockDevMgrAvailSpace {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrAvailSpace
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrAvailSpace(blockdevs.iter().map(|bd| bd.available()).sum())
}
}

/// All space that has not been allocated to metadata on the devices.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrUsableSpace(pub Sectors);

impl BlockDevMgrUsableSpace {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrUsableSpace
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrUsableSpace(
blockdevs
.iter()
.map(|b| b.total_size().sectors() - b.metadata_size())
.sum(),
)
}
}

#[derive(Debug)]
pub struct BlockDevMgr<B> {
/// All the block devices that belong to this block dev manager.
Expand All @@ -77,6 +158,10 @@ pub struct BlockDevMgr<B> {
last_update_time: TimeStamp,
}

pub struct Sizer<'a, B> {
block_devs: &'a [B],
}

impl BlockDevMgr<v1::StratBlockDev> {
/// Initialize a new StratBlockDevMgr with specified pool and devices.
pub fn initialize(
Expand Down Expand Up @@ -249,6 +334,27 @@ impl BlockDevMgr<v2::StratBlockDev> {
}
}

impl<B> Sizer<'_, B>
where
B: InternalBlockDev,
{
pub fn avail_space(&self) -> BlockDevMgrAvailSpace {
self.block_devs.into()
}

pub fn size(&self) -> BlockDevMgrSize {
self.block_devs.into()
}

pub fn metadata_size(&self) -> BlockDevMgrMetaSize {
self.block_devs.into()
}

pub fn usable_size(&self) -> BlockDevMgrUsableSpace {
self.block_devs.into()
}
}

impl<B> BlockDevMgr<B>
where
B: InternalBlockDev,
Expand Down Expand Up @@ -346,7 +452,7 @@ where
/// nothing.
pub fn alloc(&mut self, sizes: &[Sectors]) -> Option<Vec<Vec<BlkDevSegment>>> {
let total_needed: Sectors = sizes.iter().cloned().sum();
if self.avail_space() < total_needed {
if self.sizer().avail_space().sectors() < total_needed {
return None;
}

Expand Down Expand Up @@ -444,27 +550,10 @@ where
})
}

// SIZE methods

/// The number of sectors not allocated for any purpose.
pub fn avail_space(&self) -> Sectors {
self.block_devs.iter().map(|bd| bd.available()).sum()
}

/// The current size of all the blockdevs.
/// self.size() > self.avail_space() because some sectors are certainly
/// allocated for Stratis metadata
pub fn size(&self) -> Sectors {
self.block_devs
.iter()
.map(|b| b.total_size().sectors())
.sum()
}

/// The number of sectors given over to Stratis metadata
/// self.allocated_size() - self.metadata_size() >= self.avail_space()
pub fn metadata_size(&self) -> Sectors {
self.block_devs.iter().map(|bd| bd.metadata_size()).sum()
pub fn sizer(&self) -> Sizer<'_, B> {
Sizer {
block_devs: &self.block_devs,
}
}

pub fn grow(&mut self, dev: DevUuid) -> StratisResult<bool> {
Expand Down Expand Up @@ -546,13 +635,16 @@ mod tests {
None,
)
.unwrap();
assert_eq!(mgr.avail_space() + mgr.metadata_size(), mgr.size());
assert_eq!(
mgr.sizer().avail_space().sectors(),
mgr.sizer().usable_size().sectors()
);

let allocated = Sectors(2);
mgr.alloc(&[allocated]).unwrap();
assert_eq!(
mgr.avail_space() + allocated + mgr.metadata_size(),
mgr.size()
mgr.sizer().avail_space().sectors() + allocated,
mgr.sizer().usable_size().sectors()
);
}

Expand Down Expand Up @@ -764,13 +856,16 @@ mod tests {
MDADataSize::default(),
)
.unwrap();
assert_eq!(mgr.avail_space() + mgr.metadata_size(), mgr.size());
assert_eq!(
mgr.sizer().avail_space().sectors(),
mgr.sizer().usable_size().sectors()
);

let allocated = Sectors(2);
mgr.alloc(&[allocated]).unwrap();
assert_eq!(
mgr.avail_space() + allocated + mgr.metadata_size(),
mgr.size()
mgr.sizer().avail_space().sectors() + allocated,
mgr.sizer().usable_size().sectors()
);
}

Expand Down
Loading