use crate::bytes::{get_u8, get_u32, get_u64, put_u8, put_u32, put_u64};
use crate::flags::flag_set;
use super::{BlockGroupFlags, ItemType, ParseError};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentFlags(u64);
flag_set!(ExtentFlags: u64);
impl ExtentFlags {
pub const DATA: Self = Self(1 << 0);
pub const TREE_BLOCK: Self = Self(1 << 1);
pub const FULL_BACKREF: Self = Self(1 << 8);
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentItem {
pub refs: u64,
pub generation: u64,
pub flags: ExtentFlags,
}
impl ExtentItem {
pub const SIZE: usize = 24;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_extent_item",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
refs: get_u64(buf, 0),
generation: get_u64(buf, 8),
flags: ExtentFlags::from_bits(get_u64(buf, 16)),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an extent item needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u64(buf, 0, self.refs);
put_u64(buf, 8, self.generation);
put_u64(buf, 16, self.flags.bits());
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct InlineRef {
pub kind: ItemType,
pub offset: u64,
}
impl InlineRef {
pub const SIZE: usize = 9;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_extent_inline_ref",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
kind: ItemType::from_value(get_u8(buf, 0)),
offset: get_u64(buf, 1),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an inline extent reference needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u8(buf, 0, self.kind.value());
put_u64(buf, 1, self.offset);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ExtentDataRef {
pub root: u64,
pub objectid: u64,
pub offset: u64,
pub count: u32,
}
impl ExtentDataRef {
pub const SIZE: usize = 28;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_extent_data_ref",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
root: get_u64(buf, 0),
objectid: get_u64(buf, 8),
offset: get_u64(buf, 16),
count: get_u32(buf, 24),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"an extent data reference needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u64(buf, 0, self.root);
put_u64(buf, 8, self.objectid);
put_u64(buf, 16, self.offset);
put_u32(buf, 24, self.count);
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct BlockGroupItem {
pub used: u64,
pub chunk_objectid: u64,
pub flags: BlockGroupFlags,
}
impl BlockGroupItem {
pub const SIZE: usize = 24;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_block_group_item",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
used: get_u64(buf, 0),
chunk_objectid: get_u64(buf, 8),
flags: BlockGroupFlags::from_bits(get_u64(buf, 16)),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a block group item needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u64(buf, 0, self.used);
put_u64(buf, 8, self.chunk_objectid);
put_u64(buf, 16, self.flags.bits());
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct FreeSpaceInfo {
pub extent_count: u32,
pub flags: u32,
}
impl FreeSpaceInfo {
pub const SIZE: usize = 8;
pub const USING_BITMAPS: u32 = 1;
pub fn read_from(buf: &[u8]) -> Result<Self, ParseError> {
if buf.len() < Self::SIZE {
return Err(ParseError::TooShort {
structure: "btrfs_free_space_info",
need: Self::SIZE,
got: buf.len(),
});
}
Ok(Self {
extent_count: get_u32(buf, 0),
flags: get_u32(buf, 4),
})
}
pub fn write_to(&self, buf: &mut [u8]) {
assert!(
buf.len() >= Self::SIZE,
"a free space info record needs {} bytes and was given {}",
Self::SIZE,
buf.len()
);
put_u32(buf, 0, self.extent_count);
put_u32(buf, 4, self.flags);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::btrfs::ondisk::objectid;
#[test]
fn an_extent_item_and_its_reference_round_trip_through_their_bytes() {
let item = ExtentItem {
refs: 1,
generation: 7,
flags: ExtentFlags::TREE_BLOCK,
};
let reference = InlineRef {
kind: ItemType::TREE_BLOCK_REF,
offset: objectid::FS_TREE,
};
let mut bytes = vec![0u8; ExtentItem::SIZE + InlineRef::SIZE];
item.write_to(&mut bytes);
reference.write_to(&mut bytes[ExtentItem::SIZE..]);
assert_eq!(ExtentItem::read_from(&bytes), Ok(item));
assert_eq!(
InlineRef::read_from(&bytes[ExtentItem::SIZE..]),
Ok(reference)
);
assert_eq!(bytes[ExtentItem::SIZE], ItemType::TREE_BLOCK_REF.value());
}
#[test]
fn a_block_group_and_a_free_space_record_round_trip_through_their_bytes() {
let group = BlockGroupItem {
used: 147_456,
chunk_objectid: objectid::FIRST_CHUNK_TREE,
flags: BlockGroupFlags::METADATA | BlockGroupFlags::DUP,
};
let mut bytes = [0u8; BlockGroupItem::SIZE];
group.write_to(&mut bytes);
assert_eq!(BlockGroupItem::read_from(&bytes), Ok(group));
let info = FreeSpaceInfo {
extent_count: 3,
flags: 0,
};
let mut bytes = [0u8; FreeSpaceInfo::SIZE];
info.write_to(&mut bytes);
assert_eq!(FreeSpaceInfo::read_from(&bytes), Ok(info));
}
#[test]
fn every_record_here_refuses_a_buffer_too_short_to_hold_it() {
for got in 0..ExtentItem::SIZE {
assert!(ExtentItem::read_from(&vec![0u8; got]).is_err(), "{got}");
}
for got in 0..InlineRef::SIZE {
assert!(InlineRef::read_from(&vec![0u8; got]).is_err(), "{got}");
}
for got in 0..BlockGroupItem::SIZE {
assert!(BlockGroupItem::read_from(&vec![0u8; got]).is_err(), "{got}");
}
for got in 0..FreeSpaceInfo::SIZE {
assert!(FreeSpaceInfo::read_from(&vec![0u8; got]).is_err(), "{got}");
}
}
}