2020-08-06 12:21:48 +05:30
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use nom::{number::complete::*, IResult};
|
2020-08-08 17:59:30 +05:30
|
|
|
use std::sync::{Arc, Mutex};
|
2020-08-06 12:21:48 +05:30
|
|
|
|
2020-08-10 12:59:32 +05:30
|
|
|
use crate::io_engine::*;
|
2020-08-06 12:21:48 +05:30
|
|
|
use crate::pdata::btree::Unpack;
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SMRoot {
|
|
|
|
pub nr_blocks: u64,
|
|
|
|
pub nr_allocated: u64,
|
|
|
|
pub bitmap_root: u64,
|
|
|
|
pub ref_count_root: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unpack_root(data: &[u8]) -> Result<SMRoot> {
|
|
|
|
match SMRoot::unpack(data) {
|
2020-08-08 14:24:16 +05:30
|
|
|
Err(_e) => Err(anyhow!("couldn't parse SMRoot")),
|
2020-08-06 12:21:48 +05:30
|
|
|
Ok((_i, v)) => Ok(v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpack for SMRoot {
|
|
|
|
fn disk_size() -> u32 {
|
|
|
|
32
|
|
|
|
}
|
2020-08-08 14:24:16 +05:30
|
|
|
|
2020-08-06 12:21:48 +05:30
|
|
|
fn unpack(data: &[u8]) -> IResult<&[u8], SMRoot> {
|
|
|
|
let (i, nr_blocks) = le_u64(data)?;
|
|
|
|
let (i, nr_allocated) = le_u64(i)?;
|
|
|
|
let (i, bitmap_root) = le_u64(i)?;
|
|
|
|
let (i, ref_count_root) = le_u64(i)?;
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
SMRoot {
|
|
|
|
nr_blocks,
|
|
|
|
nr_allocated,
|
|
|
|
bitmap_root,
|
|
|
|
ref_count_root,
|
|
|
|
},
|
|
|
|
))
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct IndexEntry {
|
|
|
|
pub blocknr: u64,
|
|
|
|
pub nr_free: u32,
|
|
|
|
pub none_free_before: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpack for IndexEntry {
|
|
|
|
fn disk_size() -> u32 {
|
|
|
|
16
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unpack(data: &[u8]) -> IResult<&[u8], Self> {
|
|
|
|
let (i, blocknr) = le_u64(data)?;
|
|
|
|
let (i, nr_free) = le_u32(i)?;
|
|
|
|
let (i, none_free_before) = le_u32(i)?;
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
IndexEntry {
|
|
|
|
blocknr,
|
|
|
|
nr_free,
|
|
|
|
none_free_before,
|
|
|
|
},
|
|
|
|
))
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BitmapHeader {
|
|
|
|
pub csum: u32,
|
|
|
|
pub not_used: u32,
|
|
|
|
pub blocknr: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpack for BitmapHeader {
|
|
|
|
fn disk_size() -> u32 {
|
|
|
|
16
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unpack(data: &[u8]) -> IResult<&[u8], Self> {
|
|
|
|
let (i, csum) = le_u32(data)?;
|
|
|
|
let (i, not_used) = le_u32(i)?;
|
|
|
|
let (i, blocknr) = le_u64(i)?;
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
BitmapHeader {
|
|
|
|
csum,
|
|
|
|
not_used,
|
|
|
|
blocknr,
|
|
|
|
},
|
|
|
|
))
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2020-08-06 12:21:48 +05:30
|
|
|
pub enum BitmapEntry {
|
|
|
|
Small(u8),
|
|
|
|
Overflow,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Bitmap {
|
|
|
|
pub header: BitmapHeader,
|
|
|
|
pub entries: Vec<BitmapEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpack for Bitmap {
|
|
|
|
fn disk_size() -> u32 {
|
|
|
|
BLOCK_SIZE as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unpack(data: &[u8]) -> IResult<&[u8], Self> {
|
|
|
|
let (mut i, header) = BitmapHeader::unpack(data)?;
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
let mut entries = Vec::new();
|
2020-08-06 12:21:48 +05:30
|
|
|
let nr_words = (BLOCK_SIZE - BitmapHeader::disk_size() as usize) / 8;
|
|
|
|
for _w in 0..nr_words {
|
|
|
|
let (tmp, mut word) = le_u64(i)?;
|
|
|
|
|
|
|
|
for _b in 0..32 {
|
|
|
|
let val = word & 0x3;
|
2020-08-07 20:11:21 +05:30
|
|
|
word >>= 2;
|
2020-08-06 12:21:48 +05:30
|
|
|
|
|
|
|
if val < 3 {
|
|
|
|
entries.push(BitmapEntry::Small(val as u8));
|
|
|
|
} else {
|
|
|
|
entries.push(BitmapEntry::Overflow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = tmp;
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:24:16 +05:30
|
|
|
Ok((i, Bitmap { header, entries }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
pub trait SpaceMap {
|
|
|
|
fn get(&self, b: u64) -> Result<u32>;
|
|
|
|
fn inc(&mut self, begin: u64, len: u64) -> Result<()>;
|
|
|
|
}
|
2020-08-08 17:06:13 +05:30
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
pub struct CoreSpaceMap<T> {
|
|
|
|
counts: Vec<T>,
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
impl<V> CoreSpaceMap<V>
|
|
|
|
where
|
|
|
|
V: Copy + Default + std::ops::AddAssign + From<u8>,
|
|
|
|
{
|
|
|
|
pub fn new(nr_entries: u64) -> CoreSpaceMap<V> {
|
2020-08-08 14:24:16 +05:30
|
|
|
CoreSpaceMap {
|
2020-08-08 17:59:30 +05:30
|
|
|
counts: vec![V::default(); nr_entries as usize],
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
|
|
|
}
|
2020-08-08 17:59:30 +05:30
|
|
|
}
|
2020-08-08 14:24:16 +05:30
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
impl<V> SpaceMap for CoreSpaceMap<V>
|
|
|
|
where
|
|
|
|
V: Copy + Default + std::ops::AddAssign + From<u8> + Into<u32>,
|
|
|
|
{
|
|
|
|
fn get(&self, b: u64) -> Result<u32> {
|
|
|
|
Ok(self.counts[b as usize].into())
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
fn inc(&mut self, begin: u64, len: u64) -> Result<()> {
|
|
|
|
for b in begin..(begin + len) {
|
|
|
|
self.counts[b as usize] += V::from(1u8);
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
2020-08-08 17:59:30 +05:30
|
|
|
Ok(())
|
2020-08-08 14:24:16 +05:30
|
|
|
}
|
2020-08-08 17:59:30 +05:30
|
|
|
}
|
2020-08-08 14:24:16 +05:30
|
|
|
|
2020-08-08 17:59:30 +05:30
|
|
|
pub fn core_sm(nr_entries: u64, max_count: u32) -> Arc<Mutex<dyn SpaceMap + Send>> {
|
|
|
|
if max_count <= u8::MAX as u32 {
|
|
|
|
Arc::new(Mutex::new(CoreSpaceMap::<u8>::new(nr_entries)))
|
|
|
|
} else if max_count <= u16::MAX as u32 {
|
|
|
|
Arc::new(Mutex::new(CoreSpaceMap::<u16>::new(nr_entries)))
|
|
|
|
} else {
|
|
|
|
Arc::new(Mutex::new(CoreSpaceMap::<u32>::new(nr_entries)))
|
2020-08-06 12:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|