idk stuff
This commit is contained in:
parent
f1d968fbf5
commit
9a8c8228a3
42
btrfs/src/crc32c.rs
Normal file
42
btrfs/src/crc32c.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
use core::ops::{BitAnd, BitXor};
|
||||||
|
|
||||||
|
static TABLE: [u32; 256] = [
|
||||||
|
0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb,
|
||||||
|
0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24,
|
||||||
|
0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384,
|
||||||
|
0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b,
|
||||||
|
0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35,
|
||||||
|
0xaa64d611, 0x580f5512, 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa,
|
||||||
|
0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad, 0x1642ae59, 0xe4292d5a,
|
||||||
|
0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595,
|
||||||
|
0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957,
|
||||||
|
0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f, 0xed03a29b, 0x1f682198,
|
||||||
|
0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927, 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38,
|
||||||
|
0xdbfc821c, 0x2997011f, 0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7,
|
||||||
|
0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789,
|
||||||
|
0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46,
|
||||||
|
0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6,
|
||||||
|
0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de, 0xdde0eb2a, 0x2f8b6829,
|
||||||
|
0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93,
|
||||||
|
0x082f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c,
|
||||||
|
0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc,
|
||||||
|
0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033,
|
||||||
|
0xa24bb5a6, 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d,
|
||||||
|
0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0x0e330a81, 0xfc588982,
|
||||||
|
0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622,
|
||||||
|
0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed,
|
||||||
|
0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f,
|
||||||
|
0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0,
|
||||||
|
0xd3d3e1ab, 0x21b862a8, 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540,
|
||||||
|
0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f,
|
||||||
|
0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1,
|
||||||
|
0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e,
|
||||||
|
0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e,
|
||||||
|
0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351,
|
||||||
|
];
|
||||||
|
|
||||||
|
pub fn calculate_crc32c(seed: u32, bytes: &[u8]) -> u32 {
|
||||||
|
bytes.iter().fold(seed, |rem, &b| {
|
||||||
|
TABLE[rem.bitxor(b as u32).bitand(0xff) as usize] ^ (rem >> 8)
|
||||||
|
})
|
||||||
|
}
|
133
btrfs/src/lib.rs
133
btrfs/src/lib.rs
|
@ -2,30 +2,25 @@
|
||||||
#![cfg_attr(not(any(feature = "std", test)), no_std)]
|
#![cfg_attr(not(any(feature = "std", test)), no_std)]
|
||||||
|
|
||||||
use core::{
|
use core::{
|
||||||
borrow::Borrow,
|
|
||||||
cell::{RefCell, RefMut},
|
cell::{RefCell, RefMut},
|
||||||
mem::size_of,
|
mem::size_of,
|
||||||
ops::RangeBounds,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use alloc::{
|
use alloc::{
|
||||||
borrow::Cow,
|
|
||||||
boxed::Box,
|
boxed::Box,
|
||||||
collections::{btree_map::Entry, BTreeMap},
|
collections::{btree_map::Entry, BTreeMap},
|
||||||
vec,
|
vec,
|
||||||
vec::Vec,
|
vec::Vec,
|
||||||
};
|
};
|
||||||
use scroll::{Endian, Pread};
|
use scroll::Pread;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use structs::{
|
use structs::{Chunk, Key, KeyPtr, KnownObjectId, ObjectType, RootItem, Stripe, Superblock};
|
||||||
BTreeNode, Chunk, Header, Item, Key, KeyPtr, KnownObjectId, ObjectType, RootItem, Stripe,
|
|
||||||
Superblock,
|
|
||||||
};
|
|
||||||
use tree::Tree;
|
use tree::Tree;
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
pub mod crc32c;
|
||||||
pub mod structs;
|
pub mod structs;
|
||||||
pub mod tree;
|
pub mod tree;
|
||||||
|
|
||||||
|
@ -139,21 +134,27 @@ impl PartialOrd for ChunkTreeKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ChunkCacheTree {
|
|
||||||
inner: BTreeMap<ChunkTreeKey, u64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChunkTree = BTreeMap<ChunkTreeKey, u64>;
|
type ChunkTree = BTreeMap<ChunkTreeKey, u64>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Volume<R: VolumeIo> {
|
pub struct Btrfs<R: VolumeIo> {
|
||||||
reader: Box<RefCell<R>>,
|
reader: Box<RefCell<R>>,
|
||||||
superblock: Superblock,
|
superblock: Superblock,
|
||||||
pub chunk_cache: ChunkTree,
|
pub chunk_cache: ChunkTree,
|
||||||
roots: BTreeMap<KnownObjectId, RootItem>,
|
roots: BTreeMap<KnownObjectId, RootItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: VolumeIo> Volume<R> {
|
pub struct Root<'a, R: VolumeIo> {
|
||||||
|
id: KnownObjectId,
|
||||||
|
tree: Tree<'a, R>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Volume<'a, R: VolumeIo> {
|
||||||
|
fs: &'a Btrfs<R>,
|
||||||
|
root: Root<'a, R>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: VolumeIo> Btrfs<R> {
|
||||||
pub fn reader(&self) -> RefMut<R> {
|
pub fn reader(&self) -> RefMut<R> {
|
||||||
self.reader.borrow_mut()
|
self.reader.borrow_mut()
|
||||||
}
|
}
|
||||||
|
@ -171,6 +172,7 @@ impl<R: VolumeIo> Volume<R> {
|
||||||
};
|
};
|
||||||
|
|
||||||
new.parse_chunk_tree()?;
|
new.parse_chunk_tree()?;
|
||||||
|
new.parse_root_tree()?;
|
||||||
|
|
||||||
Ok(new)
|
Ok(new)
|
||||||
}
|
}
|
||||||
|
@ -203,29 +205,46 @@ impl<R: VolumeIo> Volume<R> {
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_chunk_node(&mut self, chunk: Vec<u8>) -> Result<()> {
|
fn parse_root_tree(&mut self) -> Result<()> {
|
||||||
let node = BTreeNode::parse(&chunk)?;
|
let root_tree = Tree::from_logical_offset(self, self.superblock().root.get())?;
|
||||||
let bytes = &chunk[size_of::<Header>()..];
|
|
||||||
match node {
|
|
||||||
BTreeNode::Leaf(leaf) => {
|
|
||||||
leaf.items
|
|
||||||
.iter()
|
|
||||||
.filter(|item| item.key.ty.as_type() == ObjectType::ChunkItem)
|
|
||||||
.map(|item| {
|
|
||||||
let chunk = Chunk::parse(
|
|
||||||
&bytes[item.offset.get() as usize
|
|
||||||
..item.offset.get() as usize + item.size.get() as usize],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
|
let roots = root_tree
|
||||||
|
.full_range()
|
||||||
|
.filter_map(|(item, v)| match v {
|
||||||
|
structs::TreeItem::Root(root) => Some((item, root)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.map(|(item, root)| {
|
||||||
|
let id = item.key.id();
|
||||||
|
|
||||||
|
(id, root)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
self.roots.extend(roots);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_chunk_tree(&mut self) -> Result<()> {
|
||||||
|
let chunk_tree = Tree::from_logical_offset(self, self.superblock().chunk_root.get())?;
|
||||||
|
|
||||||
|
let chunks = chunk_tree
|
||||||
|
.full_range()
|
||||||
|
.filter_map(|(item, v)| match v {
|
||||||
|
structs::TreeItem::Chunk(chunk) => Some((item, chunk)),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
for (item, chunk) in chunks {
|
||||||
let start = item.key.offset.get() as u64;
|
let start = item.key.offset.get() as u64;
|
||||||
let end = start + chunk.length.get();
|
let end = start + chunk.length.get();
|
||||||
|
|
||||||
log::info!("chunk: [{start}, {end})");
|
|
||||||
match self.chunk_cache.entry(ChunkTreeKey { range: start..end }) {
|
match self.chunk_cache.entry(ChunkTreeKey { range: start..end }) {
|
||||||
Entry::Vacant(entry) => {
|
Entry::Vacant(entry) => {
|
||||||
log::info!("inserting chunk [{start}, {end})");
|
log::info!("inserting chunk [{start}, {end})");
|
||||||
entry.insert(chunk.stripe.offset.get());
|
entry.insert(chunk.stripe.offset.get());
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
Entry::Occupied(entry) => {
|
Entry::Occupied(entry) => {
|
||||||
log::warn!("overlapping stripes!");
|
log::warn!("overlapping stripes!");
|
||||||
|
@ -243,23 +262,6 @@ impl<R: VolumeIo> Volume<R> {
|
||||||
if *entry.get() != chunk.stripe.offset.get() {
|
if *entry.get() != chunk.stripe.offset.get() {
|
||||||
log::error!("\tprobably an error?");
|
log::error!("\tprobably an error?");
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
//Err(Error::InvalidOffset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<Result<()>>()?;
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
BTreeNode::Internal(inode) => {
|
|
||||||
for keyptr in inode.children.iter() {
|
|
||||||
// TODO: make this another error that actually reports what went wrong
|
|
||||||
let chunk = self.read_range(
|
|
||||||
self.range_from_logical(keyptr.blockptr.get())
|
|
||||||
.ok_or(Error::ReadFailed)?,
|
|
||||||
)?;
|
|
||||||
self.parse_chunk_node(chunk)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -267,28 +269,13 @@ impl<R: VolumeIo> Volume<R> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_keyptr(&self, keyptr: KeyPtr) -> Result<Vec<u8>> {
|
pub fn read_keyptr(&self, keyptr: &KeyPtr) -> Result<Vec<u8>> {
|
||||||
self.read_range(
|
self.read_range(
|
||||||
self.range_from_logical(keyptr.blockptr.get())
|
self.range_from_logical(keyptr.blockptr.get())
|
||||||
.ok_or(Error::ReadFailed)?,
|
.ok_or(Error::ReadFailed)?,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_chunk_tree(&mut self) -> Result<()> {
|
|
||||||
let chunk_root = self.superblock.chunk_root.get();
|
|
||||||
// let size = self.size_from_logical(chunk_root).expect("size");
|
|
||||||
|
|
||||||
log::debug!("chunk_root: {chunk_root}");
|
|
||||||
let contains = self.chunk_cache.contains_key(&chunk_root.into());
|
|
||||||
log::debug!("chunk_cache: {contains} {:?}", self.chunk_cache);
|
|
||||||
let physical = self.range_from_logical(chunk_root).expect("range");
|
|
||||||
|
|
||||||
let root = self.read_range(physical)?;
|
|
||||||
|
|
||||||
self.parse_chunk_node(root)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bootstrap_chunk_tree(superblock: &Superblock) -> Result<ChunkTree> {
|
pub fn bootstrap_chunk_tree(superblock: &Superblock) -> Result<ChunkTree> {
|
||||||
let array_size = superblock.sys_chunk_array_size.get() as usize;
|
let array_size = superblock.sys_chunk_array_size.get() as usize;
|
||||||
let mut offset: usize = 0;
|
let mut offset: usize = 0;
|
||||||
|
@ -370,6 +357,28 @@ impl<R: VolumeIo> Volume<R> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_default_subvol(&self) -> Result<()> {
|
||||||
|
let root_tree = Tree::from_logical_offset(self, self.superblock().root.get())?;
|
||||||
|
|
||||||
|
let range = root_tree.full_range();
|
||||||
|
|
||||||
|
let key = Key::new(
|
||||||
|
KnownObjectId::Custom(self.superblock.root_dir_objectid.get()),
|
||||||
|
ObjectType::DirItem,
|
||||||
|
0x8dbfc2d2, // crc of "default"
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some((item, value)) = root_tree.find_key(key)? {
|
||||||
|
//let fs_root = value.
|
||||||
|
Root {
|
||||||
|
id: item.key.id(),
|
||||||
|
tree: todo!(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::warn!("default subvol not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn superblock(&self) -> &Superblock {
|
pub fn superblock(&self) -> &Superblock {
|
||||||
&self.superblock
|
&self.superblock
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,14 @@ use core::{mem::size_of, ops::Deref};
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
use derivative::Derivative;
|
use derivative::Derivative;
|
||||||
use num_enum::{FromPrimitive, TryFromPrimitive};
|
use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive};
|
||||||
use scroll::{ctx::TryFromCtx, Pread, SizeWith};
|
use scroll::{ctx::TryFromCtx, Pread, SizeWith};
|
||||||
use zerocopy::{byteorder::LE, AsBytes, FromBytes, U16, U32, U64};
|
use zerocopy::{byteorder::LE, AsBytes, FromBytes, U16, U32, U64};
|
||||||
|
|
||||||
use crate::{Error, Result};
|
use crate::{Error, Result};
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromPrimitive, IntoPrimitive)]
|
||||||
#[repr(u64)]
|
#[repr(u64)]
|
||||||
pub enum KnownObjectId {
|
pub enum KnownObjectId {
|
||||||
RootTree = 1,
|
RootTree = 1,
|
||||||
|
@ -22,6 +22,8 @@ pub enum KnownObjectId {
|
||||||
QuotaTree,
|
QuotaTree,
|
||||||
UuidTree,
|
UuidTree,
|
||||||
FreeSpaceTree,
|
FreeSpaceTree,
|
||||||
|
__FirstFreeId = 256,
|
||||||
|
__LastFreeId = u64::MAX - 256,
|
||||||
DataRelocTree = u64::MAX - 9,
|
DataRelocTree = u64::MAX - 9,
|
||||||
TreeReloc = u64::MAX - 8,
|
TreeReloc = u64::MAX - 8,
|
||||||
TreeLog = u64::MAX - 7,
|
TreeLog = u64::MAX - 7,
|
||||||
|
@ -45,6 +47,11 @@ impl core::fmt::Debug for ObjectId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectId {
|
impl ObjectId {
|
||||||
|
pub fn from_id(id: KnownObjectId) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: U64::<LE>::new(id.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn as_id(self) -> KnownObjectId {
|
pub fn as_id(self) -> KnownObjectId {
|
||||||
KnownObjectId::from_primitive(self.inner.get())
|
KnownObjectId::from_primitive(self.inner.get())
|
||||||
}
|
}
|
||||||
|
@ -55,7 +62,7 @@ unsafe impl Zeroable for ObjectId {}
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromPrimitive, IntoPrimitive)]
|
||||||
//#[rustc_nonnull_optimization_guaranteed]
|
//#[rustc_nonnull_optimization_guaranteed]
|
||||||
pub enum ObjectType {
|
pub enum ObjectType {
|
||||||
INodeItem = 0x01,
|
INodeItem = 0x01,
|
||||||
|
@ -105,6 +112,9 @@ impl core::fmt::Debug for ObjectTypeWrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectTypeWrapper {
|
impl ObjectTypeWrapper {
|
||||||
|
pub fn from_ty(ty: ObjectType) -> Self {
|
||||||
|
Self { inner: ty.into() }
|
||||||
|
}
|
||||||
pub fn as_type(self) -> ObjectType {
|
pub fn as_type(self) -> ObjectType {
|
||||||
ObjectType::from_primitive(self.inner)
|
ObjectType::from_primitive(self.inner)
|
||||||
}
|
}
|
||||||
|
@ -143,6 +153,13 @@ pub struct Key {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Key {
|
impl Key {
|
||||||
|
pub fn new(id: KnownObjectId, ty: ObjectType, offset: u64) -> Self {
|
||||||
|
Self {
|
||||||
|
id: ObjectId::from_id(id),
|
||||||
|
ty: ObjectTypeWrapper::from_ty(ty),
|
||||||
|
offset: U64::new(offset),
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn ty(&self) -> ObjectType {
|
pub fn ty(&self) -> ObjectType {
|
||||||
self.ty.as_type()
|
self.ty.as_type()
|
||||||
}
|
}
|
||||||
|
@ -585,7 +602,7 @@ pub enum ChecksumType {
|
||||||
Blake2B,
|
Blake2B,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calculate_crc32c(bytes: &[u8]) -> [u8; 32] {
|
pub fn calculate_crc32c(bytes: &[u8]) -> [u8; 32] {
|
||||||
let crc = crc::Crc::<u32>::new(&crc::CRC_32_ISCSI);
|
let crc = crc::Crc::<u32>::new(&crc::CRC_32_ISCSI);
|
||||||
let mut csum = [0u8; 32];
|
let mut csum = [0u8; 32];
|
||||||
csum[..4].copy_from_slice(crc.checksum(bytes).as_bytes());
|
csum[..4].copy_from_slice(crc.checksum(bytes).as_bytes());
|
||||||
|
@ -686,7 +703,8 @@ pub struct Timespec {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C, packed)]
|
#[repr(C, packed)]
|
||||||
#[derive(Debug, Clone, Copy, FromBytes, AsBytes)]
|
#[derive(Derivative, Clone, Copy, FromBytes, AsBytes)]
|
||||||
|
#[derivative(Debug)]
|
||||||
pub struct InodeItem {
|
pub struct InodeItem {
|
||||||
/// nfs style generation number
|
/// nfs style generation number
|
||||||
pub generation: U64<LE>,
|
pub generation: U64<LE>,
|
||||||
|
@ -703,6 +721,7 @@ pub struct InodeItem {
|
||||||
pub flags: U64<LE>,
|
pub flags: U64<LE>,
|
||||||
/// modification sequence number for NFS
|
/// modification sequence number for NFS
|
||||||
pub sequence: U64<LE>,
|
pub sequence: U64<LE>,
|
||||||
|
#[derivative(Debug = "ignore")]
|
||||||
pub reserved: [u64; 4],
|
pub reserved: [u64; 4],
|
||||||
pub atime: Timespec,
|
pub atime: Timespec,
|
||||||
pub ctime: Timespec,
|
pub ctime: Timespec,
|
||||||
|
@ -711,7 +730,8 @@ pub struct InodeItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C, packed)]
|
#[repr(C, packed)]
|
||||||
#[derive(Debug, Clone, Copy, FromBytes, AsBytes)]
|
#[derive(Derivative, Clone, Copy, FromBytes, AsBytes)]
|
||||||
|
#[derivative(Debug)]
|
||||||
pub struct RootItem {
|
pub struct RootItem {
|
||||||
pub inode: InodeItem,
|
pub inode: InodeItem,
|
||||||
pub generation: U64<LE>,
|
pub generation: U64<LE>,
|
||||||
|
@ -741,6 +761,7 @@ pub struct RootItem {
|
||||||
pub otime: Timespec,
|
pub otime: Timespec,
|
||||||
pub stime: Timespec,
|
pub stime: Timespec,
|
||||||
pub rtime: Timespec,
|
pub rtime: Timespec,
|
||||||
|
#[derivative(Debug = "ignore")]
|
||||||
pub reserved: [u64; 8],
|
pub reserved: [u64; 8],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use core::{mem::size_of, ops::Deref};
|
use core::{mem::size_of, ops::Deref};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
structs::{Chunk, Header, Item, KeyPtr, RootItem, TreeItem},
|
structs::{Chunk, Header, Item, Key, KeyPtr, KnownObjectId, ObjectType, RootItem, TreeItem},
|
||||||
Error, Result, Volume, VolumeIo,
|
Btrfs, Error, Result, VolumeIo,
|
||||||
};
|
};
|
||||||
use alloc::{
|
use alloc::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
|
@ -161,7 +161,7 @@ impl BTreeNode {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Tree<'a, R: VolumeIo> {
|
pub struct Tree<'a, R: VolumeIo> {
|
||||||
// volume for reader and euperblock
|
// volume for reader and euperblock
|
||||||
volume: &'a Volume<R>,
|
volume: &'a Btrfs<R>,
|
||||||
// offset of the root node,
|
// offset of the root node,
|
||||||
root: BoxedNode<'a>,
|
root: BoxedNode<'a>,
|
||||||
}
|
}
|
||||||
|
@ -189,6 +189,58 @@ impl<'a> Node<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_key(self: &Rc<Self>, key: &Key) -> SearchResult<'a> {
|
||||||
|
match &self.inner {
|
||||||
|
BTreeNode::Internal(node) => {
|
||||||
|
for (i, child) in node.children.iter().enumerate() {
|
||||||
|
if key < &child.key {
|
||||||
|
return SearchResult::Edge(NodeHandle {
|
||||||
|
node: self.clone(),
|
||||||
|
idx: if i == 0 { 0 } else { i as u32 - 1 },
|
||||||
|
});
|
||||||
|
} else if key == &child.key {
|
||||||
|
return SearchResult::Edge(NodeHandle {
|
||||||
|
node: self.clone(),
|
||||||
|
idx: i as u32,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SearchResult::Edge(NodeHandle {
|
||||||
|
node: self.clone(),
|
||||||
|
idx: node.children.len() as u32 - 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
BTreeNode::Leaf(node) => {
|
||||||
|
for (i, child) in node.items.iter().enumerate() {
|
||||||
|
// if key < &child.key {
|
||||||
|
// return SearchResult::Leaf(NodeHandle {
|
||||||
|
// node: self.clone(),
|
||||||
|
// idx: if i == 0 { 0 } else { i as u32 - 1 },
|
||||||
|
// });
|
||||||
|
// } else
|
||||||
|
if key == &child.key {
|
||||||
|
return SearchResult::Leaf(NodeHandle {
|
||||||
|
node: self.clone(),
|
||||||
|
idx: i as u32,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log::debug!("key definitely not found!");
|
||||||
|
SearchResult::Edge(NodeHandle {
|
||||||
|
node: self.clone(),
|
||||||
|
idx: node.items.len() as u32 - 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum SearchResult<'a> {
|
||||||
|
Leaf(NodeHandle<'a>),
|
||||||
|
Edge(NodeHandle<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PartialEq for Node<'a> {
|
impl<'a> PartialEq for Node<'a> {
|
||||||
|
@ -330,7 +382,7 @@ impl<'a> PartialEq for RootOrEdge<'a> {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Range<'a, R: VolumeIo> {
|
pub struct Range<'a, R: VolumeIo> {
|
||||||
volume: &'a Volume<R>,
|
volume: &'a Btrfs<R>,
|
||||||
parents: Vec<NodeHandle<'a>>,
|
parents: Vec<NodeHandle<'a>>,
|
||||||
start: RootOrEdge<'a>,
|
start: RootOrEdge<'a>,
|
||||||
end: RootOrEdge<'a>,
|
end: RootOrEdge<'a>,
|
||||||
|
@ -345,7 +397,7 @@ impl<'a, R: VolumeIo> Iterator for Range<'a, R> {
|
||||||
self.init_start().expect("error");
|
self.init_start().expect("error");
|
||||||
match &self.start.node.inner {
|
match &self.start.node.inner {
|
||||||
BTreeNode::Internal(_) => None,
|
BTreeNode::Internal(_) => None,
|
||||||
BTreeNode::Leaf(leaf) => Some(self.start.into_handle()),
|
BTreeNode::Leaf(_) => Some(self.start.into_handle()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RootOrEdge::Edge(_) => self
|
RootOrEdge::Edge(_) => self
|
||||||
|
@ -361,7 +413,7 @@ impl<'a, R: VolumeIo> Iterator for Range<'a, R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R: VolumeIo> Range<'a, R> {
|
impl<'a, R: VolumeIo> Range<'a, R> {
|
||||||
pub fn new(volume: &'a Volume<R>, start: Rc<Node<'a>>, end: Rc<Node<'a>>) -> Self {
|
pub fn new(volume: &'a Btrfs<R>, start: Rc<Node<'a>>, end: Rc<Node<'a>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
volume,
|
volume,
|
||||||
parents: Default::default(),
|
parents: Default::default(),
|
||||||
|
@ -384,7 +436,7 @@ impl<'a, R: VolumeIo> Range<'a, R> {
|
||||||
loop {
|
loop {
|
||||||
match advance {
|
match advance {
|
||||||
NodeHandleAdvanceResult::Decend { parent, child_ptr } => {
|
NodeHandleAdvanceResult::Decend { parent, child_ptr } => {
|
||||||
let bytes = self.volume.read_keyptr(child_ptr)?;
|
let bytes = self.volume.read_keyptr(&child_ptr)?;
|
||||||
let child =
|
let child =
|
||||||
NodeHandle::start(Rc::new(Node::from_bytes(bytes)?));
|
NodeHandle::start(Rc::new(Node::from_bytes(bytes)?));
|
||||||
self.parents.push(parent);
|
self.parents.push(parent);
|
||||||
|
@ -424,7 +476,7 @@ impl<'a, R: VolumeIo> Range<'a, R> {
|
||||||
loop {
|
loop {
|
||||||
match advance {
|
match advance {
|
||||||
NodeHandleAdvanceResult::Decend { parent, child_ptr } => {
|
NodeHandleAdvanceResult::Decend { parent, child_ptr } => {
|
||||||
let bytes = self.volume.read_keyptr(child_ptr)?;
|
let bytes = self.volume.read_keyptr(&child_ptr)?;
|
||||||
let child = NodeHandle::start(Rc::new(Node::from_bytes(bytes)?));
|
let child = NodeHandle::start(Rc::new(Node::from_bytes(bytes)?));
|
||||||
self.parents.push(parent);
|
self.parents.push(parent);
|
||||||
self.start = RootOrEdge::Edge(child);
|
self.start = RootOrEdge::Edge(child);
|
||||||
|
@ -463,7 +515,7 @@ impl<'a, R: VolumeIo> Range<'a, R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, R: VolumeIo> Tree<'a, R> {
|
impl<'a, R: VolumeIo> Tree<'a, R> {
|
||||||
pub fn from_logical_offset(volume: &'a Volume<R>, logical: u64) -> Result<Self> {
|
pub fn from_logical_offset(volume: &'a Btrfs<R>, logical: u64) -> Result<Self> {
|
||||||
let physical = volume
|
let physical = volume
|
||||||
.range_from_logical(logical)
|
.range_from_logical(logical)
|
||||||
.ok_or(Error::InvalidOffset)?;
|
.ok_or(Error::InvalidOffset)?;
|
||||||
|
@ -474,6 +526,32 @@ impl<'a, R: VolumeIo> Tree<'a, R> {
|
||||||
Ok(Self { volume, root })
|
Ok(Self { volume, root })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_key(&self, key: Key) -> Result<Option<(Item, TreeItem)>> {
|
||||||
|
let mut node = self.root.clone();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let search = node.find_key(&key);
|
||||||
|
match search {
|
||||||
|
SearchResult::Leaf(a) => {
|
||||||
|
return a.parse_item();
|
||||||
|
}
|
||||||
|
SearchResult::Edge(edge) => match &edge.node.inner {
|
||||||
|
BTreeNode::Internal(internal) => {
|
||||||
|
let child_ptr = internal.children.get(edge.idx as usize).expect("adsf");
|
||||||
|
|
||||||
|
let bytes = self.volume.read_keyptr(child_ptr)?;
|
||||||
|
node = Rc::new(Node::from_bytes(bytes)?);
|
||||||
|
// recurse
|
||||||
|
}
|
||||||
|
BTreeNode::Leaf(_) => {
|
||||||
|
// leaf node returning and edge means key is not present
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn full_range(&self) -> Range<'a, R> {
|
pub fn full_range(&self) -> Range<'a, R> {
|
||||||
Range::new(self.volume, self.root.clone(), self.root.clone())
|
Range::new(self.volume, self.root.clone(), self.root.clone())
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ use std::{
|
||||||
|
|
||||||
use test_log::test;
|
use test_log::test;
|
||||||
|
|
||||||
use btrfs::{structs::*, tree::Tree, Volume};
|
use btrfs::{structs::*, tree::Tree, Btrfs};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn superblock() {
|
fn superblock() {
|
||||||
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
||||||
|
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let volume = Volume::new(reader).expect("volume");
|
let volume = Btrfs::new(reader).expect("volume");
|
||||||
let sb = volume.superblock();
|
let sb = volume.superblock();
|
||||||
|
|
||||||
println!("{sb:#?}");
|
println!("{sb:#?}");
|
||||||
|
@ -27,10 +27,10 @@ fn iter_sys_chunks() {
|
||||||
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
||||||
|
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let volume = Volume::new(reader).expect("volume");
|
let volume = Btrfs::new(reader).expect("volume");
|
||||||
let sb = volume.superblock();
|
let sb = volume.superblock();
|
||||||
|
|
||||||
let result = Volume::<BufReader<std::fs::File>>::bootstrap_chunk_tree(&sb);
|
let result = Btrfs::<BufReader<std::fs::File>>::bootstrap_chunk_tree(&sb);
|
||||||
|
|
||||||
println!("{result:#?}");
|
println!("{result:#?}");
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,10 @@ fn iter_root() {
|
||||||
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
let mut file = std::fs::File::open("btrfs.img").expect("btrfs image");
|
||||||
|
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut volume = Volume::new(reader).expect("volume");
|
let mut volume = Btrfs::new(reader).expect("volume");
|
||||||
let sb = volume.superblock();
|
let sb = volume.superblock();
|
||||||
|
|
||||||
volume.iter_roots();
|
println!("{:#?}", volume);
|
||||||
|
|
||||||
println!("{:#?}", volume.chunk_cache);
|
volume.find_default_subvol();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue