Skip to content

Commit

Permalink
Add abstraction layer for backstore
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Apr 10, 2023
1 parent a673c3e commit 6acfa9f
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/engine/strat_engine/backstore/backstore/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod shared;
pub mod v1;

pub use shared::Backstore;
333 changes: 333 additions & 0 deletions src/engine/strat_engine/backstore/backstore/shared.rs
Original file line number Diff line number Diff line change
@@ -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<StratBlockDev>,
cachedevs: Vec<StratBlockDev>,
last_update_time: DateTime<Utc>,
) -> BDARecordResult<Backstore> {
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<Backstore> {
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<Vec<DevUuid>> {
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<Vec<DevUuid>> {
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<Vec<DevUuid>> {
match self {
Backstore::V1(b) => b.add_datadevs(pool_name, pool_uuid, devices),
}
}

pub fn request_alloc(
&mut self,
sizes: &[Sectors],
) -> StratisResult<Option<RequestTransaction>> {
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<DevUuid, BDA> {
match self {
Backstore::V1(b) => b.into_bdas(),
}
}

pub fn drain_bds(&mut self) -> Vec<StratBlockDev> {
match self {
Backstore::V1(b) => b.drain_bds(),
}
}

pub fn device(&self) -> Option<Device> {
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<Option<DevUuid>> {
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<PoolEncryptionInfo> {
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<bool> {
match self {
Backstore::V1(b) => b.bind_clevis(pin, clevis_info),
}
}

pub fn unbind_clevis(&mut self) -> StratisResult<bool> {
match self {
Backstore::V1(b) => b.unbind_clevis(),
}
}

pub fn bind_keyring(&mut self, key_desc: &KeyDescription) -> StratisResult<bool> {
match self {
Backstore::V1(b) => b.bind_keyring(key_desc),
}
}

pub fn unbind_keyring(&mut self) -> StratisResult<bool> {
match self {
Backstore::V1(b) => b.unbind_keyring(),
}
}

pub fn rebind_keyring(&mut self, key_desc: &KeyDescription) -> StratisResult<Option<bool>> {
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<bool> {
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<BlockSizeSummary> {
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<Value> for &'a Backstore {
fn into(self) -> Value {
match self {
Backstore::V1(b) => b.into(),
}
}
}

impl Recordable<BackstoreSave> for Backstore {
fn record(&self) -> BackstoreSave {
match self {
Backstore::V1(b) => b.record(),
}
}
}
4 changes: 2 additions & 2 deletions src/engine/strat_engine/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 6acfa9f

Please sign in to comment.