use crate::bytes::{get_u32_be, put_u32_be};
pub const JBD2_MAGIC: u32 = 0xc03b_3998;
pub const JBD2_SUPERBLOCK_V2: u32 = 4;
pub const MIN_JOURNAL_BLOCKS: u32 = 1024;
pub(crate) const SUPERBLOCK_SIZE: usize = 1024;
pub(crate) mod offset {
pub const BLOCK_SIZE: usize = 0x0c;
pub const MAX_LEN: usize = 0x10;
pub const FIRST: usize = 0x14;
pub const SEQUENCE: usize = 0x18;
pub const START: usize = 0x1c;
pub const FEATURE_COMPAT: usize = 0x24;
pub const FEATURE_INCOMPAT: usize = 0x28;
pub const FEATURE_RO_COMPAT: usize = 0x2c;
pub const UUID: usize = 0x30;
pub const CHECKSUM_TYPE: usize = 0x50;
pub const CHECKSUM: usize = 0xfc;
}
pub(crate) const INCOMPAT_CSUM_V2: u32 = 0x0000_0008;
pub(crate) const INCOMPAT_CSUM_V3: u32 = 0x0000_0010;
pub(crate) const CRC32C_CHKSUM: u8 = 4;
pub(crate) const JBD2_SUPERBLOCK_V1: u32 = 3;
pub(crate) const BLOCKTYPE_DESCRIPTOR: u32 = 1;
pub(crate) const BLOCKTYPE_COMMIT: u32 = 2;
pub(crate) const BLOCKTYPE_REVOKE: u32 = 5;
pub(crate) const COMPAT_CHECKSUM: u32 = 0x0000_0001;
pub(crate) const INCOMPAT_REVOKE: u32 = 0x0000_0001;
pub(crate) const INCOMPAT_64BIT: u32 = 0x0000_0002;
pub(crate) const INCOMPAT_ASYNC_COMMIT: u32 = 0x0000_0004;
pub(crate) const INCOMPAT_FAST_COMMIT: u32 = 0x0000_0020;
pub(crate) const TAG_ESCAPED: u32 = 0x1;
pub(crate) const TAG_SAME_UUID: u32 = 0x2;
pub(crate) const TAG_DELETED: u32 = 0x4;
pub(crate) const TAG_LAST: u32 = 0x8;
pub(crate) fn superblock_is_checksummed(record: &[u8]) -> bool {
assert!(record.len() >= SUPERBLOCK_SIZE, "a whole record is needed");
get_u32_be(record, offset::FEATURE_INCOMPAT) & (INCOMPAT_CSUM_V2 | INCOMPAT_CSUM_V3) != 0
}
pub(crate) fn superblock_checksum(record: &[u8]) -> u32 {
use crate::crc32c::crc32c;
assert!(record.len() >= SUPERBLOCK_SIZE, "a whole record is needed");
let c = crc32c(!0, &record[..offset::CHECKSUM]);
let c = crc32c(c, &[0u8; 4]);
crc32c(c, &record[offset::CHECKSUM + 4..SUPERBLOCK_SIZE])
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum JournalSize {
#[default]
Auto,
Blocks(u32),
}
#[must_use]
pub fn default_journal_blocks(num_blocks: u64) -> Option<u32> {
const STEPS: [(u64, u32); 8] = [
(2048, 1024),
(32_768, 4096),
(262_144, 8192),
(524_288, 16_384),
(4_194_304, 32_768),
(8_388_608, 65_536),
(16_777_216, 131_072),
(33_554_432, 262_144),
];
STEPS
.iter()
.rev()
.find(|&&(from, _)| num_blocks >= from)
.map(|&(_, blocks)| blocks)
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct JournalParams {
pub block_size: u32,
pub journal_blocks: u32,
pub uuid: [u8; 16],
}
impl JournalParams {
#[must_use]
pub const fn new(block_size: u32, journal_blocks: u32, uuid: [u8; 16]) -> Self {
Self {
block_size,
journal_blocks,
uuid,
}
}
}
#[must_use]
pub fn build_superblock(params: &JournalParams) -> Vec<u8> {
let &JournalParams {
block_size,
journal_blocks,
uuid,
} = params;
let mut b = vec![0u8; block_size as usize];
put_u32_be(&mut b, 0x00, JBD2_MAGIC);
put_u32_be(&mut b, 0x04, JBD2_SUPERBLOCK_V2);
put_u32_be(&mut b, 0x0c, block_size);
put_u32_be(&mut b, 0x10, journal_blocks);
put_u32_be(&mut b, 0x14, 1); put_u32_be(&mut b, 0x18, 1); b[0x30..0x40].copy_from_slice(&uuid); put_u32_be(&mut b, 0x40, 1); b
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct JournalSuperblock {
pub block_size: u32,
pub max_len: u32,
pub first: u32,
pub sequence: u32,
pub start: u32,
pub nr_users: u32,
pub uuid: [u8; 16],
}
impl JournalSuperblock {
pub fn read_from(buf: &[u8]) -> Result<Self, crate::ondisk::ParseError> {
use crate::ondisk::ParseError;
if buf.len() < 0x44 {
return Err(ParseError::TooShort {
structure: "JournalSuperblock",
need: 0x44,
got: buf.len(),
});
}
let magic = get_u32_be(buf, 0x00);
if magic != JBD2_MAGIC {
return Err(ParseError::BadMagic {
structure: "JournalSuperblock",
found: magic,
expected: JBD2_MAGIC,
});
}
let blocktype = get_u32_be(buf, 0x04);
if blocktype != JBD2_SUPERBLOCK_V2 {
return Err(ParseError::BadMagic {
structure: "JournalSuperblock blocktype",
found: blocktype,
expected: JBD2_SUPERBLOCK_V2,
});
}
let mut uuid = [0u8; 16];
uuid.copy_from_slice(&buf[0x30..0x40]);
Ok(Self {
block_size: get_u32_be(buf, 0x0c),
max_len: get_u32_be(buf, 0x10),
first: get_u32_be(buf, 0x14),
sequence: get_u32_be(buf, 0x18),
start: get_u32_be(buf, 0x1c),
nr_users: get_u32_be(buf, 0x40),
uuid,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_heuristic_matches_mke2fs_across_the_range() {
assert_eq!(default_journal_blocks(2047), None);
assert_eq!(default_journal_blocks(2048), Some(1024));
assert_eq!(default_journal_blocks(16_384), Some(1024)); assert_eq!(default_journal_blocks(32_767), Some(1024));
assert_eq!(default_journal_blocks(32_768), Some(4096));
assert_eq!(default_journal_blocks(131_072), Some(4096)); assert_eq!(default_journal_blocks(261_888), Some(4096));
assert_eq!(default_journal_blocks(262_144), Some(8192));
assert_eq!(default_journal_blocks(393_216), Some(8192));
assert_eq!(default_journal_blocks(524_288), Some(16_384));
assert_eq!(default_journal_blocks(1_048_576), Some(16_384));
assert_eq!(default_journal_blocks(1_572_864), Some(16_384));
assert_eq!(default_journal_blocks(2_097_152), Some(16_384));
assert_eq!(default_journal_blocks(4_194_303), Some(16_384));
assert_eq!(default_journal_blocks(4_194_304), Some(32_768));
assert_eq!(default_journal_blocks(6_291_456), Some(32_768)); assert_eq!(default_journal_blocks(8_388_608), Some(65_536));
assert_eq!(default_journal_blocks(12_582_912), Some(65_536)); assert_eq!(default_journal_blocks(16_777_216), Some(131_072));
assert_eq!(default_journal_blocks(25_165_824), Some(131_072)); assert_eq!(default_journal_blocks(33_554_431), Some(131_072));
assert_eq!(default_journal_blocks(33_554_432), Some(262_144));
assert_eq!(default_journal_blocks(67_108_864), Some(262_144));
assert_eq!(default_journal_blocks(1 << 30), Some(262_144)); }
#[test]
fn superblock_pins_the_mke2fs_byte_layout() {
let uuid = [0x11u8; 16];
let sb = build_superblock(&JournalParams::new(4096, 1024, uuid));
assert_eq!(sb.len(), 4096);
assert_eq!(get_u32_be(&sb, 0x00), 0xc03b_3998);
assert_eq!(get_u32_be(&sb, 0x04), 4);
assert_eq!(get_u32_be(&sb, 0x08), 0);
assert_eq!(get_u32_be(&sb, 0x0c), 4096);
assert_eq!(get_u32_be(&sb, 0x10), 1024);
assert_eq!(get_u32_be(&sb, 0x14), 1);
assert_eq!(get_u32_be(&sb, 0x18), 1);
assert_eq!(get_u32_be(&sb, 0x1c), 0);
assert_eq!(&sb[0x30..0x40], &uuid);
assert_eq!(get_u32_be(&sb, 0x40), 1);
assert!(sb[0x50..].iter().all(|&x| x == 0));
}
#[test]
fn superblock_round_trips_through_the_reader() {
let uuid = [0xabu8; 16];
let sb = build_superblock(&JournalParams::new(4096, 8192, uuid));
let parsed = JournalSuperblock::read_from(&sb).unwrap();
assert_eq!(
parsed,
JournalSuperblock {
block_size: 4096,
max_len: 8192,
first: 1,
sequence: 1,
start: 0,
nr_users: 1,
uuid,
}
);
}
#[test]
fn a_log_declares_its_superblock_checksum_through_the_incompat_word() {
let mut sb = build_superblock(&JournalParams::new(4096, 1024, [0; 16]));
assert!(!superblock_is_checksummed(&sb));
for bit in [0x1u32, 0x2, 0x4, 0x20] {
put_u32_be(&mut sb, offset::FEATURE_INCOMPAT, bit);
assert!(
!superblock_is_checksummed(&sb),
"{bit:#x} is not a checksum"
);
}
for bit in [INCOMPAT_CSUM_V2, INCOMPAT_CSUM_V3] {
put_u32_be(&mut sb, offset::FEATURE_INCOMPAT, bit);
assert!(superblock_is_checksummed(&sb), "{bit:#x} is a checksum");
}
}
#[test]
fn the_superblock_checksum_covers_the_record_and_not_its_own_field() {
let mut sb = build_superblock(&JournalParams::new(4096, 1024, [0x33; 16]));
let sealed = superblock_checksum(&sb);
sb[offset::CHECKSUM..offset::CHECKSUM + 4].copy_from_slice(&sealed.to_be_bytes());
assert_eq!(superblock_checksum(&sb), sealed);
sb[offset::CHECKSUM..offset::CHECKSUM + 4].copy_from_slice(&[0xff; 4]);
assert_eq!(superblock_checksum(&sb), sealed);
for moved in [0x00usize, offset::UUID, offset::CHECKSUM - 1, 0x100, 0x3ff] {
let mut other = sb.clone();
other[moved] ^= 0xff;
assert_ne!(
superblock_checksum(&other),
sealed,
"byte {moved:#x} is not covered"
);
}
let mut past = sb.clone();
past[SUPERBLOCK_SIZE] ^= 0xff;
assert_eq!(superblock_checksum(&past), sealed);
}
#[test]
fn reader_rejects_a_non_journal_block() {
let mut sb = build_superblock(&JournalParams::new(4096, 1024, [0; 16]));
sb[0] ^= 0xff;
assert!(JournalSuperblock::read_from(&sb).is_err());
}
#[test]
fn reader_refuses_a_buffer_too_short_for_nr_users() {
let sb = build_superblock(&JournalParams::new(4096, 1024, [0; 16]));
for len in 0x40..0x44 {
let err = JournalSuperblock::read_from(&sb[..len]).unwrap_err();
assert!(
matches!(
err,
crate::ondisk::ParseError::TooShort {
structure: "JournalSuperblock",
need: 0x44,
got,
} if got == len
),
"length {len} gave {err:?}"
);
}
assert!(JournalSuperblock::read_from(&sb[..0x44]).is_ok());
}
}