[checksum] Support cache and era superblock

This commit is contained in:
Ming-Hung Tsai 2021-05-19 17:11:58 +08:00
parent 159dda9659
commit 511ae9e908
3 changed files with 17 additions and 7 deletions

View File

@ -5,8 +5,9 @@ use crc32c::crc32c;
use std::io::Cursor;
const BLOCK_SIZE: u64 = 4096;
#[allow(dead_code)]
const SUPERBLOCK_CSUM_XOR: u32 = 160774;
const THIN_SUPERBLOCK_CSUM_XOR: u32 = 160774;
const CACHE_SUPERBLOCK_CSUM_XOR: u32 = 9031977;
const ERA_SUPERBLOCK_CSUM_XOR: u32 = 146538381;
const BITMAP_CSUM_XOR: u32 = 240779;
const INDEX_CSUM_XOR: u32 = 160478;
const BTREE_CSUM_XOR: u32 = 121107;
@ -18,8 +19,11 @@ fn checksum(buf: &[u8]) -> u32 {
#[derive(Debug, PartialEq)]
#[allow(clippy::upper_case_acronyms)]
#[allow(non_camel_case_types)]
pub enum BT {
SUPERBLOCK,
THIN_SUPERBLOCK,
CACHE_SUPERBLOCK,
ERA_SUPERBLOCK,
NODE,
INDEX,
BITMAP,
@ -39,7 +43,9 @@ pub fn metadata_block_type(buf: &[u8]) -> BT {
let btype = csum ^ sum_on_disk;
match btype {
SUPERBLOCK_CSUM_XOR => BT::SUPERBLOCK,
THIN_SUPERBLOCK_CSUM_XOR => BT::THIN_SUPERBLOCK,
CACHE_SUPERBLOCK_CSUM_XOR => BT::CACHE_SUPERBLOCK,
ERA_SUPERBLOCK_CSUM_XOR => BT::ERA_SUPERBLOCK,
BTREE_CSUM_XOR => BT::NODE,
BITMAP_CSUM_XOR => BT::BITMAP,
INDEX_CSUM_XOR => BT::INDEX,
@ -55,7 +61,9 @@ pub fn write_checksum(buf: &mut [u8], kind: BT) -> Result<()> {
use BT::*;
let salt = match kind {
SUPERBLOCK => SUPERBLOCK_CSUM_XOR,
THIN_SUPERBLOCK => THIN_SUPERBLOCK_CSUM_XOR,
CACHE_SUPERBLOCK => CACHE_SUPERBLOCK_CSUM_XOR,
ERA_SUPERBLOCK => ERA_SUPERBLOCK_CSUM_XOR,
NODE => BTREE_CSUM_XOR,
BITMAP => BITMAP_CSUM_XOR,
INDEX => INDEX_CSUM_XOR,

View File

@ -205,7 +205,9 @@ where
fn pack_block<W: Write>(w: &mut W, kind: BT, buf: &[u8]) -> Result<()> {
match kind {
BT::SUPERBLOCK => pack_superblock(w, buf).context("unable to pack superblock")?,
BT::THIN_SUPERBLOCK | BT::CACHE_SUPERBLOCK | BT::ERA_SUPERBLOCK => {
pack_superblock(w, buf).context("unable to pack superblock")?
}
BT::NODE => pack_btree_node(w, buf).context("unable to pack btree node")?,
BT::INDEX => pack_index(w, buf).context("unable to pack space map index")?,
BT::BITMAP => pack_bitmap(w, buf).context("unable to pack space map bitmap")?,

View File

@ -137,7 +137,7 @@ pub fn write_superblock(engine: &dyn IoEngine, _loc: u64, sb: &Superblock) -> Re
}
// calculate the checksum
write_checksum(b.get_data(), BT::SUPERBLOCK)?;
write_checksum(b.get_data(), BT::THIN_SUPERBLOCK)?;
// write
engine.write(&b)?;