use core::fmt;
pub const LARGE_FILE_MIN_SIZE: u64 = 0x8000_0000;
pub(crate) const fn resize_inode_size(block_size: u32) -> u64 {
let apb = block_size as u64 / 4; block_size as u64 * (12 + apb + apb * apb)
}
macro_rules! feature_word {
(
$(#[$ty_doc:meta])*
$name:ident {
$(
$(#[$flag_doc:meta])*
$flag:ident($on_disk:literal) = $value:expr
),* $(,)?
}
) => {
$(#[$ty_doc])*
#[derive(Clone, Copy, PartialEq, Eq, Default, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct $name(u32);
impl $name {
pub const NONE: Self = Self(0);
$(
$(#[$flag_doc])*
pub const $flag: Self = Self($value);
)*
const FLAGS: &'static [(&'static str, &'static str, u32)] = &[
$((stringify!($flag), $on_disk, $value),)*
];
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
pub const fn without(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
#[must_use]
pub const fn unknown_bits(self) -> u32 {
let mut known = 0u32;
let mut i = 0;
while i < Self::FLAGS.len() {
known |= Self::FLAGS[i].2;
i += 1;
}
self.0 & !known
}
#[must_use]
pub fn names(self) -> Vec<&'static str> {
Self::FLAGS
.iter()
.filter(|(_, _, bit)| *bit != 0 && self.0 & bit == *bit)
.map(|(_, name, _)| *name)
.collect()
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
Self::FLAGS
.iter()
.find(|(_, on_disk, _)| *on_disk == name)
.map(|(_, _, bit)| Self(*bit))
}
}
impl core::ops::BitOr for $name {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl core::ops::BitOrAssign for $name {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, concat!(stringify!($name), "("))?;
let mut first = true;
for (name, _, bit) in Self::FLAGS {
if self.0 & bit == *bit && *bit != 0 {
if !first {
write!(f, " | ")?;
}
write!(f, "{name}")?;
first = false;
}
}
let unknown = self.unknown_bits();
if unknown != 0 {
if !first {
write!(f, " | ")?;
}
write!(f, "{unknown:#x}")?;
first = false;
}
if first {
write!(f, "NONE")?;
}
write!(f, ")")
}
}
};
}
feature_word! {
Compat {
HAS_JOURNAL("has_journal") = 0x0004,
EXT_ATTR("ext_attr") = 0x0008,
RESIZE_INODE("resize_inode") = 0x0010,
DIR_INDEX("dir_index") = 0x0020,
ORPHAN_FILE("orphan_file") = 0x1000,
}
}
feature_word! {
Incompat {
FILETYPE("filetype") = 0x0002,
META_BG("meta_bg") = 0x0010,
EXTENTS("extent") = 0x0040,
SIXTY_FOUR_BIT("64bit") = 0x0080,
FLEX_BG("flex_bg") = 0x0200,
CSUM_SEED("metadata_csum_seed") = 0x2000,
}
}
feature_word! {
RoCompat {
SPARSE_SUPER("sparse_super") = 0x0001,
LARGE_FILE("large_file") = 0x0002,
HUGE_FILE("huge_file") = 0x0008,
DIR_NLINK("dir_nlink") = 0x0020,
EXTRA_ISIZE("extra_isize") = 0x0040,
METADATA_CSUM("metadata_csum") = 0x0400,
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FeatureSet {
pub compat: Compat,
pub incompat: Incompat,
pub ro_compat: RoCompat,
pub block_size: u32,
pub inode_size: u16,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FeatureError {
#[error(
"meta_bg is never planned: it is the online group-descriptor layout that \
reserved GDT blocks exist to avoid"
)]
MetaBg,
#[error(
"{feature} requires extent mapping: it belongs to the ext4 feature layer, not the \
block-mapped ext2/ext3 baseline"
)]
#[non_exhaustive]
RequiresExtents {
feature: &'static str,
},
#[error(
"flex_bg is required with extent mapping: the extent formatter packs each flex \
block group's bitmaps and inode tables together"
)]
FlexBgRequired,
#[error("filetype is required: this formatter records a file type in every directory entry")]
FiletypeRequired,
#[error(
"metadata_csum_seed requires metadata_csum: the seed it stores has no checksums to serve"
)]
CsumSeedWithoutMetadataCsum,
#[error("orphan_file requires has_journal: its entries are written through the journal")]
OrphanFileWithoutJournal,
#[error(
"resize_inode requires large_file at a {block_size}-byte block: its resize inode \
declares {size} bytes, which is a large file"
)]
#[non_exhaustive]
ResizeInodeRequiresLargeFile {
block_size: u32,
size: u64,
},
#[error("unsupported block size {block_size}: expected 1024, 2048, or 4096")]
#[non_exhaustive]
BlockSize {
block_size: u32,
},
#[error("unsupported inode size {inode_size}: expected a power of two in 128..=block_size")]
#[non_exhaustive]
InodeSize {
inode_size: u16,
},
}
impl FeatureSet {
pub const EMPTY: Self = Self {
compat: Compat::NONE,
incompat: Incompat::NONE,
ro_compat: RoCompat::NONE,
block_size: 4096,
inode_size: 256,
};
pub const DEFAULT: Self = Self {
compat: Compat(
Compat::HAS_JOURNAL.0
| Compat::EXT_ATTR.0
| Compat::RESIZE_INODE.0
| Compat::DIR_INDEX.0
| Compat::ORPHAN_FILE.0,
),
incompat: Incompat(
Incompat::FILETYPE.0
| Incompat::EXTENTS.0
| Incompat::SIXTY_FOUR_BIT.0
| Incompat::FLEX_BG.0
| Incompat::CSUM_SEED.0,
),
ro_compat: RoCompat(
RoCompat::SPARSE_SUPER.0
| RoCompat::LARGE_FILE.0
| RoCompat::HUGE_FILE.0
| RoCompat::DIR_NLINK.0
| RoCompat::EXTRA_ISIZE.0
| RoCompat::METADATA_CSUM.0,
),
block_size: 4096,
inode_size: 256,
};
pub const LATEST: Self = Self::DEFAULT;
pub const EXT2: Self = Self {
compat: Compat(Compat::EXT_ATTR.0 | Compat::RESIZE_INODE.0 | Compat::DIR_INDEX.0),
incompat: Incompat(Incompat::FILETYPE.0),
ro_compat: RoCompat(RoCompat::SPARSE_SUPER.0 | RoCompat::LARGE_FILE.0),
block_size: 4096,
inode_size: 256,
};
pub const EXT3: Self = Self {
compat: Compat(Self::EXT2.compat.0 | Compat::HAS_JOURNAL.0),
..Self::EXT2
};
#[must_use]
pub const fn with_block_size(mut self, block_size: u32) -> Self {
self.block_size = block_size;
self
}
#[must_use]
pub const fn with_inode_size(mut self, inode_size: u16) -> Self {
self.inode_size = inode_size;
self
}
#[must_use]
pub const fn desc_size(self) -> u16 {
if self.incompat.contains(Incompat::SIXTY_FOUR_BIT) {
64
} else {
32
}
}
#[must_use]
pub const fn is_64bit(self) -> bool {
self.incompat.contains(Incompat::SIXTY_FOUR_BIT)
}
#[must_use]
pub const fn has_extents(self) -> bool {
self.incompat.contains(Incompat::EXTENTS)
}
#[must_use]
pub const fn is_sparse_super(self) -> bool {
self.ro_compat.contains(RoCompat::SPARSE_SUPER)
}
#[must_use]
pub const fn has_filetype(self) -> bool {
self.incompat.contains(Incompat::FILETYPE)
}
#[must_use]
pub const fn has_flex_bg(self) -> bool {
self.incompat.contains(Incompat::FLEX_BG)
}
#[must_use]
pub const fn has_journal(self) -> bool {
self.compat.contains(Compat::HAS_JOURNAL)
}
#[must_use]
pub const fn has_metadata_csum(self) -> bool {
self.ro_compat.contains(RoCompat::METADATA_CSUM)
}
#[must_use]
pub const fn has_dir_nlink(self) -> bool {
self.ro_compat.contains(RoCompat::DIR_NLINK)
}
#[must_use]
pub const fn has_ext_attr(self) -> bool {
self.compat.contains(Compat::EXT_ATTR)
}
#[must_use]
pub const fn has_large_file(self) -> bool {
self.ro_compat.contains(RoCompat::LARGE_FILE)
}
#[must_use]
pub const fn has_resize_inode(self) -> bool {
self.compat.contains(Compat::RESIZE_INODE)
}
#[must_use]
pub const fn has_dir_index(self) -> bool {
self.compat.contains(Compat::DIR_INDEX)
}
#[must_use]
pub const fn has_orphan_file(self) -> bool {
self.compat.contains(Compat::ORPHAN_FILE)
}
#[must_use]
pub const fn has_csum_seed(self) -> bool {
self.incompat.contains(Incompat::CSUM_SEED)
}
#[must_use]
pub const fn inodes_per_block(self) -> u32 {
if self.inode_size == 0 {
return 0;
}
self.block_size / self.inode_size as u32
}
#[must_use]
pub fn names(self) -> Vec<&'static str> {
let mut out = self.compat.names();
out.extend(self.incompat.names());
out.extend(self.ro_compat.names());
out
}
#[must_use]
pub fn pin(self) -> String {
let Self {
compat,
incompat,
ro_compat,
block_size,
inode_size,
} = self;
let mut out = String::from("ferrosys-feature-pin 1\n");
push_pin_word(
&mut out,
"compat",
compat.bits(),
&compat.names(),
compat.unknown_bits(),
);
push_pin_word(
&mut out,
"incompat",
incompat.bits(),
&incompat.names(),
incompat.unknown_bits(),
);
push_pin_word(
&mut out,
"ro_compat",
ro_compat.bits(),
&ro_compat.names(),
ro_compat.unknown_bits(),
);
out.push_str("block_size ");
out.push_str(&block_size.to_string());
out.push_str("\ninode_size ");
out.push_str(&inode_size.to_string());
out.push('\n');
out
}
#[allow(clippy::question_mark)]
#[must_use]
pub fn with_feature(mut self, name: &str, on: bool) -> Option<Self> {
if let Some(flag) = Compat::from_name(name) {
self.compat = if on {
self.compat | flag
} else {
self.compat.without(flag)
};
} else if let Some(flag) = Incompat::from_name(name) {
self.incompat = if on {
self.incompat | flag
} else {
self.incompat.without(flag)
};
} else if let Some(flag) = RoCompat::from_name(name) {
self.ro_compat = if on {
self.ro_compat | flag
} else {
self.ro_compat.without(flag)
};
} else {
return None;
}
Some(self)
}
pub const fn validate(self) -> Result<(), FeatureError> {
if self.incompat.contains(Incompat::META_BG) {
return Err(FeatureError::MetaBg);
}
if !self.has_filetype() {
return Err(FeatureError::FiletypeRequired);
}
if self.has_csum_seed() && !self.has_metadata_csum() {
return Err(FeatureError::CsumSeedWithoutMetadataCsum);
}
if self.has_orphan_file() && !self.has_journal() {
return Err(FeatureError::OrphanFileWithoutJournal);
}
match self.block_size {
1024 | 2048 | 4096 => {}
other => return Err(FeatureError::BlockSize { block_size: other }),
}
let isize = self.inode_size as u32;
if isize < 128
|| !isize.is_power_of_two()
|| isize > self.block_size
|| !self.block_size.is_multiple_of(isize)
{
return Err(FeatureError::InodeSize {
inode_size: self.inode_size,
});
}
let resize_size = resize_inode_size(self.block_size);
if self.has_resize_inode() && !self.has_large_file() && resize_size >= LARGE_FILE_MIN_SIZE {
return Err(FeatureError::ResizeInodeRequiresLargeFile {
block_size: self.block_size,
size: resize_size,
});
}
if self.has_extents() {
if !self.has_flex_bg() {
return Err(FeatureError::FlexBgRequired);
}
} else {
if self.has_flex_bg() {
return Err(FeatureError::RequiresExtents { feature: "flex_bg" });
}
if self.is_64bit() {
return Err(FeatureError::RequiresExtents { feature: "64bit" });
}
if self.has_metadata_csum() {
return Err(FeatureError::RequiresExtents {
feature: "metadata_csum",
});
}
if self.has_csum_seed() {
return Err(FeatureError::RequiresExtents {
feature: "metadata_csum_seed",
});
}
if self.ro_compat.contains(RoCompat::HUGE_FILE) {
return Err(FeatureError::RequiresExtents {
feature: "huge_file",
});
}
if self.has_dir_nlink() {
return Err(FeatureError::RequiresExtents {
feature: "dir_nlink",
});
}
if self.ro_compat.contains(RoCompat::EXTRA_ISIZE) {
return Err(FeatureError::RequiresExtents {
feature: "extra_isize",
});
}
if self.has_orphan_file() {
return Err(FeatureError::RequiresExtents {
feature: "orphan_file",
});
}
}
Ok(())
}
}
impl Default for FeatureSet {
fn default() -> Self {
Self::DEFAULT
}
}
fn push_pin_word(out: &mut String, label: &str, bits: u32, names: &[&str], unknown: u32) {
out.push_str(label);
out.push_str(" 0x");
out.push_str(&format!("{bits:08x}"));
for name in names {
out.push(' ');
out.push_str(name);
}
if unknown != 0 {
out.push_str(" unknown:0x");
out.push_str(&format!("{unknown:08x}"));
}
out.push('\n');
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Profile {
Ext2,
Ext3,
#[default]
Ext4,
}
impl Profile {
#[must_use]
pub const fn feature_set(self) -> FeatureSet {
match self {
Profile::Ext2 => FeatureSet::EXT2,
Profile::Ext3 => FeatureSet::EXT3,
Profile::Ext4 => FeatureSet::DEFAULT,
}
}
#[must_use]
pub const fn of(features: FeatureSet) -> Self {
if features.has_extents() {
Profile::Ext4
} else if features.has_journal() {
Profile::Ext3
} else {
Profile::Ext2
}
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Profile::Ext2 => "ext2",
Profile::Ext3 => "ext3",
Profile::Ext4 => "ext4",
}
}
}
impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_set_is_frozen() {
let fs = FeatureSet::default();
assert_eq!(
fs.compat.bits(),
0x103c,
"has_journal | ext_attr | resize_inode | dir_index | orphan_file"
);
assert_eq!(
fs.incompat.bits(),
0x22c2,
"filetype | extents | 64bit | flex_bg | metadata_csum_seed"
);
assert_eq!(
fs.ro_compat.bits(),
0x46b,
"sparse_super | large_file | huge_file | dir_nlink | extra_isize | metadata_csum"
);
assert_eq!(fs.block_size, 4096);
assert_eq!(fs.inode_size, 256);
assert_eq!(fs, FeatureSet::DEFAULT);
assert_eq!(Profile::Ext4.feature_set(), FeatureSet::DEFAULT);
}
#[test]
fn latest_still_matches_the_frozen_default() {
assert_eq!(FeatureSet::LATEST, FeatureSet::DEFAULT);
}
#[test]
fn the_empty_set_carries_no_features() {
let empty = FeatureSet::EMPTY;
assert_eq!(empty.compat.bits(), 0);
assert_eq!(empty.incompat.bits(), 0);
assert_eq!(empty.ro_compat.bits(), 0);
assert!(empty.names().is_empty());
assert_eq!(empty.block_size, 4096);
assert_eq!(empty.inode_size, 256);
assert!(matches!(
empty.validate(),
Err(FeatureError::FiletypeRequired)
));
}
#[test]
fn the_geometry_builders_replace_one_value_each() {
let fs = FeatureSet::DEFAULT
.with_block_size(1024)
.with_inode_size(128);
assert_eq!(fs.block_size, 1024);
assert_eq!(fs.inode_size, 128);
assert_eq!(fs.compat, FeatureSet::DEFAULT.compat);
assert_eq!(fs.incompat, FeatureSet::DEFAULT.incompat);
assert_eq!(fs.ro_compat, FeatureSet::DEFAULT.ro_compat);
}
#[test]
fn the_pin_document_is_the_recorded_contract() {
assert_eq!(
FeatureSet::DEFAULT.pin(),
"ferrosys-feature-pin 1\n\
compat 0x0000103c has_journal ext_attr resize_inode dir_index orphan_file\n\
incompat 0x000022c2 filetype extent 64bit flex_bg metadata_csum_seed\n\
ro_compat 0x0000046b sparse_super large_file huge_file dir_nlink extra_isize \
metadata_csum\n\
block_size 4096\n\
inode_size 256\n"
);
assert_eq!(
FeatureSet::EXT2.pin(),
"ferrosys-feature-pin 1\n\
compat 0x00000038 ext_attr resize_inode dir_index\n\
incompat 0x00000002 filetype\n\
ro_compat 0x00000003 sparse_super large_file\n\
block_size 4096\n\
inode_size 256\n"
);
assert_eq!(
FeatureSet::EMPTY.pin(),
"ferrosys-feature-pin 1\n\
compat 0x00000000\n\
incompat 0x00000000\n\
ro_compat 0x00000000\n\
block_size 4096\n\
inode_size 256\n"
);
}
#[test]
fn the_pin_records_geometry_the_feature_names_drop() {
let a = FeatureSet::DEFAULT;
let b = FeatureSet::DEFAULT
.with_block_size(1024)
.with_inode_size(128);
assert_eq!(a.names(), b.names());
assert_ne!(a.pin(), b.pin());
}
#[test]
fn the_pin_names_replay_onto_an_empty_set() {
for set in [FeatureSet::DEFAULT, FeatureSet::EXT2, FeatureSet::EXT3] {
let replayed = set
.names()
.iter()
.try_fold(FeatureSet::EMPTY, |acc, name| acc.with_feature(name, true))
.expect("every name a set emits is a name a word resolves");
assert_eq!(replayed.compat, set.compat);
assert_eq!(replayed.incompat, set.incompat);
assert_eq!(replayed.ro_compat, set.ro_compat);
}
}
#[test]
fn the_pin_reports_a_bit_no_word_names() {
let mut fs = FeatureSet::EMPTY;
fs.incompat = Incompat::from_bits(Incompat::FILETYPE.bits() | 0x0080_0000);
let pinned = fs.pin();
assert!(
pinned.contains("incompat 0x00800002 filetype unknown:0x00800000\n"),
"{pinned}"
);
}
#[test]
fn ext2_ext3_baselines_match_mke2fs() {
let ext2 = FeatureSet::EXT2;
assert_eq!(
ext2.compat.bits(),
0x0038,
"ext_attr | resize_inode | dir_index"
);
assert_eq!(ext2.incompat.bits(), 0x0002, "filetype");
assert_eq!(ext2.ro_compat.bits(), 0x0003, "sparse_super | large_file");
assert_eq!(ext2.block_size, 4096);
assert_eq!(ext2.inode_size, 256);
let ext3 = FeatureSet::EXT3;
assert_eq!(ext3.compat.bits(), 0x003c, "ext2 plus has_journal");
assert_eq!(ext3.incompat.bits(), 0x0002, "filetype");
assert_eq!(ext3.ro_compat.bits(), 0x0003, "sparse_super | large_file");
assert_eq!(ext3.block_size, 4096);
assert_eq!(ext3.inode_size, 256);
assert_eq!(
ext3,
FeatureSet {
compat: ext2.compat | Compat::HAS_JOURNAL,
..ext2
},
"ext3 must differ from ext2 only by has_journal"
);
}
#[test]
fn profile_seeds_the_matching_baseline() {
assert_eq!(Profile::Ext2.feature_set(), FeatureSet::EXT2);
assert_eq!(Profile::Ext3.feature_set(), FeatureSet::EXT3);
assert_eq!(Profile::Ext4.feature_set(), FeatureSet::DEFAULT);
assert_eq!(Profile::default(), Profile::Ext4);
assert_eq!(Profile::default().feature_set(), FeatureSet::default());
}
#[test]
fn every_profile_round_trips_through_its_feature_set() {
for profile in [Profile::Ext2, Profile::Ext3, Profile::Ext4] {
assert_eq!(Profile::of(profile.feature_set()), profile);
}
}
#[test]
fn of_classifies_on_the_extent_then_journal_discriminator() {
assert_eq!(Profile::of(FeatureSet::DEFAULT), Profile::Ext4);
let extent_no_journal = FeatureSet::DEFAULT
.with_feature("has_journal", false)
.expect("a known name");
assert!(extent_no_journal.has_extents() && !extent_no_journal.has_journal());
assert_eq!(Profile::of(extent_no_journal), Profile::Ext4);
assert_eq!(Profile::of(FeatureSet::EXT3), Profile::Ext3);
assert_eq!(Profile::of(FeatureSet::EXT2), Profile::Ext2);
}
#[test]
fn of_reads_the_words_not_the_seed() {
let downgraded = Profile::Ext4
.feature_set()
.with_feature("extent", false)
.expect("a known name");
assert_eq!(Profile::of(downgraded), Profile::Ext3);
let bare = downgraded
.with_feature("has_journal", false)
.expect("a known name");
assert_eq!(Profile::of(bare), Profile::Ext2);
}
#[test]
fn profile_names_are_the_mke2fs_t_vocabulary() {
assert_eq!(Profile::Ext2.name(), "ext2");
assert_eq!(Profile::Ext3.name(), "ext3");
assert_eq!(Profile::Ext4.name(), "ext4");
assert_eq!(Profile::Ext4.to_string(), "ext4");
assert_eq!(format!("{}", Profile::Ext2), "ext2");
}
#[test]
fn a_feature_that_serves_another_is_rejected_without_it() {
let mut fs = FeatureSet::default();
fs.ro_compat = RoCompat::from_bits(fs.ro_compat.bits() & !RoCompat::METADATA_CSUM.bits());
assert_eq!(
fs.validate(),
Err(FeatureError::CsumSeedWithoutMetadataCsum)
);
let mut fs = FeatureSet::default();
fs.compat = Compat::from_bits(fs.compat.bits() & !Compat::HAS_JOURNAL.bits());
assert_eq!(fs.validate(), Err(FeatureError::OrphanFileWithoutJournal));
let mut fs = FeatureSet::default();
fs.compat = Compat::from_bits(
fs.compat.bits() & !(Compat::HAS_JOURNAL.bits() | Compat::ORPHAN_FILE.bits()),
);
fs.incompat = Incompat::from_bits(fs.incompat.bits() & !Incompat::CSUM_SEED.bits());
fs.ro_compat = RoCompat::from_bits(fs.ro_compat.bits() & !RoCompat::METADATA_CSUM.bits());
assert_eq!(fs.validate(), Ok(()));
}
#[test]
fn default_derives_the_64bit_descriptor_width() {
let fs = FeatureSet::default();
assert!(fs.is_64bit());
assert_eq!(fs.desc_size(), 64);
assert_eq!(fs.inodes_per_block(), 16);
assert!(fs.has_extents());
assert!(fs.is_sparse_super());
assert!(fs.has_filetype());
assert!(fs.has_metadata_csum());
assert!(fs.has_journal());
}
#[test]
fn default_profile_validates() {
assert_eq!(FeatureSet::default().validate(), Ok(()));
}
#[test]
fn meta_bg_is_rejected() {
let mut fs = FeatureSet::default();
fs.incompat |= Incompat::META_BG;
assert_eq!(fs.validate(), Err(FeatureError::MetaBg));
}
#[test]
fn an_extent_set_still_requires_flex_bg_and_filetype() {
let fs = FeatureSet::DEFAULT
.with_feature("flex_bg", false)
.expect("a known name");
assert_eq!(fs.validate(), Err(FeatureError::FlexBgRequired));
let fs = FeatureSet::DEFAULT
.with_feature("filetype", false)
.expect("a known name");
assert_eq!(fs.validate(), Err(FeatureError::FiletypeRequired));
}
#[test]
fn the_non_extent_baselines_validate() {
assert_eq!(FeatureSet::EXT2.validate(), Ok(()));
assert_eq!(FeatureSet::EXT3.validate(), Ok(()));
}
#[test]
fn a_non_extent_set_refuses_every_ext4_layer_feature() {
for (name, present) in [
("flex_bg", "flex_bg"),
("64bit", "64bit"),
("metadata_csum", "metadata_csum"),
("huge_file", "huge_file"),
("dir_nlink", "dir_nlink"),
("extra_isize", "extra_isize"),
] {
let fs = FeatureSet::EXT2
.with_feature(name, true)
.expect("a known name");
assert_eq!(
fs.validate(),
Err(FeatureError::RequiresExtents { feature: present }),
"ext2 + {name} is refused as an ext4-layer feature"
);
}
let fs = FeatureSet::EXT2
.with_feature("metadata_csum", true)
.expect("a known name")
.with_feature("metadata_csum_seed", true)
.expect("a known name");
assert_eq!(
fs.validate(),
Err(FeatureError::RequiresExtents {
feature: "metadata_csum"
})
);
let fs = FeatureSet::EXT3
.with_feature("orphan_file", true)
.expect("a known name");
assert_eq!(
fs.validate(),
Err(FeatureError::RequiresExtents {
feature: "orphan_file"
})
);
}
#[test]
fn the_resize_inode_declares_the_size_mke2fs_writes() {
assert_eq!(resize_inode_size(1024), 67_383_296);
assert_eq!(resize_inode_size(2048), 537_944_064);
assert_eq!(resize_inode_size(4096), 4_299_210_752);
assert!(resize_inode_size(1024) < LARGE_FILE_MIN_SIZE);
assert!(resize_inode_size(2048) < LARGE_FILE_MIN_SIZE);
assert!(resize_inode_size(4096) >= LARGE_FILE_MIN_SIZE);
}
#[test]
fn the_resize_inode_requires_large_file_where_its_size_needs_it() {
let fs = FeatureSet::EXT2
.with_feature("large_file", false)
.expect("a known name");
assert_eq!(fs.block_size, 4096);
assert_eq!(
fs.validate(),
Err(FeatureError::ResizeInodeRequiresLargeFile {
block_size: 4096,
size: 4_299_210_752,
})
);
for block_size in [1024, 2048] {
let fs = FeatureSet { block_size, ..fs };
assert_eq!(
fs.validate(),
Ok(()),
"a {block_size}-byte block needs no large_file for its resize inode"
);
}
let fs = FeatureSet {
block_size: 4096,
..fs
}
.with_feature("resize_inode", false)
.expect("a known name");
assert_eq!(fs.validate(), Ok(()));
}
#[test]
fn unsupported_sizes_are_rejected() {
let fs = FeatureSet {
block_size: 3000,
..FeatureSet::default()
};
assert_eq!(
fs.validate(),
Err(FeatureError::BlockSize { block_size: 3000 })
);
let fs = FeatureSet {
inode_size: 100,
..FeatureSet::default()
};
assert_eq!(
fs.validate(),
Err(FeatureError::InodeSize { inode_size: 100 })
);
let mut fs = FeatureSet::default();
fs.incompat = Incompat::from_bits(fs.incompat.bits() & !Incompat::SIXTY_FOUR_BIT.bits());
assert_eq!(fs.desc_size(), 32);
assert!(!fs.is_64bit());
}
#[test]
fn inodes_per_block_survives_a_zero_inode_size() {
let fs = FeatureSet {
block_size: 4096,
inode_size: 0,
..FeatureSet::default()
};
assert_eq!(fs.inodes_per_block(), 0);
let fs = FeatureSet {
block_size: 4096,
inode_size: 256,
..FeatureSet::default()
};
assert_eq!(fs.inodes_per_block(), 16);
}
#[test]
fn contains_and_membership() {
let f = Incompat::FILETYPE | Incompat::EXTENTS;
assert!(f.contains(Incompat::FILETYPE));
assert!(f.contains(Incompat::EXTENTS));
assert!(!f.contains(Incompat::FLEX_BG));
assert!(!f.is_empty());
assert!(Incompat::NONE.is_empty());
}
#[test]
fn unknown_bits_flags_unrecognized_features() {
assert_eq!(FeatureSet::default().incompat.unknown_bits(), 0);
let mystery = Incompat::from_bits(Incompat::FILETYPE.bits() | 0x8000_0000);
assert_eq!(mystery.unknown_bits(), 0x8000_0000);
}
#[test]
fn debug_renders_flag_names() {
let f = Incompat::FILETYPE | Incompat::EXTENTS;
assert_eq!(format!("{f:?}"), "Incompat(FILETYPE | EXTENTS)");
assert_eq!(format!("{:?}", Incompat::NONE), "Incompat(NONE)");
}
#[test]
fn the_default_profile_names_its_features_on_disk() {
assert_eq!(
FeatureSet::DEFAULT.names(),
[
"has_journal",
"ext_attr",
"resize_inode",
"dir_index",
"orphan_file",
"filetype",
"extent",
"64bit",
"flex_bg",
"metadata_csum_seed",
"sparse_super",
"large_file",
"huge_file",
"dir_nlink",
"extra_isize",
"metadata_csum",
]
);
}
#[test]
fn every_name_round_trips_back_to_its_bit() {
for (name, bit) in [
("has_journal", Compat::HAS_JOURNAL),
("orphan_file", Compat::ORPHAN_FILE),
] {
assert_eq!(Compat::from_name(name), Some(bit));
assert_eq!(Incompat::from_name(name), None);
assert_eq!(RoCompat::from_name(name), None);
}
assert_eq!(Incompat::from_name("extent"), Some(Incompat::EXTENTS));
assert_eq!(Incompat::from_name("64bit"), Some(Incompat::SIXTY_FOUR_BIT));
assert_eq!(
Incompat::from_name("metadata_csum_seed"),
Some(Incompat::CSUM_SEED)
);
assert_eq!(
RoCompat::from_name("metadata_csum"),
Some(RoCompat::METADATA_CSUM)
);
assert_eq!(Incompat::from_name("EXTENTS"), None);
assert_eq!(Incompat::from_name("no_such_feature"), None);
}
#[test]
fn with_feature_toggles_by_on_disk_name() {
let fs = FeatureSet::DEFAULT;
let off = fs.with_feature("extent", false).expect("a known name");
assert!(!off.has_extents());
assert!(!off.names().contains(&"extent"));
assert_eq!(off.compat, fs.compat);
assert_eq!(off.ro_compat, fs.ro_compat);
assert_eq!(
off.with_feature("extent", true).expect("a known name"),
fs,
"turning a feature off and back on is the identity"
);
let orphaned = fs.with_feature("has_journal", false).expect("a known name");
assert_eq!(
orphaned.validate(),
Err(FeatureError::OrphanFileWithoutJournal)
);
assert_eq!(fs.with_feature("no_such_feature", true), None);
}
#[test]
fn an_unknown_bit_contributes_no_name() {
let mystery = Incompat::from_bits(Incompat::EXTENTS.bits() | 0x8000_0000);
assert_eq!(mystery.names(), ["extent"]);
assert_eq!(mystery.unknown_bits(), 0x8000_0000);
}
#[test]
fn without_clears_only_the_named_flags() {
let f = Incompat::FILETYPE | Incompat::EXTENTS | Incompat::FLEX_BG;
let cleared = f.without(Incompat::EXTENTS);
assert!(!cleared.contains(Incompat::EXTENTS));
assert!(cleared.contains(Incompat::FILETYPE));
assert!(cleared.contains(Incompat::FLEX_BG));
assert_eq!(cleared.without(Incompat::EXTENTS), cleared);
}
}