use crate::bytes::{get_u16, get_u32, get_u64, put_u16, put_u32, put_u64};
use super::{ObjectHeader, ParseError};
pub const CHECKPOINT_MAP_LAST: u32 = 0x1;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CheckpointMapping {
pub type_word: u32,
pub subtype: u32,
pub size: u32,
pub pad: u32,
pub volume_oid: u64,
pub oid: u64,
pub address: u64,
}
impl CheckpointMapping {
pub const SIZE: usize = 40;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("checkpoint mapping", Self::SIZE, buf)?;
Ok(Self {
type_word: get_u32(buf, 0),
subtype: get_u32(buf, 4),
size: get_u32(buf, 8),
pad: get_u32(buf, 12),
volume_oid: get_u64(buf, 16),
oid: get_u64(buf, 24),
address: get_u64(buf, 32),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a checkpoint mapping needs 40 bytes"
);
put_u32(buf, 0, self.type_word);
put_u32(buf, 4, self.subtype);
put_u32(buf, 8, self.size);
put_u32(buf, 12, self.pad);
put_u64(buf, 16, self.volume_oid);
put_u64(buf, 24, self.oid);
put_u64(buf, 32, self.address);
}
#[must_use]
pub const fn kind(&self) -> u16 {
(self.type_word & 0xFFFF) as u16
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CheckpointMap {
pub header: ObjectHeader,
pub flags: u32,
pub count: u32,
}
impl CheckpointMap {
pub const MAPPINGS_AT: usize = 40;
pub fn read_from(block: &[u8]) -> Result<(Self, Vec<CheckpointMapping>), ParseError> {
ParseError::need("checkpoint-mapping block", Self::MAPPINGS_AT, block)?;
let map = Self {
header: ObjectHeader::read_from(block)?,
flags: get_u32(block, 32),
count: get_u32(block, 36),
};
let need = (map.count as usize)
.checked_mul(CheckpointMapping::SIZE)
.and_then(|n| n.checked_add(Self::MAPPINGS_AT))
.unwrap_or(usize::MAX);
ParseError::need("checkpoint-mapping block", need, block)?;
let mappings = (0..map.count as usize)
.map(|i| {
CheckpointMapping::read_from(
&block[Self::MAPPINGS_AT + i * CheckpointMapping::SIZE..],
)
})
.collect::<Result<_, _>>()?;
Ok((map, mappings))
}
pub fn write_to(&self, mappings: &[CheckpointMapping], block: &mut [u8]) {
let need = Self::MAPPINGS_AT + mappings.len() * CheckpointMapping::SIZE;
assert!(
block.len() >= need,
"a checkpoint-mapping block needs {need} bytes"
);
self.header.write_to(block);
put_u32(block, 32, self.flags);
put_u32(block, 36, self.count);
for (i, mapping) in mappings.iter().enumerate() {
mapping.write_to(&mut block[Self::MAPPINGS_AT + i * CheckpointMapping::SIZE..]);
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct SpaceDevice {
pub block_count: u64,
pub chunk_count: u64,
pub cib_count: u32,
pub cab_count: u32,
pub free_count: u64,
pub addr_offset: u32,
pub reserved: u32,
pub reserved2: u64,
}
impl SpaceDevice {
pub const SIZE: usize = 48;
fn read(buf: &[u8], at: usize) -> Self {
Self {
block_count: get_u64(buf, at),
chunk_count: get_u64(buf, at + 8),
cib_count: get_u32(buf, at + 16),
cab_count: get_u32(buf, at + 20),
free_count: get_u64(buf, at + 24),
addr_offset: get_u32(buf, at + 32),
reserved: get_u32(buf, at + 36),
reserved2: get_u64(buf, at + 40),
}
}
fn write(&self, buf: &mut [u8], at: usize) {
put_u64(buf, at, self.block_count);
put_u64(buf, at + 8, self.chunk_count);
put_u32(buf, at + 16, self.cib_count);
put_u32(buf, at + 20, self.cab_count);
put_u64(buf, at + 24, self.free_count);
put_u32(buf, at + 32, self.addr_offset);
put_u32(buf, at + 36, self.reserved);
put_u64(buf, at + 40, self.reserved2);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct FreeQueue {
pub count: u64,
pub tree_oid: u64,
pub oldest_xid: u64,
pub tree_node_limit: u16,
pub pad16: u16,
pub pad32: u32,
pub reserved: u64,
}
impl FreeQueue {
pub const SIZE: usize = 40;
fn read(buf: &[u8], at: usize) -> Self {
Self {
count: get_u64(buf, at),
tree_oid: get_u64(buf, at + 8),
oldest_xid: get_u64(buf, at + 16),
tree_node_limit: get_u16(buf, at + 24),
pad16: get_u16(buf, at + 26),
pad32: get_u32(buf, at + 28),
reserved: get_u64(buf, at + 32),
}
}
fn write(&self, buf: &mut [u8], at: usize) {
put_u64(buf, at, self.count);
put_u64(buf, at + 8, self.tree_oid);
put_u64(buf, at + 16, self.oldest_xid);
put_u16(buf, at + 24, self.tree_node_limit);
put_u16(buf, at + 26, self.pad16);
put_u32(buf, at + 28, self.pad32);
put_u64(buf, at + 32, self.reserved);
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SpaceManager {
pub header: ObjectHeader,
pub block_size: u32,
pub blocks_per_chunk: u32,
pub chunks_per_cib: u32,
pub cibs_per_cab: u32,
pub devices: [SpaceDevice; 2],
pub flags: u32,
pub ip_bm_tx_multiplier: u32,
pub ip_block_count: u64,
pub ip_bm_size_in_blocks: u32,
pub ip_bm_block_count: u32,
pub ip_bm_base: u64,
pub ip_base: u64,
pub fs_reserve_block_count: u64,
pub fs_reserve_alloc_count: u64,
pub free_queues: [FreeQueue; 3],
pub ip_bm_free_head: u16,
pub ip_bm_free_tail: u16,
pub ip_bm_xid_offset: u32,
pub ip_bitmap_offset: u32,
pub ip_bm_free_next_offset: u32,
pub version: u32,
pub struct_size: u32,
}
impl SpaceManager {
pub const SIZE: usize = 344;
pub const MAIN: usize = 0;
pub const INTERNAL_POOL: usize = 0;
pub const MAIN_QUEUE: usize = 1;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("space manager", Self::SIZE, buf)?;
Ok(Self {
header: ObjectHeader::read_from(buf)?,
block_size: get_u32(buf, 32),
blocks_per_chunk: get_u32(buf, 36),
chunks_per_cib: get_u32(buf, 40),
cibs_per_cab: get_u32(buf, 44),
devices: [SpaceDevice::read(buf, 48), SpaceDevice::read(buf, 96)],
flags: get_u32(buf, 144),
ip_bm_tx_multiplier: get_u32(buf, 148),
ip_block_count: get_u64(buf, 152),
ip_bm_size_in_blocks: get_u32(buf, 160),
ip_bm_block_count: get_u32(buf, 164),
ip_bm_base: get_u64(buf, 168),
ip_base: get_u64(buf, 176),
fs_reserve_block_count: get_u64(buf, 184),
fs_reserve_alloc_count: get_u64(buf, 192),
free_queues: [
FreeQueue::read(buf, 200),
FreeQueue::read(buf, 240),
FreeQueue::read(buf, 280),
],
ip_bm_free_head: get_u16(buf, 320),
ip_bm_free_tail: get_u16(buf, 322),
ip_bm_xid_offset: get_u32(buf, 324),
ip_bitmap_offset: get_u32(buf, 328),
ip_bm_free_next_offset: get_u32(buf, 332),
version: get_u32(buf, 336),
struct_size: get_u32(buf, 340),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a space manager needs {} bytes",
Self::SIZE
);
self.header.write_to(buf);
put_u32(buf, 32, self.block_size);
put_u32(buf, 36, self.blocks_per_chunk);
put_u32(buf, 40, self.chunks_per_cib);
put_u32(buf, 44, self.cibs_per_cab);
self.devices[0].write(buf, 48);
self.devices[1].write(buf, 96);
put_u32(buf, 144, self.flags);
put_u32(buf, 148, self.ip_bm_tx_multiplier);
put_u64(buf, 152, self.ip_block_count);
put_u32(buf, 160, self.ip_bm_size_in_blocks);
put_u32(buf, 164, self.ip_bm_block_count);
put_u64(buf, 168, self.ip_bm_base);
put_u64(buf, 176, self.ip_base);
put_u64(buf, 184, self.fs_reserve_block_count);
put_u64(buf, 192, self.fs_reserve_alloc_count);
for (i, queue) in self.free_queues.iter().enumerate() {
queue.write(buf, 200 + i * FreeQueue::SIZE);
}
put_u16(buf, 320, self.ip_bm_free_head);
put_u16(buf, 322, self.ip_bm_free_tail);
put_u32(buf, 324, self.ip_bm_xid_offset);
put_u32(buf, 328, self.ip_bitmap_offset);
put_u32(buf, 332, self.ip_bm_free_next_offset);
put_u32(buf, 336, self.version);
put_u32(buf, 340, self.struct_size);
}
pub fn ip_bitmap_xids(&self, object: &[u8]) -> Result<Vec<u64>, ParseError> {
let n = self.ip_bm_size_in_blocks as usize;
array(
object,
self.ip_bm_xid_offset,
n,
8,
"internal-pool bitmap transactions",
)
.map(|at| at.map(|off| get_u64(object, off)).collect())
}
pub fn ip_bitmap_indices(&self, object: &[u8]) -> Result<Vec<u16>, ParseError> {
let n = self.ip_bm_size_in_blocks as usize;
array(
object,
self.ip_bitmap_offset,
n,
2,
"internal-pool bitmap indices",
)
.map(|at| at.map(|off| get_u16(object, off)).collect())
}
pub fn ip_bitmap_free_next(&self, object: &[u8]) -> Result<Vec<u16>, ParseError> {
let n = self.ip_bm_block_count as usize;
array(
object,
self.ip_bm_free_next_offset,
n,
2,
"internal-pool bitmap free list",
)
.map(|at| at.map(|off| get_u16(object, off)).collect())
}
pub fn addresses(&self, device: usize, object: &[u8]) -> Result<Vec<u64>, ParseError> {
let dev = &self.devices[device];
let n = if dev.cab_count != 0 {
dev.cab_count
} else {
dev.cib_count
} as usize;
array(object, dev.addr_offset, n, 8, "space manager address list")
.map(|at| at.map(|off| get_u64(object, off)).collect())
}
}
fn array(
object: &[u8],
at: u32,
n: usize,
width: usize,
structure: &'static str,
) -> Result<impl Iterator<Item = usize>, ParseError> {
let at = at as usize;
let need = n
.checked_mul(width)
.and_then(|len| len.checked_add(at))
.unwrap_or(usize::MAX);
ParseError::need(structure, need, object)?;
Ok((0..n).map(move |i| at + i * width))
}
pub const CI_COUNT_MASK: u32 = 0x000F_FFFF;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct ChunkInfo {
pub xid: u64,
pub address: u64,
pub block_count: u32,
pub free_count: u32,
pub bitmap: u64,
}
impl ChunkInfo {
pub const SIZE: usize = 32;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("chunk information", Self::SIZE, buf)?;
Ok(Self {
xid: get_u64(buf, 0),
address: get_u64(buf, 8),
block_count: get_u32(buf, 16),
free_count: get_u32(buf, 20),
bitmap: get_u64(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(buf.len() >= Self::SIZE, "chunk information needs 32 bytes");
put_u64(buf, 0, self.xid);
put_u64(buf, 8, self.address);
put_u32(buf, 16, self.block_count);
put_u32(buf, 20, self.free_count);
put_u64(buf, 24, self.bitmap);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ChunkList {
pub header: ObjectHeader,
pub index: u32,
pub count: u32,
}
impl ChunkList {
pub const ENTRIES_AT: usize = 40;
pub fn read_from(block: &[u8]) -> Result<Self, ParseError> {
ParseError::need("chunk list", Self::ENTRIES_AT, block)?;
Ok(Self {
header: ObjectHeader::read_from(block)?,
index: get_u32(block, 32),
count: get_u32(block, 36),
})
}
pub fn write_to(&self, block: &mut [u8]) {
assert!(
block.len() >= Self::ENTRIES_AT,
"a chunk list needs 40 bytes"
);
self.header.write_to(block);
put_u32(block, 32, self.index);
put_u32(block, 36, self.count);
}
pub fn chunks(&self, block: &[u8]) -> Result<Vec<ChunkInfo>, ParseError> {
array(
block,
Self::ENTRIES_AT as u32,
self.count as usize,
ChunkInfo::SIZE,
"chunk-info block",
)?
.map(|off| ChunkInfo::read_from(&block[off..]))
.collect()
}
pub fn addresses(&self, block: &[u8]) -> Result<Vec<u64>, ParseError> {
array(
block,
Self::ENTRIES_AT as u32,
self.count as usize,
8,
"chunk-info address block",
)
.map(|at| at.map(|off| get_u64(block, off)).collect())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct FreeQueueKey {
pub xid: u64,
pub address: u64,
}
impl FreeQueueKey {
pub const SIZE: usize = 16;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("free queue key", Self::SIZE, buf)?;
Ok(Self {
xid: get_u64(buf, 0),
address: get_u64(buf, 8),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(buf.len() >= Self::SIZE, "a free queue key needs 16 bytes");
put_u64(buf, 0, self.xid);
put_u64(buf, 8, self.address);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Reaper {
pub header: ObjectHeader,
pub next_reap_id: u64,
pub completed_id: u64,
pub head: u64,
pub tail: u64,
pub flags: u32,
pub list_count: u32,
pub kind: u32,
pub size: u32,
pub volume_oid: u64,
pub oid: u64,
pub xid: u64,
pub entry_flags: u32,
pub state_buffer_size: u32,
}
impl Reaper {
pub const SIZE: usize = 112;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("reaper", Self::SIZE, buf)?;
Ok(Self {
header: ObjectHeader::read_from(buf)?,
next_reap_id: get_u64(buf, 32),
completed_id: get_u64(buf, 40),
head: get_u64(buf, 48),
tail: get_u64(buf, 56),
flags: get_u32(buf, 64),
list_count: get_u32(buf, 68),
kind: get_u32(buf, 72),
size: get_u32(buf, 76),
volume_oid: get_u64(buf, 80),
oid: get_u64(buf, 88),
xid: get_u64(buf, 96),
entry_flags: get_u32(buf, 104),
state_buffer_size: get_u32(buf, 108),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a reaper needs {} bytes",
Self::SIZE
);
self.header.write_to(buf);
put_u64(buf, 32, self.next_reap_id);
put_u64(buf, 40, self.completed_id);
put_u64(buf, 48, self.head);
put_u64(buf, 56, self.tail);
put_u32(buf, 64, self.flags);
put_u32(buf, 68, self.list_count);
put_u32(buf, 72, self.kind);
put_u32(buf, 76, self.size);
put_u64(buf, 80, self.volume_oid);
put_u64(buf, 88, self.oid);
put_u64(buf, 96, self.xid);
put_u32(buf, 104, self.entry_flags);
put_u32(buf, 108, self.state_buffer_size);
}
}
pub const REAPER_ALWAYS: u32 = 0x1;
pub const REAPER_CONTINUE: u32 = 0x2;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ReapList {
pub header: ObjectHeader,
pub next: u64,
pub flags: u32,
pub max: u32,
pub count: u32,
pub first: u32,
pub last: u32,
pub free: u32,
}
impl ReapList {
pub const SIZE: usize = 64;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
ParseError::need("reap list", Self::SIZE, buf)?;
Ok(Self {
header: ObjectHeader::read_from(buf)?,
next: get_u64(buf, 32),
flags: get_u32(buf, 40),
max: get_u32(buf, 44),
count: get_u32(buf, 48),
first: get_u32(buf, 52),
last: get_u32(buf, 56),
free: get_u32(buf, 60),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a reap list needs {} bytes",
Self::SIZE
);
self.header.write_to(buf);
put_u64(buf, 32, self.next);
put_u32(buf, 40, self.flags);
put_u32(buf, 44, self.max);
put_u32(buf, 48, self.count);
put_u32(buf, 52, self.first);
put_u32(buf, 56, self.last);
put_u32(buf, 60, self.free);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_space_manager_reads_back_as_written() {
let sm = SpaceManager {
header: ObjectHeader {
checksum: 0,
oid: 1024,
xid: 2,
type_word: 0x8000_0005,
subtype: 0,
},
block_size: 4096,
blocks_per_chunk: 32768,
chunks_per_cib: 126,
cibs_per_cab: 507,
devices: [
SpaceDevice {
block_count: 16384,
chunk_count: 1,
cib_count: 1,
cab_count: 0,
free_count: 16183,
addr_offset: 2568,
reserved: 0,
reserved2: 0,
},
SpaceDevice::default(),
],
flags: 1,
ip_bm_tx_multiplier: 16,
ip_block_count: 6,
ip_bm_size_in_blocks: 1,
ip_bm_block_count: 16,
ip_bm_base: 169,
ip_base: 185,
fs_reserve_block_count: 0,
fs_reserve_alloc_count: 0,
free_queues: [
FreeQueue {
count: 2,
tree_oid: 1027,
oldest_xid: 2,
tree_node_limit: 1,
..FreeQueue::default()
},
FreeQueue {
count: 2,
tree_oid: 1029,
oldest_xid: 2,
tree_node_limit: 4,
..FreeQueue::default()
},
FreeQueue::default(),
],
ip_bm_free_head: 2,
ip_bm_free_tail: 0,
ip_bm_xid_offset: 2520,
ip_bitmap_offset: 2528,
ip_bm_free_next_offset: 2536,
version: 1,
struct_size: 2520,
};
let mut object = vec![0u8; 4096];
sm.write_to(&mut object);
put_u64(&mut object, 2520, 2);
put_u16(&mut object, 2528, 1);
put_u64(&mut object, 2568, 187);
let back = SpaceManager::read_from(&object).expect("a space manager");
assert_eq!(back, sm);
assert_eq!(back.ip_bitmap_xids(&object), Ok(vec![2]));
assert_eq!(back.ip_bitmap_indices(&object), Ok(vec![1]));
assert_eq!(back.addresses(SpaceManager::MAIN, &object), Ok(vec![187]));
assert_eq!(
back.ip_bitmap_free_next(&object).map(|list| list.len()),
Ok(16)
);
}
#[test]
fn an_array_past_the_object_is_refused() {
let mut object = vec![0u8; SpaceManager::SIZE];
put_u32(&mut object, 160, 1);
put_u32(&mut object, 324, 4000);
let sm = SpaceManager::read_from(&object).expect("a space manager");
assert!(matches!(
sm.ip_bitmap_xids(&object),
Err(ParseError::TooShort { .. })
));
}
#[test]
fn a_mapping_block_reads_every_mapping_its_count_names() {
let mappings = [
CheckpointMapping {
type_word: 0x8000_0005,
subtype: 0,
size: 4096,
pad: 0,
volume_oid: 0,
oid: 1024,
address: 11,
},
CheckpointMapping {
type_word: 0x8000_0002,
subtype: 9,
size: 4096,
pad: 0,
volume_oid: 0,
oid: 1027,
address: 13,
},
];
let map = CheckpointMap {
header: ObjectHeader {
checksum: 0,
oid: 3,
xid: 2,
type_word: 0x4000_000C,
subtype: 0,
},
flags: CHECKPOINT_MAP_LAST,
count: 2,
};
let mut block = vec![0u8; 4096];
map.write_to(&mappings, &mut block);
let (back, read) = CheckpointMap::read_from(&block).expect("a mapping block");
assert_eq!(back, map);
assert_eq!(read, mappings);
assert_eq!(read[0].kind(), super::super::kind::SPACE_MANAGER);
put_u32(&mut block, 36, 200);
assert!(CheckpointMap::read_from(&block).is_err());
}
#[test]
fn a_chunk_info_block_reads_its_chunks() {
let list = ChunkList {
header: ObjectHeader {
checksum: 0,
oid: 187,
xid: 2,
type_word: 0x4000_0007,
subtype: 0,
},
index: 0,
count: 1,
};
let chunk = ChunkInfo {
xid: 2,
address: 0,
block_count: 16384,
free_count: 16183,
bitmap: 188,
};
let mut block = vec![0u8; 4096];
list.write_to(&mut block);
chunk.write_to(&mut block[ChunkList::ENTRIES_AT..]);
let back = ChunkList::read_from(&block).expect("a chunk list");
assert_eq!(back, list);
assert_eq!(back.chunks(&block), Ok(vec![chunk]));
}
}