diff --git a/src/engine/strat_engine/backstore/backstore/mod.rs b/src/engine/strat_engine/backstore/backstore/mod.rs new file mode 100644 index 00000000000..9c689e46e44 --- /dev/null +++ b/src/engine/strat_engine/backstore/backstore/mod.rs @@ -0,0 +1,4 @@ +mod shared; +pub mod v1; + +pub use shared::Backstore; diff --git a/src/engine/strat_engine/backstore/backstore/shared.rs b/src/engine/strat_engine/backstore/backstore/shared.rs new file mode 100644 index 00000000000..2fcf98089c7 --- /dev/null +++ b/src/engine/strat_engine/backstore/backstore/shared.rs @@ -0,0 +1,333 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use std::collections::HashMap; + +use chrono::{DateTime, Utc}; +use serde_json::Value; + +use devicemapper::{Device, Sectors}; + +use crate::{ + engine::{ + strat_engine::{ + backstore::{ + backstore::v1, blockdev::StratBlockDev, devices::UnownedDevices, + shared::BlockSizeSummary, transaction::RequestTransaction, + }, + metadata::{MDADataSize, BDA}, + serde_structs::{BackstoreSave, Recordable}, + types::BDARecordResult, + }, + types::{ + ActionAvailability, BlockDevTier, DevUuid, EncryptionInfo, KeyDescription, Name, + PoolEncryptionInfo, PoolUuid, + }, + }, + stratis::StratisResult, +}; + +#[derive(Debug)] +pub enum Backstore { + V1(v1::Backstore), +} + +impl Backstore { + pub fn setup_v1( + pool_uuid: PoolUuid, + backstore_save: &BackstoreSave, + datadevs: Vec, + cachedevs: Vec, + last_update_time: DateTime, + ) -> BDARecordResult { + v1::Backstore::setup( + pool_uuid, + backstore_save, + datadevs, + cachedevs, + last_update_time, + ) + .map(Backstore::V1) + } + + pub fn initialize_v1( + pool_name: Name, + pool_uuid: PoolUuid, + devices: UnownedDevices, + mda_data_size: MDADataSize, + encryption_info: Option<&EncryptionInfo>, + ) -> StratisResult { + v1::Backstore::initialize( + pool_name, + pool_uuid, + devices, + mda_data_size, + encryption_info, + ) + .map(Backstore::V1) + } + + pub fn init_cache( + &mut self, + pool_name: Name, + pool_uuid: PoolUuid, + devices: UnownedDevices, + ) -> StratisResult> { + match self { + Backstore::V1(b) => b.init_cache(pool_name, pool_uuid, devices), + } + } + + pub fn add_cachedevs( + &mut self, + pool_name: Name, + pool_uuid: PoolUuid, + devices: UnownedDevices, + ) -> StratisResult> { + match self { + Backstore::V1(b) => b.add_cachedevs(pool_name, pool_uuid, devices), + } + } + + /// Add datadevs to the backstore. The data tier always exists if the + /// backstore exists at all, so there is no need to create it. + pub fn add_datadevs( + &mut self, + pool_name: Name, + pool_uuid: PoolUuid, + devices: UnownedDevices, + ) -> StratisResult> { + match self { + Backstore::V1(b) => b.add_datadevs(pool_name, pool_uuid, devices), + } + } + + pub fn request_alloc( + &mut self, + sizes: &[Sectors], + ) -> StratisResult> { + match self { + Backstore::V1(b) => b.request_alloc(sizes), + } + } + + pub fn commit_alloc( + &mut self, + pool_uuid: PoolUuid, + transaction: RequestTransaction, + ) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.commit_alloc(pool_uuid, transaction), + } + } + + pub fn datadevs(&self) -> Vec<(DevUuid, &StratBlockDev)> { + match self { + Backstore::V1(b) => b.datadevs(), + } + } + + pub fn cachedevs(&self) -> Vec<(DevUuid, &StratBlockDev)> { + match self { + Backstore::V1(b) => b.cachedevs(), + } + } + + pub fn blockdevs(&self) -> Vec<(DevUuid, BlockDevTier, &StratBlockDev)> { + match self { + Backstore::V1(b) => b.blockdevs(), + } + } + + pub fn blockdevs_mut(&mut self) -> Vec<(DevUuid, BlockDevTier, &mut StratBlockDev)> { + match self { + Backstore::V1(b) => b.blockdevs_mut(), + } + } + + pub fn datatier_size(&self) -> Sectors { + match self { + Backstore::V1(b) => b.datatier_size(), + } + } + + pub fn datatier_allocated_size(&self) -> Sectors { + match self { + Backstore::V1(b) => b.datatier_allocated_size(), + } + } + + pub fn datatier_usable_size(&self) -> Sectors { + match self { + Backstore::V1(b) => b.datatier_usable_size(), + } + } + + pub fn available_in_backstore(&self) -> Sectors { + match self { + Backstore::V1(b) => b.available_in_backstore(), + } + } + + pub fn destroy(&mut self, pool_uuid: PoolUuid) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.destroy(pool_uuid), + } + } + + pub fn teardown(&mut self, pool_uuid: PoolUuid) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.teardown(pool_uuid), + } + } + + pub fn into_bdas(self) -> HashMap { + match self { + Backstore::V1(b) => b.into_bdas(), + } + } + + pub fn drain_bds(&mut self) -> Vec { + match self { + Backstore::V1(b) => b.drain_bds(), + } + } + + pub fn device(&self) -> Option { + match self { + Backstore::V1(b) => b.device(), + } + } + + pub fn get_blockdev_by_uuid(&self, uuid: DevUuid) -> Option<(BlockDevTier, &StratBlockDev)> { + match self { + Backstore::V1(b) => b.get_blockdev_by_uuid(uuid), + } + } + + pub fn get_mut_blockdev_by_uuid( + &mut self, + uuid: DevUuid, + ) -> Option<(BlockDevTier, &mut StratBlockDev)> { + match self { + Backstore::V1(b) => b.get_mut_blockdev_by_uuid(uuid), + } + } + + pub fn datatier_metadata_size(&self) -> Sectors { + match self { + Backstore::V1(b) => b.datatier_metadata_size(), + } + } + + pub fn save_state(&mut self, metadata: &[u8]) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.save_state(metadata), + } + } + + pub fn set_blockdev_user_info( + &mut self, + uuid: DevUuid, + user_info: Option<&str>, + ) -> StratisResult> { + match self { + Backstore::V1(b) => b.set_blockdev_user_info(uuid, user_info), + } + } + + pub fn is_encrypted(&self) -> bool { + match self { + Backstore::V1(b) => b.is_encrypted(), + } + } + + pub fn encryption_info(&self) -> Option { + match self { + Backstore::V1(b) => b.encryption_info(), + } + } + + pub fn has_cache(&self) -> bool { + match self { + Backstore::V1(b) => b.has_cache(), + } + } + + pub fn bind_clevis(&mut self, pin: &str, clevis_info: &Value) -> StratisResult { + match self { + Backstore::V1(b) => b.bind_clevis(pin, clevis_info), + } + } + + pub fn unbind_clevis(&mut self) -> StratisResult { + match self { + Backstore::V1(b) => b.unbind_clevis(), + } + } + + pub fn bind_keyring(&mut self, key_desc: &KeyDescription) -> StratisResult { + match self { + Backstore::V1(b) => b.bind_keyring(key_desc), + } + } + + pub fn unbind_keyring(&mut self) -> StratisResult { + match self { + Backstore::V1(b) => b.unbind_keyring(), + } + } + + pub fn rebind_keyring(&mut self, key_desc: &KeyDescription) -> StratisResult> { + match self { + Backstore::V1(b) => b.rebind_keyring(key_desc), + } + } + + pub fn rebind_clevis(&mut self) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.rebind_clevis(), + } + } + + pub fn grow(&mut self, dev: DevUuid) -> StratisResult { + match self { + Backstore::V1(b) => b.grow(dev), + } + } + + pub fn rename_pool(&mut self, new_name: &Name) -> StratisResult<()> { + match self { + Backstore::V1(b) => b.rename_pool(new_name), + } + } + + pub fn block_size_summary(&self, tier: BlockDevTier) -> Option { + match self { + Backstore::V1(b) => b.block_size_summary(tier), + } + } + + pub fn action_availability(&self) -> ActionAvailability { + match self { + Backstore::V1(b) => b.action_availability(), + } + } +} + +impl<'a> Into for &'a Backstore { + fn into(self) -> Value { + match self { + Backstore::V1(b) => b.into(), + } + } +} + +impl Recordable for Backstore { + fn record(&self) -> BackstoreSave { + match self { + Backstore::V1(b) => b.record(), + } + } +} diff --git a/src/engine/strat_engine/backstore/backstore.rs b/src/engine/strat_engine/backstore/backstore/v1.rs similarity index 100% rename from src/engine/strat_engine/backstore/backstore.rs rename to src/engine/strat_engine/backstore/backstore/v1.rs diff --git a/src/engine/strat_engine/pool.rs b/src/engine/strat_engine/pool.rs index baff33871db..753d6a682e7 100644 --- a/src/engine/strat_engine/pool.rs +++ b/src/engine/strat_engine/pool.rs @@ -174,7 +174,7 @@ impl StratPool { // FIXME: Initializing with the minimum MDA size is not necessarily // enough. If there are enough devices specified, more space will be // required. - let mut backstore = Backstore::initialize( + let mut backstore = Backstore::initialize_v1( Name::new(name.to_string()), pool_uuid, devices, @@ -252,7 +252,7 @@ impl StratPool { } let backstore = - Backstore::setup(uuid, &metadata.backstore, datadevs, cachedevs, timestamp)?; + Backstore::setup_v1(uuid, &metadata.backstore, datadevs, cachedevs, timestamp)?; let action_avail = get_pool_state(encryption_info, &backstore); let pool_name = &metadata.name; diff --git a/src/engine/strat_engine/thinpool/thinpool.rs b/src/engine/strat_engine/thinpool/thinpool.rs index 00506f5ff9d..8c79db1921d 100644 --- a/src/engine/strat_engine/thinpool/thinpool.rs +++ b/src/engine/strat_engine/thinpool/thinpool.rs @@ -1704,7 +1704,7 @@ mod tests { let devices = get_devices(paths).unwrap(); let mut backstore = - Backstore::initialize(pool_name, pool_uuid, devices, MDADataSize::default(), None) + Backstore::initialize_v1(pool_name, pool_uuid, devices, MDADataSize::default(), None) .unwrap(); let size = ThinPoolSizeParams::new(backstore.datatier_usable_size()).unwrap(); let mut pool = ThinPool::new(pool_uuid, &size, DATA_BLOCK_SIZE, &mut backstore).unwrap(); @@ -1794,7 +1794,7 @@ mod tests { let first_devices = get_devices(first_path).unwrap(); let remaining_devices = get_devices(remaining_paths).unwrap(); - let mut backstore = Backstore::initialize( + let mut backstore = Backstore::initialize_v1( Name::new(pool_name.to_string()), pool_uuid, first_devices, @@ -1932,7 +1932,7 @@ mod tests { let devices = get_devices(paths).unwrap(); - let mut backstore = Backstore::initialize( + let mut backstore = Backstore::initialize_v1( Name::new(pool_name.to_string()), pool_uuid, devices, @@ -2052,7 +2052,7 @@ mod tests { let devices = get_devices(paths).unwrap(); let mut backstore = - Backstore::initialize(pool_name, pool_uuid, devices, MDADataSize::default(), None) + Backstore::initialize_v1(pool_name, pool_uuid, devices, MDADataSize::default(), None) .unwrap(); let mut pool = ThinPool::new( pool_uuid, @@ -2116,7 +2116,7 @@ mod tests { let devices = get_devices(paths).unwrap(); - let mut backstore = Backstore::initialize( + let mut backstore = Backstore::initialize_v1( Name::new(pool_name.to_string()), pool_uuid, devices, @@ -2197,7 +2197,7 @@ mod tests { let devices = get_devices(paths).unwrap(); let mut backstore = - Backstore::initialize(pool_name, pool_uuid, devices, MDADataSize::default(), None) + Backstore::initialize_v1(pool_name, pool_uuid, devices, MDADataSize::default(), None) .unwrap(); let mut pool = ThinPool::new( pool_uuid, @@ -2258,7 +2258,7 @@ mod tests { let devices = get_devices(paths).unwrap(); - let mut backstore = Backstore::initialize( + let mut backstore = Backstore::initialize_v1( Name::new(pool_name.to_string()), pool_uuid, devices,