use super::{ParseError, split64};
use super::{
get_arr, get_u8, get_u16, get_u32, get_u64, join64, put_arr, put_u8, put_u16, put_u32, put_u64,
};
pub const SUPERBLOCK_MAGIC: u16 = 0xef53;
pub const STATE_CLEAN: u16 = 0x0001;
pub const STATE_ERRORS: u16 = 0x0002;
#[must_use]
pub fn unpadded(field: &[u8]) -> &[u8] {
let end = field.iter().position(|&b| b == 0).unwrap_or(field.len());
&field[..end]
}
#[must_use]
pub fn superblock_checksum(bytes: &[u8]) -> u32 {
assert!(
bytes.len() >= SuperBlock::SIZE,
"a whole superblock record is needed"
);
crate::crc32c::crc32c(!0, &bytes[..SuperBlock::CHECKSUM_OFFSET])
}
const INCOMPAT_64BIT: u32 = 0x0080;
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct SuperBlock {
pub inodes_count: u32,
pub blocks_count: u64,
pub r_blocks_count: u64,
pub free_blocks_count: u64,
pub free_inodes_count: u32,
pub first_data_block: u32,
pub log_block_size: u32,
pub log_cluster_size: u32,
pub blocks_per_group: u32,
pub clusters_per_group: u32,
pub inodes_per_group: u32,
pub mtime: u32,
pub wtime: u32,
pub mnt_count: u16,
pub max_mnt_count: u16,
pub magic: u16,
pub state: u16,
pub errors: u16,
pub minor_rev_level: u16,
pub lastcheck: u32,
pub checkinterval: u32,
pub creator_os: u32,
pub rev_level: u32,
pub def_resuid: u16,
pub def_resgid: u16,
pub first_ino: u32,
pub inode_size: u16,
pub block_group_nr: u16,
pub feature_compat: u32,
pub feature_incompat: u32,
pub feature_ro_compat: u32,
pub uuid: [u8; 16],
pub volume_name: [u8; 16],
pub last_mounted: [u8; 64],
pub reserved_gdt_blocks: u16,
pub journal_inum: u32,
pub jnl_backup_type: u8,
pub jnl_blocks: [u32; 17],
pub hash_seed: [u8; 16],
pub def_hash_version: u8,
pub desc_size: u16,
pub default_mount_opts: u32,
pub first_meta_bg: u32,
pub mkfs_time: u32,
pub min_extra_isize: u16,
pub want_extra_isize: u16,
pub flags: u32,
pub mmp_update_interval: u16,
pub mmp_block: u64,
pub log_groups_per_flex: u8,
pub checksum_type: u8,
pub kbytes_written: u64,
pub overhead_clusters: u32,
pub backup_bgs: [u32; 2],
pub checksum_seed: u32,
pub orphan_file_inum: u32,
pub checksum: u32,
}
impl SuperBlock {
pub const SIZE: usize = 1024;
pub const MAGIC_OFFSET: usize = 0x38;
pub const FEATURE_INCOMPAT_OFFSET: usize = 0x60;
pub const UUID_OFFSET: usize = 0x68;
pub const VOLUME_NAME_OFFSET: usize = 0x78;
pub const CHECKSUM_SEED_OFFSET: usize = 0x270;
pub const CHECKSUM_OFFSET: usize = Self::SIZE - 4;
#[must_use]
pub fn new() -> Self {
Self {
inodes_count: 0,
blocks_count: 0,
r_blocks_count: 0,
free_blocks_count: 0,
free_inodes_count: 0,
first_data_block: 0,
log_block_size: 0,
log_cluster_size: 0,
blocks_per_group: 0,
clusters_per_group: 0,
inodes_per_group: 0,
mtime: 0,
wtime: 0,
mnt_count: 0,
max_mnt_count: 0,
magic: SUPERBLOCK_MAGIC,
state: 0,
errors: 0,
minor_rev_level: 0,
lastcheck: 0,
checkinterval: 0,
creator_os: 0,
rev_level: 0,
def_resuid: 0,
def_resgid: 0,
first_ino: 0,
inode_size: 0,
block_group_nr: 0,
feature_compat: 0,
feature_incompat: 0,
feature_ro_compat: 0,
uuid: [0; 16],
volume_name: [0; 16],
last_mounted: [0; 64],
reserved_gdt_blocks: 0,
journal_inum: 0,
jnl_backup_type: 0,
jnl_blocks: [0; 17],
hash_seed: [0; 16],
def_hash_version: 0,
desc_size: 0,
default_mount_opts: 0,
first_meta_bg: 0,
mkfs_time: 0,
min_extra_isize: 0,
want_extra_isize: 0,
flags: 0,
mmp_update_interval: 0,
mmp_block: 0,
log_groups_per_flex: 0,
checksum_type: 0,
kbytes_written: 0,
overhead_clusters: 0,
backup_bgs: [0; 2],
checksum_seed: 0,
orphan_file_inum: 0,
checksum: 0,
}
}
#[must_use]
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut b = [0u8; Self::SIZE];
let (blocks_lo, blocks_hi) = split64(self.blocks_count);
let (r_blocks_lo, r_blocks_hi) = split64(self.r_blocks_count);
let (free_lo, free_hi) = split64(self.free_blocks_count);
put_u32(&mut b, 0x00, self.inodes_count);
put_u32(&mut b, 0x04, blocks_lo);
put_u32(&mut b, 0x08, r_blocks_lo);
put_u32(&mut b, 0x0c, free_lo);
put_u32(&mut b, 0x10, self.free_inodes_count);
put_u32(&mut b, 0x14, self.first_data_block);
put_u32(&mut b, 0x18, self.log_block_size);
put_u32(&mut b, 0x1c, self.log_cluster_size);
put_u32(&mut b, 0x20, self.blocks_per_group);
put_u32(&mut b, 0x24, self.clusters_per_group);
put_u32(&mut b, 0x28, self.inodes_per_group);
put_u32(&mut b, 0x2c, self.mtime);
put_u32(&mut b, 0x30, self.wtime);
put_u16(&mut b, 0x34, self.mnt_count);
put_u16(&mut b, 0x36, self.max_mnt_count);
put_u16(&mut b, Self::MAGIC_OFFSET, self.magic);
put_u16(&mut b, 0x3a, self.state);
put_u16(&mut b, 0x3c, self.errors);
put_u16(&mut b, 0x3e, self.minor_rev_level);
put_u32(&mut b, 0x40, self.lastcheck);
put_u32(&mut b, 0x44, self.checkinterval);
put_u32(&mut b, 0x48, self.creator_os);
put_u32(&mut b, 0x4c, self.rev_level);
put_u16(&mut b, 0x50, self.def_resuid);
put_u16(&mut b, 0x52, self.def_resgid);
put_u32(&mut b, 0x54, self.first_ino);
put_u16(&mut b, 0x58, self.inode_size);
put_u16(&mut b, 0x5a, self.block_group_nr);
put_u32(&mut b, 0x5c, self.feature_compat);
put_u32(&mut b, Self::FEATURE_INCOMPAT_OFFSET, self.feature_incompat);
put_u32(&mut b, 0x64, self.feature_ro_compat);
put_arr(&mut b, Self::UUID_OFFSET, &self.uuid);
put_arr(&mut b, Self::VOLUME_NAME_OFFSET, &self.volume_name);
put_arr(&mut b, 0x88, &self.last_mounted);
put_u16(&mut b, 0xce, self.reserved_gdt_blocks);
put_u32(&mut b, 0xe0, self.journal_inum);
put_arr(&mut b, 0xec, &self.hash_seed);
put_u8(&mut b, 0xfc, self.def_hash_version);
put_u8(&mut b, 0xfd, self.jnl_backup_type);
put_u16(&mut b, 0xfe, self.desc_size);
put_u32(&mut b, 0x100, self.default_mount_opts);
put_u32(&mut b, 0x104, self.first_meta_bg);
put_u32(&mut b, 0x108, self.mkfs_time);
for (i, &w) in self.jnl_blocks.iter().enumerate() {
put_u32(&mut b, 0x10c + i * 4, w);
}
put_u32(&mut b, 0x150, blocks_hi);
put_u32(&mut b, 0x154, r_blocks_hi);
put_u32(&mut b, 0x158, free_hi);
put_u16(&mut b, 0x15c, self.min_extra_isize);
put_u16(&mut b, 0x15e, self.want_extra_isize);
put_u32(&mut b, 0x160, self.flags);
put_u16(&mut b, 0x166, self.mmp_update_interval);
put_u64(&mut b, 0x168, self.mmp_block);
put_u8(&mut b, 0x174, self.log_groups_per_flex);
put_u8(&mut b, 0x175, self.checksum_type);
let (kb_lo, kb_hi) = split64(self.kbytes_written);
put_u32(&mut b, 0x178, kb_lo);
put_u32(&mut b, 0x17c, kb_hi);
put_u32(&mut b, 0x248, self.overhead_clusters);
put_u32(&mut b, 0x24c, self.backup_bgs[0]);
put_u32(&mut b, 0x250, self.backup_bgs[1]);
put_u32(&mut b, Self::CHECKSUM_SEED_OFFSET, self.checksum_seed);
put_u32(&mut b, 0x280, self.orphan_file_inum);
put_u32(&mut b, Self::CHECKSUM_OFFSET, self.checksum);
b
}
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "SuperBlock",
need: Self::SIZE,
got: buf.len(),
});
}
let magic = get_u16(buf, Self::MAGIC_OFFSET);
if magic != SUPERBLOCK_MAGIC {
return Err(ParseError::BadMagic {
structure: "SuperBlock",
found: u32::from(magic),
expected: u32::from(SUPERBLOCK_MAGIC),
});
}
let hi = |off| {
if get_u32(buf, Self::FEATURE_INCOMPAT_OFFSET) & INCOMPAT_64BIT != 0 {
get_u32(buf, off)
} else {
0
}
};
Ok(Self {
inodes_count: get_u32(buf, 0x00),
blocks_count: join64(get_u32(buf, 0x04), hi(0x150)),
r_blocks_count: join64(get_u32(buf, 0x08), hi(0x154)),
free_blocks_count: join64(get_u32(buf, 0x0c), hi(0x158)),
free_inodes_count: get_u32(buf, 0x10),
first_data_block: get_u32(buf, 0x14),
log_block_size: get_u32(buf, 0x18),
log_cluster_size: get_u32(buf, 0x1c),
blocks_per_group: get_u32(buf, 0x20),
clusters_per_group: get_u32(buf, 0x24),
inodes_per_group: get_u32(buf, 0x28),
mtime: get_u32(buf, 0x2c),
wtime: get_u32(buf, 0x30),
mnt_count: get_u16(buf, 0x34),
max_mnt_count: get_u16(buf, 0x36),
magic,
state: get_u16(buf, 0x3a),
errors: get_u16(buf, 0x3c),
minor_rev_level: get_u16(buf, 0x3e),
lastcheck: get_u32(buf, 0x40),
checkinterval: get_u32(buf, 0x44),
creator_os: get_u32(buf, 0x48),
rev_level: get_u32(buf, 0x4c),
def_resuid: get_u16(buf, 0x50),
def_resgid: get_u16(buf, 0x52),
first_ino: get_u32(buf, 0x54),
inode_size: get_u16(buf, 0x58),
block_group_nr: get_u16(buf, 0x5a),
feature_compat: get_u32(buf, 0x5c),
feature_incompat: get_u32(buf, Self::FEATURE_INCOMPAT_OFFSET),
feature_ro_compat: get_u32(buf, 0x64),
uuid: get_arr(buf, Self::UUID_OFFSET),
volume_name: get_arr(buf, Self::VOLUME_NAME_OFFSET),
last_mounted: get_arr(buf, 0x88),
reserved_gdt_blocks: get_u16(buf, 0xce),
journal_inum: get_u32(buf, 0xe0),
jnl_backup_type: get_u8(buf, 0xfd),
jnl_blocks: {
let mut w = [0u32; 17];
for (i, slot) in w.iter_mut().enumerate() {
*slot = get_u32(buf, 0x10c + i * 4);
}
w
},
hash_seed: get_arr(buf, 0xec),
def_hash_version: get_u8(buf, 0xfc),
desc_size: get_u16(buf, 0xfe),
default_mount_opts: get_u32(buf, 0x100),
first_meta_bg: get_u32(buf, 0x104),
mkfs_time: get_u32(buf, 0x108),
min_extra_isize: get_u16(buf, 0x15c),
want_extra_isize: get_u16(buf, 0x15e),
flags: get_u32(buf, 0x160),
mmp_update_interval: get_u16(buf, 0x166),
mmp_block: get_u64(buf, 0x168),
log_groups_per_flex: get_u8(buf, 0x174),
checksum_type: get_u8(buf, 0x175),
kbytes_written: join64(get_u32(buf, 0x178), get_u32(buf, 0x17c)),
overhead_clusters: get_u32(buf, 0x248),
backup_bgs: [get_u32(buf, 0x24c), get_u32(buf, 0x250)],
checksum_seed: get_u32(buf, Self::CHECKSUM_SEED_OFFSET),
orphan_file_inum: get_u32(buf, 0x280),
checksum: get_u32(buf, Self::CHECKSUM_OFFSET),
})
}
}
impl Default for SuperBlock {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn baseline() -> SuperBlock {
let mut s = SuperBlock::new();
s.inodes_count = 16384;
s.blocks_count = 16384;
s.r_blocks_count = 819;
s.free_blocks_count = 15347;
s.free_inodes_count = 16373;
s.first_data_block = 0;
s.log_block_size = 2;
s.log_cluster_size = 2;
s.blocks_per_group = 32768;
s.clusters_per_group = 32768;
s.inodes_per_group = 16384;
s.wtime = 1_700_000_000;
s.max_mnt_count = 0xffff;
s.state = STATE_CLEAN;
s.errors = 1;
s.lastcheck = 1_700_000_000;
s.rev_level = 1;
s.first_ino = 11;
s.inode_size = 256;
s.feature_compat = 0x18;
s.feature_incompat = 0x2c2;
s.feature_ro_compat = 0x46b;
s.checksum_type = 1;
s.uuid = [
0xf0, 0xe1, 0x70, 0x55, 0, 0, 0x40, 0, 0x80, 0, 0, 0, 0, 0, 0, 0,
];
s.reserved_gdt_blocks = 3;
s.def_hash_version = 1;
s.desc_size = 64;
s.default_mount_opts = 0x0c;
s.mkfs_time = 1_700_000_000;
s.min_extra_isize = 32;
s.want_extra_isize = 32;
s.flags = 1;
s.log_groups_per_flex = 4;
s
}
#[test]
fn round_trips() {
let s = baseline();
assert_eq!(SuperBlock::read_from(&s.to_bytes()).unwrap(), s);
}
#[test]
fn matches_ground_truth_offsets() {
let b = baseline().to_bytes();
assert_eq!(get_u16(&b, 0x38), 0xef53, "magic");
assert_eq!(get_u32(&b, 0x18), 2, "log_block_size");
assert_eq!(get_u32(&b, 0x20), 32768, "blocks_per_group");
assert_eq!(get_u32(&b, 0x54), 11, "first_ino");
assert_eq!(get_u16(&b, 0x58), 256, "inode_size");
assert_eq!(get_u32(&b, 0x5c), 0x18, "feature_compat");
assert_eq!(get_u32(&b, 0x60), 0x2c2, "feature_incompat");
assert_eq!(get_u32(&b, 0x64), 0x46b, "feature_ro_compat");
assert_eq!(get_u8(&b, 0x175), 1, "checksum_type: crc32c");
assert_eq!(get_u16(&b, 0xce), 3, "reserved_gdt_blocks");
assert_eq!(get_u16(&b, 0xfe), 64, "desc_size");
assert_eq!(get_u32(&b, 0x100), 0x0c, "default_mount_opts");
assert_eq!(get_u16(&b, 0x15c), 32, "min_extra_isize");
assert_eq!(get_u32(&b, 0x160), 1, "flags: signed hash");
assert_eq!(get_u8(&b, 0x174), 4, "log_groups_per_flex");
assert_eq!(&b[0x68..0x6c], &[0xf0, 0xe1, 0x70, 0x55], "uuid prefix");
}
#[test]
fn the_block_counts_take_their_high_words_only_under_64bit() {
let mut b = baseline().to_bytes(); put_u32(&mut b, 0x150, 1);
put_u32(&mut b, 0x154, 2);
put_u32(&mut b, 0x158, 3);
let wide = SuperBlock::read_from(&b).unwrap();
assert_eq!(wide.blocks_count, 16384 + (1 << 32));
assert_eq!(wide.r_blocks_count, 819 + (2 << 32));
assert_eq!(wide.free_blocks_count, 15347 + (3 << 32));
put_u32(&mut b, 0x60, 0x2c2 & !INCOMPAT_64BIT);
let narrow = SuperBlock::read_from(&b).unwrap();
assert_eq!(narrow.blocks_count, 16384);
assert_eq!(narrow.r_blocks_count, 819);
assert_eq!(narrow.free_blocks_count, 15347);
}
#[test]
fn rejects_bad_magic() {
let mut b = baseline().to_bytes();
b[0x38] = 0;
b[0x39] = 0;
assert!(matches!(
SuperBlock::read_from(&b),
Err(ParseError::BadMagic { .. })
));
}
#[test]
fn rejects_short_buffer() {
assert!(matches!(
SuperBlock::read_from(&[0u8; 512]),
Err(ParseError::TooShort { .. })
));
}
}