use crate::bytes::{get_arr, get_u32, get_u64, put_arr, put_u32, put_u64};
use super::{ObjectHeader, ParseError};
pub const CONTAINER_MAGIC: [u8; 4] = *b"NXSB";
pub const MAX_VOLUMES: usize = 100;
pub const COUNTERS: usize = 32;
pub const MIN_BLOCK_SIZE: u32 = 4096;
pub const MAX_BLOCK_SIZE: u32 = 65536;
pub const CHECKPOINT_AREA_IS_TREE: u32 = 0x8000_0000;
pub mod incompat {
pub const VERSION1: u64 = 0x1;
pub const VERSION2: u64 = 0x2;
pub const FUSION: u64 = 0x100;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct BlockRange {
pub start: u64,
pub count: u64,
}
impl BlockRange {
fn read(buf: &[u8], off: usize) -> Self {
Self {
start: get_u64(buf, off),
count: get_u64(buf, off + 8),
}
}
fn write(&self, buf: &mut [u8], off: usize) {
put_u64(buf, off, self.start);
put_u64(buf, off + 8, self.count);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct CheckpointAreas {
pub desc_blocks: u32,
pub data_blocks: u32,
pub desc_base: u64,
pub data_base: u64,
pub desc_next: u32,
pub data_next: u32,
pub desc_index: u32,
pub desc_len: u32,
pub data_index: u32,
pub data_len: u32,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ContainerSuperblock {
pub header: ObjectHeader,
pub block_size: u32,
pub block_count: u64,
pub features: u64,
pub readonly_compatible_features: u64,
pub incompatible_features: u64,
pub uuid: [u8; 16],
pub next_oid: u64,
pub next_xid: u64,
pub checkpoint: CheckpointAreas,
pub space_manager_oid: u64,
pub object_map_oid: u64,
pub reaper_oid: u64,
pub test_type: u32,
pub max_volumes: u32,
pub volume_oids: [u64; MAX_VOLUMES],
pub counters: [u64; COUNTERS],
pub blocked_out: BlockRange,
pub evict_mapping_tree_oid: u64,
pub flags: u64,
pub efi_jumpstart: u64,
pub fusion_uuid: [u8; 16],
pub keylocker: BlockRange,
pub ephemeral_info: [u64; 4],
pub test_oid: u64,
pub fusion_middle_tree_oid: u64,
pub fusion_cache_oid: u64,
pub fusion_cache: BlockRange,
pub newest_mounted_version: u64,
pub media_keybag: BlockRange,
}
impl ContainerSuperblock {
pub const SIZE: usize = 1408;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("container superblock", Self::SIZE, buf)?;
let magic: [u8; 4] = get_arr(buf, 32);
if magic != CONTAINER_MAGIC {
return Err(ParseError::BadMagic {
structure: "container superblock",
found: magic,
});
}
let mut volume_oids = [0u64; MAX_VOLUMES];
for (i, slot) in volume_oids.iter_mut().enumerate() {
*slot = get_u64(buf, 184 + 8 * i);
}
let mut counters = [0u64; COUNTERS];
for (i, slot) in counters.iter_mut().enumerate() {
*slot = get_u64(buf, 984 + 8 * i);
}
let mut ephemeral_info = [0u64; 4];
for (i, slot) in ephemeral_info.iter_mut().enumerate() {
*slot = get_u64(buf, 1312 + 8 * i);
}
Ok(Self {
header: ObjectHeader::read_from(buf)?,
block_size: get_u32(buf, 36),
block_count: get_u64(buf, 40),
features: get_u64(buf, 48),
readonly_compatible_features: get_u64(buf, 56),
incompatible_features: get_u64(buf, 64),
uuid: get_arr(buf, 72),
next_oid: get_u64(buf, 88),
next_xid: get_u64(buf, 96),
checkpoint: CheckpointAreas {
desc_blocks: get_u32(buf, 104),
data_blocks: get_u32(buf, 108),
desc_base: get_u64(buf, 112),
data_base: get_u64(buf, 120),
desc_next: get_u32(buf, 128),
data_next: get_u32(buf, 132),
desc_index: get_u32(buf, 136),
desc_len: get_u32(buf, 140),
data_index: get_u32(buf, 144),
data_len: get_u32(buf, 148),
},
space_manager_oid: get_u64(buf, 152),
object_map_oid: get_u64(buf, 160),
reaper_oid: get_u64(buf, 168),
test_type: get_u32(buf, 176),
max_volumes: get_u32(buf, 180),
volume_oids,
counters,
blocked_out: BlockRange::read(buf, 1240),
evict_mapping_tree_oid: get_u64(buf, 1256),
flags: get_u64(buf, 1264),
efi_jumpstart: get_u64(buf, 1272),
fusion_uuid: get_arr(buf, 1280),
keylocker: BlockRange::read(buf, 1296),
ephemeral_info,
test_oid: get_u64(buf, 1344),
fusion_middle_tree_oid: get_u64(buf, 1352),
fusion_cache_oid: get_u64(buf, 1360),
fusion_cache: BlockRange::read(buf, 1368),
newest_mounted_version: get_u64(buf, 1384),
media_keybag: BlockRange::read(buf, 1392),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a container superblock needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
self.header.write_to(buf);
put_arr(buf, 32, &CONTAINER_MAGIC);
put_u32(buf, 36, self.block_size);
put_u64(buf, 40, self.block_count);
put_u64(buf, 48, self.features);
put_u64(buf, 56, self.readonly_compatible_features);
put_u64(buf, 64, self.incompatible_features);
put_arr(buf, 72, &self.uuid);
put_u64(buf, 88, self.next_oid);
put_u64(buf, 96, self.next_xid);
let c = &self.checkpoint;
put_u32(buf, 104, c.desc_blocks);
put_u32(buf, 108, c.data_blocks);
put_u64(buf, 112, c.desc_base);
put_u64(buf, 120, c.data_base);
put_u32(buf, 128, c.desc_next);
put_u32(buf, 132, c.data_next);
put_u32(buf, 136, c.desc_index);
put_u32(buf, 140, c.desc_len);
put_u32(buf, 144, c.data_index);
put_u32(buf, 148, c.data_len);
put_u64(buf, 152, self.space_manager_oid);
put_u64(buf, 160, self.object_map_oid);
put_u64(buf, 168, self.reaper_oid);
put_u32(buf, 176, self.test_type);
put_u32(buf, 180, self.max_volumes);
for (i, oid) in self.volume_oids.iter().enumerate() {
put_u64(buf, 184 + 8 * i, *oid);
}
for (i, counter) in self.counters.iter().enumerate() {
put_u64(buf, 984 + 8 * i, *counter);
}
self.blocked_out.write(buf, 1240);
put_u64(buf, 1256, self.evict_mapping_tree_oid);
put_u64(buf, 1264, self.flags);
put_u64(buf, 1272, self.efi_jumpstart);
put_arr(buf, 1280, &self.fusion_uuid);
self.keylocker.write(buf, 1296);
for (i, info) in self.ephemeral_info.iter().enumerate() {
put_u64(buf, 1312 + 8 * i, *info);
}
put_u64(buf, 1344, self.test_oid);
put_u64(buf, 1352, self.fusion_middle_tree_oid);
put_u64(buf, 1360, self.fusion_cache_oid);
self.fusion_cache.write(buf, 1368);
put_u64(buf, 1384, self.newest_mounted_version);
self.media_keybag.write(buf, 1392);
}
#[must_use]
pub const fn desc_area_blocks(&self) -> u32 {
self.checkpoint.desc_blocks & !CHECKPOINT_AREA_IS_TREE
}
pub fn volumes(&self) -> impl Iterator<Item = (usize, u64)> + '_ {
self.volume_oids
.iter()
.enumerate()
.filter(|(_, oid)| **oid != 0)
.map(|(slot, oid)| (slot, *oid))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> ContainerSuperblock {
let mut volume_oids = [0u64; MAX_VOLUMES];
volume_oids[0] = 1026;
volume_oids[3] = 1100;
let mut counters = [0u64; COUNTERS];
counters[0] = 21;
ContainerSuperblock {
header: ObjectHeader {
checksum: 0xAABB,
oid: 1,
xid: 9,
type_word: 0x8000_0001,
subtype: 0,
},
block_size: 4096,
block_count: 16384,
features: 0,
readonly_compatible_features: 0,
incompatible_features: incompat::VERSION2,
uuid: [0x5A; 16],
next_oid: 1100,
next_xid: 10,
checkpoint: CheckpointAreas {
desc_blocks: 8,
data_blocks: 52,
desc_base: 1,
data_base: 9,
desc_next: 4,
data_next: 12,
desc_index: 2,
desc_len: 2,
data_index: 8,
data_len: 4,
},
space_manager_oid: 1024,
object_map_oid: 77,
reaper_oid: 1025,
test_type: 0,
max_volumes: 1,
volume_oids,
counters,
blocked_out: BlockRange::default(),
evict_mapping_tree_oid: 0,
flags: 0,
efi_jumpstart: 0,
fusion_uuid: [0; 16],
keylocker: BlockRange { start: 3, count: 4 },
ephemeral_info: [5, 0, 0, 0],
test_oid: 0,
fusion_middle_tree_oid: 0,
fusion_cache_oid: 0,
fusion_cache: BlockRange::default(),
newest_mounted_version: 0x0009_0000_0000_0001,
media_keybag: BlockRange { start: 6, count: 1 },
}
}
#[test]
fn a_superblock_round_trips() {
let sb = sample();
let mut buf = vec![0u8; 4096];
sb.write_to(&mut buf);
assert_eq!(&buf[32..36], b"NXSB");
assert_eq!(ContainerSuperblock::read_from(&buf), Ok(sb));
}
#[test]
fn fields_sit_at_the_offsets_the_reference_gives() {
let sb = sample();
let mut buf = vec![0u8; 4096];
sb.write_to(&mut buf);
assert_eq!(get_u64(&buf, 160), 77, "the object map at 160");
assert_eq!(get_u32(&buf, 180), 1, "the volume count at 180");
assert_eq!(get_u64(&buf, 184), 1026, "the first volume slot at 184");
assert_eq!(get_u64(&buf, 984), 21, "the counters at 984");
assert_eq!(get_u64(&buf, 1384), 0x0009_0000_0000_0001);
}
#[test]
fn another_signature_is_not_a_container() {
let mut buf = vec![0u8; 4096];
sample().write_to(&mut buf);
buf[32] = b'X';
assert!(matches!(
ContainerSuperblock::read_from(&buf),
Err(ParseError::BadMagic { found, .. }) if found == *b"XXSB"
));
}
#[test]
fn only_named_volume_slots_are_volumes() {
let sb = sample();
assert_eq!(sb.volumes().collect::<Vec<_>>(), [(0, 1026), (3, 1100)]);
}
#[test]
fn the_tree_bit_is_not_part_of_the_count() {
let mut sb = sample();
sb.checkpoint.desc_blocks = CHECKPOINT_AREA_IS_TREE | 8;
assert_eq!(sb.desc_area_blocks(), 8);
}
}