[cache (rust)] Implement Pack for superblock

This commit is contained in:
Ming-Hung Tsai 2021-05-19 17:16:54 +08:00
parent 511ae9e908
commit 1198a3f713
2 changed files with 84 additions and 4 deletions

View File

@ -1,14 +1,19 @@
use anyhow::{anyhow, Result};
use byteorder::{LittleEndian, WriteBytesExt};
use nom::{bytes::complete::*, number::complete::*, IResult};
use std::io::Cursor;
use crate::checksum::*;
use crate::io_engine::*;
//------------------------------------------
pub const SPACE_MAP_ROOT_SIZE: usize = 128;
pub const SUPERBLOCK_LOCATION: u64 = 0;
const MAGIC: u64 = 0o6142003; // 0x18c403 in hex
const POLICY_NAME_SIZE: usize = 16;
const SPACE_MAP_ROOT_SIZE: usize = 128;
const UUID_SIZE: usize = 16;
//------------------------------------------
@ -38,7 +43,6 @@ pub struct Superblock {
pub discard_nr_blocks: u64,
pub data_block_size: u32,
pub metadata_block_size: u32,
pub cache_blocks: u32,
pub compat_flags: u32,
@ -71,7 +75,7 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> {
let (i, discard_nr_blocks) = le_u64(i)?;
let (i, data_block_size) = le_u32(i)?;
let (i, metadata_block_size) = le_u32(i)?;
let (i, _metadata_block_size) = le_u32(i)?;
let (i, cache_blocks) = le_u32(i)?;
let (i, compat_flags) = le_u32(i)?;
@ -115,7 +119,6 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> {
discard_block_size,
discard_nr_blocks,
data_block_size,
metadata_block_size,
cache_blocks,
compat_flags,
compat_ro_flags,
@ -139,3 +142,79 @@ pub fn read_superblock(engine: &dyn IoEngine, loc: u64) -> Result<Superblock> {
}
//------------------------------------------
fn pack_superblock<W: WriteBytesExt>(sb: &Superblock, w: &mut W) -> Result<()> {
// checksum, which we don't know yet
w.write_u32::<LittleEndian>(0)?;
// flags
let mut flags: u32 = 0;
if sb.flags.clean_shutdown {
flags |= 0x1;
}
if sb.flags.needs_check {
flags |= 0x2;
}
w.write_u32::<LittleEndian>(flags)?;
w.write_u64::<LittleEndian>(sb.block)?;
w.write_all(&[0; UUID_SIZE])?;
w.write_u64::<LittleEndian>(MAGIC)?;
w.write_u32::<LittleEndian>(sb.version)?;
let mut policy_name = [0u8; POLICY_NAME_SIZE];
policy_name[..sb.policy_name.len()].copy_from_slice(&sb.policy_name[..]);
w.write_all(&policy_name)?;
w.write_u32::<LittleEndian>(sb.policy_hint_size)?;
w.write_all(&[0; SPACE_MAP_ROOT_SIZE])?; // metadata sm root
w.write_u64::<LittleEndian>(sb.mapping_root)?;
w.write_u64::<LittleEndian>(sb.hint_root)?;
w.write_u64::<LittleEndian>(sb.discard_root)?;
w.write_u64::<LittleEndian>(sb.discard_block_size)?;
w.write_u64::<LittleEndian>(sb.discard_nr_blocks)?;
w.write_u32::<LittleEndian>(sb.data_block_size)?;
// metadata block size
w.write_u32::<LittleEndian>((BLOCK_SIZE >> SECTOR_SHIFT) as u32)?;
w.write_u32::<LittleEndian>(sb.cache_blocks)?;
w.write_u32::<LittleEndian>(sb.compat_flags)?;
w.write_u32::<LittleEndian>(sb.compat_ro_flags)?;
w.write_u32::<LittleEndian>(sb.incompat_flags)?;
w.write_u32::<LittleEndian>(sb.read_hits)?;
w.write_u32::<LittleEndian>(sb.read_misses)?;
w.write_u32::<LittleEndian>(sb.write_hits)?;
w.write_u32::<LittleEndian>(sb.write_misses)?;
w.write_u32::<LittleEndian>(sb.policy_version[0])?;
w.write_u32::<LittleEndian>(sb.policy_version[1])?;
w.write_u32::<LittleEndian>(sb.policy_version[2])?;
if sb.dirty_root.is_some() {
w.write_u64::<LittleEndian>(sb.dirty_root.unwrap())?;
}
Ok(())
}
pub fn write_superblock(engine: &dyn IoEngine, _loc: u64, sb: &Superblock) -> Result<()> {
let b = Block::zeroed(SUPERBLOCK_LOCATION);
// pack the superblock
{
let mut cursor = Cursor::new(b.get_data());
pack_superblock(sb, &mut cursor)?;
}
// calculate the checksum
write_checksum(b.get_data(), BT::CACHE_SUPERBLOCK)?;
// write
engine.write(&b)?;
Ok(())
}
//------------------------------------------

View File

@ -17,6 +17,7 @@ use crate::file_utils;
//------------------------------------------
pub const BLOCK_SIZE: usize = 4096;
pub const SECTOR_SHIFT: usize = 9;
const ALIGN: usize = 4096;
#[derive(Clone, Debug)]