pub const JBD2_MAGIC: u32 = 0xc03b_3998;
pub const JBD2_SUPERBLOCK_V2: u32 = 4;
pub const MIN_JOURNAL_BLOCKS: u32 = 1024;
#[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> {
if num_blocks < 2048 {
None
} else if num_blocks < 32_768 {
Some(1024)
} else if num_blocks < 262_144 {
Some(4096)
} else if num_blocks < 524_288 {
Some(8192)
} else {
Some((num_blocks / 128).clamp(16_384, 262_144) as u32)
}
}
#[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_be_u32(&mut b, 0x00, JBD2_MAGIC);
put_be_u32(&mut b, 0x04, JBD2_SUPERBLOCK_V2);
put_be_u32(&mut b, 0x0c, block_size);
put_be_u32(&mut b, 0x10, journal_blocks);
put_be_u32(&mut b, 0x14, 1); put_be_u32(&mut b, 0x18, 1); b[0x30..0x40].copy_from_slice(&uuid); put_be_u32(&mut b, 0x40, 1); b
}
#[inline]
fn put_be_u32(buf: &mut [u8], off: usize, v: u32) {
buf[off..off + 4].copy_from_slice(&v.to_be_bytes());
}
#[inline]
fn get_be_u32(buf: &[u8], off: usize) -> u32 {
u32::from_be_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]])
}
#[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_be_u32(buf, 0x00);
if magic != JBD2_MAGIC {
return Err(ParseError::BadMagic {
structure: "JournalSuperblock",
found: magic,
expected: JBD2_MAGIC,
});
}
let blocktype = get_be_u32(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_be_u32(buf, 0x0c),
max_len: get_be_u32(buf, 0x10),
first: get_be_u32(buf, 0x14),
sequence: get_be_u32(buf, 0x18),
start: get_be_u32(buf, 0x1c),
nr_users: get_be_u32(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_304), Some(32_768));
assert_eq!(default_journal_blocks(8_388_608), Some(65_536));
assert_eq!(default_journal_blocks(33_554_432), Some(262_144));
assert_eq!(default_journal_blocks(67_108_864), 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_be_u32(&sb, 0x00), 0xc03b_3998);
assert_eq!(get_be_u32(&sb, 0x04), 4);
assert_eq!(get_be_u32(&sb, 0x08), 0);
assert_eq!(get_be_u32(&sb, 0x0c), 4096);
assert_eq!(get_be_u32(&sb, 0x10), 1024);
assert_eq!(get_be_u32(&sb, 0x14), 1);
assert_eq!(get_be_u32(&sb, 0x18), 1);
assert_eq!(get_be_u32(&sb, 0x1c), 0);
assert_eq!(&sb[0x30..0x40], &uuid);
assert_eq!(get_be_u32(&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 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());
}
}