Skip to content

Commit

Permalink
Add field for metadata version in static header
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Mar 10, 2023
1 parent 8f24169 commit 77fa3d0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/engine/strat_engine/backstore/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
BDA,
},
names::KeyDescription,
types::StratSigblockVersion,
udev::{
block_device_apply, decide_ownership, get_udev_property, UdevOwnership,
STRATIS_FS_TYPE,
Expand Down Expand Up @@ -572,6 +573,7 @@ pub fn initialize_devices(
};

let bda = BDA::new(
StratSigblockVersion::V1,
StratisIdentifiers::new(pool_uuid, dev_uuid),
mda_data_size,
data_size,
Expand Down
15 changes: 13 additions & 2 deletions src/engine/strat_engine/metadata/bda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
sizes::{BDAExtendedSize, BlockdevSize, MDADataSize, STATIC_HEADER_SIZE},
static_header::{MetadataLocation, StaticHeader, StratisIdentifiers},
},
types::StratSigblockVersion,
writing::SyncAll,
},
types::{DevUuid, PoolUuid},
Expand All @@ -30,6 +31,7 @@ pub struct BDA {
impl Default for BDA {
fn default() -> BDA {
BDA::new(
StratSigblockVersion::V1,
StratisIdentifiers::new(PoolUuid::nil(), DevUuid::nil()),
MDADataSize::default(),
BlockdevSize::default(),
Expand All @@ -40,13 +42,19 @@ impl Default for BDA {

impl BDA {
pub fn new(
sigblock_version: StratSigblockVersion,
identifiers: StratisIdentifiers,
mda_data_size: MDADataSize,
blkdev_size: BlockdevSize,
initialization_time: DateTime<Utc>,
) -> BDA {
let header =
StaticHeader::new(identifiers, mda_data_size, blkdev_size, initialization_time);
let header = StaticHeader::new(
sigblock_version,
identifiers,
mda_data_size,
blkdev_size,
initialization_time,
);

let regions = mda::MDARegions::new(header.mda_size);

Expand Down Expand Up @@ -172,6 +180,7 @@ mod tests {
let mut buf = Cursor::new(vec![0; buf_size]);

let bda = BDA::new(
StratSigblockVersion::V1,
sh.identifiers,
sh.mda_size.region_size().data_size(),
sh.blkdev_size,
Expand Down Expand Up @@ -203,6 +212,7 @@ mod tests {
)
]);
let mut bda = BDA::new(
StratSigblockVersion::V1,
sh.identifiers,
sh.mda_size.region_size().data_size(),
sh.blkdev_size,
Expand Down Expand Up @@ -253,6 +263,7 @@ mod tests {
let buf_size = convert_test!(*sh.mda_size.bda_size().sectors().bytes(), u128, usize);
let mut buf = Cursor::new(vec![0; buf_size]);
let mut bda = BDA::new(
StratSigblockVersion::V1,
sh.identifiers,
sh.mda_size.region_size().data_size(),
sh.blkdev_size,
Expand Down
18 changes: 9 additions & 9 deletions src/engine/strat_engine/metadata/static_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
static_header_size, BDAExtendedSize, BlockdevSize, MDADataSize, MDASize,
ReservedSize,
},
types::StratSigblockVersion,
writing::SyncAll,
},
types::{DevUuid, PoolUuid},
Expand All @@ -34,8 +35,6 @@ const RESERVED_SECTORS: Sectors = Sectors(3 * IEC::Mi / (SECTOR_SIZE as u64)); /

const STRAT_MAGIC: &[u8] = b"!Stra0tis\x86\xff\x02^\x41rh";

const STRAT_SIGBLOCK_VERSION: u8 = 1;

const CASTAGNOLI: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI);

/// Data structure to hold results of reading and parsing a signature buffer.
Expand Down Expand Up @@ -133,6 +132,7 @@ where
#[derive(Debug, Eq, PartialEq)]
pub struct StaticHeader {
pub blkdev_size: BlockdevSize,
pub sigblock_version: StratSigblockVersion,
pub identifiers: StratisIdentifiers,
pub mda_size: MDASize,
pub reserved_size: ReservedSize,
Expand All @@ -142,13 +142,15 @@ pub struct StaticHeader {

impl StaticHeader {
pub fn new(
sigblock_version: StratSigblockVersion,
identifiers: StratisIdentifiers,
mda_data_size: MDADataSize,
blkdev_size: BlockdevSize,
initialization_time: DateTime<Utc>,
) -> StaticHeader {
StaticHeader {
blkdev_size,
sigblock_version,
identifiers,
mda_size: mda_data_size.region_size().mda_size(),
reserved_size: ReservedSize::new(RESERVED_SECTORS),
Expand Down Expand Up @@ -496,7 +498,7 @@ impl StaticHeader {
let mut buf = [0u8; bytes!(static_header_size::SIGBLOCK_SECTORS)];
buf[4..20].clone_from_slice(STRAT_MAGIC);
LittleEndian::write_u64(&mut buf[20..28], *self.blkdev_size.sectors());
buf[28] = STRAT_SIGBLOCK_VERSION;
buf[28] = self.sigblock_version as u8;
buf[32..64].clone_from_slice(uuid_to_string!(self.identifiers.pool_uuid).as_bytes());
buf[64..96].clone_from_slice(uuid_to_string!(self.identifiers.device_uuid).as_bytes());
LittleEndian::write_u64(&mut buf[96..104], *self.mda_size.sectors());
Expand Down Expand Up @@ -530,12 +532,8 @@ impl StaticHeader {

let blkdev_size = BlockdevSize::new(Sectors(LittleEndian::read_u64(&buf[20..28])));

let version = buf[28];
if version != STRAT_SIGBLOCK_VERSION {
return Err(StratisError::Msg(format!(
"Unknown sigblock version: {version}"
)));
}
let version_buf = buf[28];
let version = StratSigblockVersion::try_from(version_buf)?;

let pool_uuid = PoolUuid::parse_str(from_utf8(&buf[32..64])?)?;
let dev_uuid = DevUuid::parse_str(from_utf8(&buf[64..96])?)?;
Expand All @@ -547,6 +545,7 @@ impl StaticHeader {
Ok(Some(StaticHeader {
identifiers: StratisIdentifiers::new(pool_uuid, dev_uuid),
blkdev_size,
sigblock_version: version,
mda_size,
reserved_size: ReservedSize::new(Sectors(LittleEndian::read_u64(&buf[104..112]))),
flags: 0,
Expand Down Expand Up @@ -623,6 +622,7 @@ pub mod tests {
MDADataSize::new(MDADataSize::default().bytes() + Bytes::from(mda_size_factor * 4));
let blkdev_size = (Bytes::from(IEC::Mi) + Sectors(blkdev_size).bytes()).sectors();
StaticHeader::new(
StratSigblockVersion::V1,
StratisIdentifiers::new(pool_uuid, dev_uuid),
mda_data_size,
BlockdevSize::new(blkdev_size),
Expand Down
20 changes: 20 additions & 0 deletions src/engine/strat_engine/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,23 @@ use crate::{

pub type BDAResult<T> = Result<T, (StratisError, BDA)>;
pub type BDARecordResult<T> = Result<T, (StratisError, HashMap<DevUuid, BDA>)>;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StratSigblockVersion {
V1 = 1,
V2 = 2,
}

impl TryFrom<u8> for StratSigblockVersion {
type Error = StratisError;

fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
i if i == StratSigblockVersion::V1 as u8 => Ok(StratSigblockVersion::V1),
i if i == StratSigblockVersion::V2 as u8 => Ok(StratSigblockVersion::V2),
_ => Err(StratisError::Msg(format!(
"Unknown sigblock version: {value}"
))),
}
}
}

0 comments on commit 77fa3d0

Please sign in to comment.