Skip to content

Commit

Permalink
work on disk format
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespharaoh committed Jan 14, 2017
1 parent 285829c commit 6126012
Show file tree
Hide file tree
Showing 26 changed files with 1,426 additions and 407 deletions.
22 changes: 0 additions & 22 deletions src/diskformat/devitem.rs

This file was deleted.

188 changes: 188 additions & 0 deletions src/diskformat/item/chunk_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
use std::mem;
use std::slice;

use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsChunkItem <'a> {
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

pub struct BtrfsChunkItemSimple <'a> {
key: & 'a BtrfsKey,
data: & 'a BtrfsChunkItemData,
}

#[ repr (C, packed) ]
#[ derive (Copy, Clone, Debug) ]
pub struct BtrfsChunkItemData {
chunk_size: u64,
root_object_id: u64,
stripe_length: u64,
flags: u64,
optimal_io_alignment: u32,
optimal_io_width: u32,
minimal_io_size: u32,
num_stripes: u16,
sub_stripes: u16,
}

#[ repr (C, packed) ]
#[ derive (Copy, Clone, Debug) ]
pub struct BtrfsChunkItemStripeData {
device_id: u64,
offset: u64,
device_uuid: BtrfsUuid,
}

impl <'a> BtrfsChunkItem <'a> {

pub fn from_bytes (
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsChunkItem <'a>, String> {

// sanity check

if data_bytes.len () < mem::size_of::<BtrfsChunkItemData> () {

return Err (
format! (
"Must be at least 0x{:x} bytes",
mem::size_of::<BtrfsInodeItemData> ()));

}

// TODO check stripes

// create chunk item

Ok (
BtrfsChunkItem {
header: header,
data_bytes: data_bytes,
}
)

}

pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

pub fn object_id (& self) -> u64 {
self.header.object_id ()
}

pub fn key (& self) -> BtrfsKey {
self.header.key ()
}

pub fn data (& self) -> & BtrfsChunkItemData {

unsafe {
& * (
self.data_bytes.as_ptr ()
as * const BtrfsChunkItemData
)
}

}

pub fn stripes (& self) -> & [BtrfsChunkItemStripeData] {
self.data ().stripes ()
}

}

impl <'a> BtrfsChunkItemSimple <'a> {

pub fn new (
key: & 'a BtrfsKey,
data: & 'a BtrfsChunkItemData,
) -> Result <BtrfsChunkItemSimple <'a>, String> {

if key.item_type () != BTRFS_CHUNK_ITEM_TYPE {

return Err (
format! (
"Invalid key type for chunk item: 0x{:02x}",
key.item_type ()));

}

Ok (
BtrfsChunkItemSimple {
key: key,
data: data,
}
)

}

pub fn key (& self) -> & BtrfsKey {
self.key
}

pub fn data (& self) -> & BtrfsChunkItemData {
self.data
}

pub fn stripes (& self) -> & [BtrfsChunkItemStripeData] {
self.data.stripes ()
}

}

impl BtrfsChunkItemData {

pub fn chunk_size (& self) -> u64 {
self.chunk_size
}

pub fn stripes (& self) -> & [BtrfsChunkItemStripeData] {

unsafe {
slice::from_raw_parts (
(
self
as * const BtrfsChunkItemData
as * const u8
).offset (
mem::size_of::<BtrfsChunkItemData> () as isize,
) as * const BtrfsChunkItemStripeData,
self.num_stripes as usize,
)
}

}

pub fn num_stripes (& self) -> u16 {
self.num_stripes
}

pub fn sub_stripes (& self) -> u16 {
self.sub_stripes
}

}

impl BtrfsChunkItemStripeData {

pub fn device_id (& self) -> u64 {
self.device_id
}

pub fn offset (& self) -> u64 {
self.offset
}

pub fn device_uuid (& self) -> BtrfsUuid {
self.device_uuid
}

}


// ex: noet ts=4 filetype=rust
30 changes: 30 additions & 0 deletions src/diskformat/item/dev_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use diskformat::*;

#[ repr (C, packed) ]
#[ derive (Copy, Clone) ]
pub struct BtrfsDevItem {
device_id: u64,
num_bytes: u64,
num_bytes_used: u64,
optimal_io_align: u32,
optimal_io_width: u32,
minimal_io_size: u32,
device_type: u64,
generation: u64,
start_offset: u64,
dev_group: u32,
seek_speed: u8,
bandwidth: u8,
device_uuid: BtrfsUuid,
fs_uuid: BtrfsUuid,
}

impl BtrfsDevItem {

pub fn device_id (& self) -> u64 {
self.device_id
}

}

// ex: noet ts=4 filetype=rust
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, PartialEq) ]
pub struct BtrfsDirIndex <'a> {
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

Expand All @@ -21,7 +21,7 @@ pub struct BtrfsDirIndexData {
impl <'a> BtrfsDirIndex <'a> {

pub fn from_bytes (
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsDirIndex <'a>, String> {

Expand Down Expand Up @@ -66,7 +66,7 @@ impl <'a> BtrfsDirIndex <'a> {

}

pub fn header (& self) -> & BtrfsItemHeader {
pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

Expand Down
6 changes: 3 additions & 3 deletions src/diskformat/diritem.rs → src/diskformat/item/dir_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsDirItem <'a> {
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

Expand All @@ -21,7 +21,7 @@ pub struct BtrfsDirItemData {
impl <'a> BtrfsDirItem <'a> {

pub fn from_bytes (
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsDirItem <'a>, String> {

Expand Down Expand Up @@ -66,7 +66,7 @@ impl <'a> BtrfsDirItem <'a> {

}

pub fn header (& self) -> & BtrfsItemHeader {
pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsExtentData <'a> {
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

Expand All @@ -36,7 +36,7 @@ pub struct BtrfsExtentDataData {
impl <'a> BtrfsExtentData <'a> {

pub fn from_bytes (
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsExtentData <'a>, String> {

Expand Down Expand Up @@ -84,7 +84,7 @@ impl <'a> BtrfsExtentData <'a> {

}

pub fn header (& self) -> & BtrfsItemHeader {
pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, PartialEq) ]
pub struct BtrfsExtentItem <'a> {
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

Expand All @@ -21,7 +21,7 @@ pub struct BtrfsExtentItemData {
impl <'a> BtrfsExtentItem <'a> {

pub fn from_bytes (
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsExtentItem <'a>, String> {

Expand Down Expand Up @@ -50,7 +50,7 @@ impl <'a> BtrfsExtentItem <'a> {

}

pub fn header (& self) -> & BtrfsItemHeader {
pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use diskformat::*;

#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsInodeItem <'a> {
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

Expand Down Expand Up @@ -33,7 +33,7 @@ pub struct BtrfsInodeItemData {
impl <'a> BtrfsInodeItem <'a> {

pub fn from_bytes (
header: & 'a BtrfsItemHeader,
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsInodeItem <'a>, String> {

Expand All @@ -59,7 +59,7 @@ impl <'a> BtrfsInodeItem <'a> {

}

pub fn header (& self) -> & BtrfsItemHeader {
pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

Expand Down
Loading

0 comments on commit 6126012

Please sign in to comment.