use core::fmt;
use core::fmt::Display;
#[cfg(feature = "std")]
use std::error;
use crate::phys::SECTOR_SHIFT;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VdevType {
Disk,
DRaid,
DRaidSpare,
File,
Hole,
Indirect,
L2Cache,
Log,
Mirror,
Missing,
RaidZ,
Replacing,
Root,
Spare,
}
impl Display for VdevType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: &'static str = (*self).into();
write!(f, "{s}")
}
}
impl From<VdevType> for &'static str {
fn from(val: VdevType) -> &'static str {
match val {
VdevType::Root => "root",
VdevType::Mirror => "mirror",
VdevType::Replacing => "replacing",
VdevType::RaidZ => "raidz",
VdevType::Disk => "disk",
VdevType::File => "file",
VdevType::Missing => "missing",
VdevType::Spare => "spare",
VdevType::Log => "log",
VdevType::L2Cache => "l2cache",
VdevType::Hole => "hole",
VdevType::Indirect => "indirect",
VdevType::DRaid => "draid",
VdevType::DRaidSpare => "dspare",
}
}
}
impl TryFrom<&str> for VdevType {
type Error = VdevTypeDecodeError;
fn try_from(vdev_type: &str) -> Result<Self, Self::Error> {
match vdev_type {
"root" => Ok(VdevType::Root),
"mirror" => Ok(VdevType::Mirror),
"replacing" => Ok(VdevType::Replacing),
"raidz" => Ok(VdevType::RaidZ),
"disk" => Ok(VdevType::Disk),
"file" => Ok(VdevType::File),
"missing" => Ok(VdevType::Missing),
"spare" => Ok(VdevType::Spare),
"log" => Ok(VdevType::Log),
"l2cache" => Ok(VdevType::L2Cache),
"hole" => Ok(VdevType::Hole),
"indirect" => Ok(VdevType::Indirect),
"draid" => Ok(VdevType::DRaid),
"dspare" => Ok(VdevType::DRaidSpare),
_ => Err(VdevTypeDecodeError::Unknown {}),
}
}
}
#[derive(Debug)]
pub enum VdevTypeDecodeError {
Unknown {},
}
impl fmt::Display for VdevTypeDecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VdevTypeDecodeError::Unknown {} => {
write!(f, "VdevType decode error, unknown type")
}
}
}
}
#[cfg(feature = "std")]
impl error::Error for VdevTypeDecodeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VdevTreeKey {
AllocateShift,
AllocateSize,
Children,
CreateTxg,
DevId,
Guid,
Id,
IsLog,
MetaSlabArray,
MetaSlabShift,
NParity,
Path,
PhysPath,
VdevType,
WholeDisk,
}
impl VdevTreeKey {
const ALL: [VdevTreeKey; 14] = [
VdevTreeKey::AllocateShift,
VdevTreeKey::AllocateSize,
VdevTreeKey::Children,
VdevTreeKey::CreateTxg,
VdevTreeKey::DevId,
VdevTreeKey::Guid,
VdevTreeKey::Id,
VdevTreeKey::IsLog,
VdevTreeKey::MetaSlabArray,
VdevTreeKey::MetaSlabShift,
VdevTreeKey::Path,
VdevTreeKey::PhysPath,
VdevTreeKey::VdevType,
VdevTreeKey::WholeDisk,
];
pub fn all() -> &'static [VdevTreeKey] {
&VdevTreeKey::ALL
}
}
impl Display for VdevTreeKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: &'static str = (*self).into();
write!(f, "{s}")
}
}
impl From<VdevTreeKey> for &'static str {
fn from(val: VdevTreeKey) -> &'static str {
match val {
VdevTreeKey::AllocateShift => "ashift",
VdevTreeKey::AllocateSize => "asize",
VdevTreeKey::Children => "children",
VdevTreeKey::CreateTxg => "create_txg",
VdevTreeKey::DevId => "devid",
VdevTreeKey::Guid => "guid",
VdevTreeKey::Id => "id",
VdevTreeKey::IsLog => "is_log",
VdevTreeKey::MetaSlabArray => "metaslab_array",
VdevTreeKey::MetaSlabShift => "metaslab_shift",
VdevTreeKey::NParity => "nparity",
VdevTreeKey::Path => "path",
VdevTreeKey::PhysPath => "phys_path",
VdevTreeKey::VdevType => "type",
VdevTreeKey::WholeDisk => "whole_disk",
}
}
}
impl TryFrom<&str> for VdevTreeKey {
type Error = VdevTreeKeyDecodeError;
fn try_from(feature: &str) -> Result<Self, Self::Error> {
match feature {
"ashift" => Ok(VdevTreeKey::AllocateShift),
"asize" => Ok(VdevTreeKey::AllocateSize),
"create_txg" => Ok(VdevTreeKey::CreateTxg),
"children" => Ok(VdevTreeKey::Children),
"devid" => Ok(VdevTreeKey::DevId),
"guid" => Ok(VdevTreeKey::Guid),
"id" => Ok(VdevTreeKey::Id),
"is_log" => Ok(VdevTreeKey::IsLog),
"metaslab_array" => Ok(VdevTreeKey::MetaSlabArray),
"metaslab_shift" => Ok(VdevTreeKey::MetaSlabShift),
"nparity" => Ok(VdevTreeKey::NParity),
"path" => Ok(VdevTreeKey::Path),
"phys_path" => Ok(VdevTreeKey::PhysPath),
"type" => Ok(VdevTreeKey::VdevType),
"whole_disk" => Ok(VdevTreeKey::WholeDisk),
_ => Err(VdevTreeKeyDecodeError::Unknown {}),
}
}
}
#[derive(Debug)]
pub enum VdevTreeKeyDecodeError {
Unknown {},
}
impl fmt::Display for VdevTreeKeyDecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VdevTreeKeyDecodeError::Unknown {} => {
write!(f, "VdevTreeKey decode error, unknown key")
}
}
}
}
#[cfg(feature = "std")]
impl error::Error for VdevTreeKeyDecodeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
#[derive(Debug)]
pub struct VdevTree {}
impl VdevTree {
pub const ASHIFT_MIN: u32 = SECTOR_SHIFT;
pub const ASHIFT_MAX: u32 = 16;
}