Skip to content

Commit b965010

Browse files
authored
Migrate to latest versions of dependencies (#9)
1 parent d0cd70f commit b965010

22 files changed

+618
-270
lines changed

.github/workflows/rust-build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ jobs:
3939

4040
- name: Check formatting
4141
run: cargo fmt --check
42+
43+
- name: Run lints
44+
run: cargo clippy -- -D clippy::all

Cargo.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
name = "btrfs-diskformat"
33
version = "0.4.0"
44
authors = ["Christopher Tam <[email protected]>"]
5-
edition = "2021"
5+
edition = "2024"
66
description = "An implementation of the BTRFS disk format."
77
readme = "README.md"
88
repository = "https://github.com/GodTamIt/btrfs-diskformat"
99
license = "BSD-2-Clause"
1010
keywords = ["btrfs", "filesystem", "diskformat"]
1111
categories = ["filesystem", "no-std"]
1212

13+
[features]
14+
default = []
15+
alloc = ["zerocopy/alloc"]
16+
std = ["strum/std", "zerocopy/std"]
17+
1318
[dependencies]
14-
byteorder = { version = "^1.4.3", default-features = false }
15-
bitflags = "^1.3"
16-
num_enum = { version = "^0.5", default-features = false }
19+
bitflags = "^2.9"
20+
num_enum = { version = "^0.7", default-features = false }
1721
static_assertions = "^1.1.0"
18-
strum = { version = "^0.23", features = ["derive"], default-features = false }
19-
zerocopy = "^0.4.0"
22+
strum = { version = "^0.27", features = ["derive"], default-features = false }
23+
zerocopy = { version = "^0.8", default-features = false }
24+
zerocopy-derive = "^0.8"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ Clean-room implementation of the [btrfs] disk format in Rust.
1313

1414
See the [LICENSE-BSD](LICENSE-BSD) file in this repository for more information.
1515

16+
## Cargo Features
17+
18+
- `alloc`: Enables allocation and the `alloc` feature in `zerocopy`.
19+
- `std`: By default, the crate is `no_std`. This enables `std` features from
20+
the `zerocopy` and `strum` dependencies.
21+
1622
## Contributing
1723

1824
Because this codebase is developed without knowledge of the Linux btrfs source code and is released under a more permissive license(s) than GPLv2, development is heavily dependent on information released on the [btrfs wiki] and reverse engineering the effects of operations made by `btrfs-progs` and other utilities. As a result, contributions to this codebase must strictly follow the same siloed approach.
1925

2026
[btrfs]: https://btrfs.wiki.kernel.org/index.php/Main_Page
21-
[btrfs wiki]: https://btrfs.wiki.kernel.org/index.php/Main_Page
27+
[btrfs wiki]: https://btrfs.wiki.kernel.org/index.php/Main_Page

src/aliases.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub type btrfs_dev_extent = DevExtent;
4444
// extent
4545
pub type btrfs_block_group_item = BlockGroupItem;
4646
pub type btrfs_extent_data_ref = ExtentDataRef;
47-
pub type btrfs_extent_inline_ref = ExtentInlineRef;
47+
pub type btrfs_extent_inline_ref = ExtentInlineRefHeader;
4848
pub type btrfs_shared_data_ref = SharedDataRef;
4949

5050
pub const BTRFS_BLOCK_GROUP_DATA: u64 = AllocationType::DATA.bits();

src/chunk/chunk.rs

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use {
2-
crate::Stripe,
3-
byteorder::LE,
4-
static_assertions::const_assert_eq,
5-
zerocopy::{AsBytes, FromBytes, Unaligned, U16, U32, U64},
1+
use crate::Stripe;
2+
use static_assertions::const_assert_eq;
3+
use zerocopy::{
4+
CastError, FromBytes as _,
5+
little_endian::{U16 as U16LE, U32 as U32LE, U64 as U64LE},
66
};
7+
use zerocopy_derive::*;
78

89
/// This structure contains the mapping from a virtualized usable byte range within the backing
910
/// storage to a set of one or more stripes on individual backing devices. In addition to the
@@ -13,40 +14,107 @@ use {
1314
/// many struct [`Stripe`] structures as specified in the [`num_stripes`] and [`sub_stripes`]
1415
/// fields.
1516
///
17+
/// For the dynamically-sized version of this type with the stripes following, see [`ChunkDynamic`].
18+
///
1619
/// [`Stripe`]: crate::Stripe
1720
/// [`num_stripes`]: Chunk::num_stripes
1821
/// [`sub_stripes`]: Chunk::sub_stripes
19-
#[derive(Copy, Clone, Debug, AsBytes, FromBytes, Unaligned)]
22+
#[derive(Copy, Clone, Debug, Hash, IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
2023
#[repr(C, packed)]
2124
pub struct Chunk {
2225
/// The size of this chunk, in bytes.
23-
pub length: U64<LE>,
26+
pub length: U64LE,
27+
28+
/// The object ID of the root referencing this chunk. This is always the ID of an extent root.
29+
pub owner: U64LE,
30+
31+
/// The replication stripe length.
32+
pub stripe_len: U64LE,
33+
34+
/// Flags indicating allocation type and replication policy.
35+
pub chunk_type: U64LE,
36+
37+
/// The optimal I/O alignment for this chunk.
38+
pub io_align: U32LE,
39+
40+
/// The optimal I/O width for this chunk.
41+
pub io_width: U32LE,
42+
43+
/// The minimal I/O size for this chunk.
44+
pub sector_size: U32LE,
45+
46+
/// The number of replication stripes.
47+
pub num_stripes: U16LE,
48+
49+
/// The number of sub-stripes. This is only used for RAID-10.
50+
///
51+
/// This is 2 for RAID-10, and 1 for all other chunk types.
52+
pub sub_stripes: U16LE,
53+
}
54+
const_assert_eq!(core::mem::size_of::<Chunk>(), 48);
55+
56+
impl Chunk {
57+
/// A convenience method for converting a [`Chunk`] into a [`ChunkDynamic`].
58+
///
59+
/// For a safe version, use [`ChunkDynamic::ref_from_prefix_with_elems`].
60+
///
61+
/// # Safety
62+
/// This function is unsafe because it assumes that the bytes following the `Chunk` structure
63+
/// are valid and contain the expected number of `Stripe` structures as specified by
64+
/// `num_stripes`. If this assumption is violated, it may lead to undefined behavior.
65+
pub unsafe fn into_dynamic(
66+
&self,
67+
num_stripes: usize,
68+
) -> Result<&ChunkDynamic, CastError<&[u8], ChunkDynamic>> {
69+
// This is simple arithmetic, since this is a packed structure.
70+
let expected_size =
71+
core::mem::size_of::<Chunk>() + num_stripes * core::mem::size_of::<Stripe>();
72+
73+
// Safety: We assume the bytes for `num_stripes` after the `Chunk` structure are valid, as
74+
// part of the contract of this function.
75+
let bytes = unsafe {
76+
core::slice::from_raw_parts(self as *const Chunk as *const u8, expected_size)
77+
};
78+
79+
ChunkDynamic::ref_from_prefix_with_elems(bytes, num_stripes).map(|(chunk, _)| chunk)
80+
}
81+
}
82+
83+
/// This structure contains the mapping from a virtualized usable byte range within the backing
84+
/// storage to a set of one or more stripes on individual backing devices. In addition to the
85+
/// mapping, hints on optimal I/O parameters for this chunk. It is associated with `CHUNK_ITEM`.
86+
///
87+
/// For the constant-sized version of the type, see [`Chunk`].`
88+
#[derive(IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
89+
#[repr(C, packed)]
90+
pub struct ChunkDynamic {
91+
/// The size of this chunk, in bytes.
92+
pub length: U64LE,
2493

2594
/// The object ID of the root referencing this chunk. This is always the ID of an extent root.
26-
pub owner: U64<LE>,
95+
pub owner: U64LE,
2796

2897
/// The replication stripe length.
29-
pub stripe_len: U64<LE>,
98+
pub stripe_len: U64LE,
3099

31100
/// Flags indicating allocation type and replication policy.
32-
pub r#type: U64<LE>,
101+
pub chunk_type: U64LE,
33102

34103
/// The optimal I/O alignment for this chunk.
35-
pub io_align: U32<LE>,
104+
pub io_align: U32LE,
36105

37106
/// The optimal I/O width for this chunk.
38-
pub io_width: U32<LE>,
107+
pub io_width: U32LE,
39108

40109
/// The minimal I/O size for this chunk.
41-
pub sector_size: U32<LE>,
110+
pub sector_size: U32LE,
42111

43112
/// The number of replication stripes.
44-
pub num_stripes: U16<LE>,
113+
pub num_stripes: U16LE,
45114

46115
/// The number of sub-stripes. This is only used for RAID-10.
47-
pub sub_stripes: U16<LE>,
116+
pub sub_stripes: U16LE,
48117

49118
/// The first of one or more stripes that map to device extents.
50-
pub stripe: Stripe,
119+
pub stripe: [Stripe],
51120
}
52-
const_assert_eq!(core::mem::size_of::<Chunk>(), 80);

src/chunk/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(clippy::module_inception)]
12
mod chunk;
23
mod stripe;
34

src/chunk/stripe.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
use {
2-
crate::UuidBytes,
3-
byteorder::LE,
4-
static_assertions::const_assert_eq,
5-
zerocopy::{AsBytes, FromBytes, Unaligned, U64},
6-
};
1+
use crate::UuidBytes;
2+
use static_assertions::const_assert_eq;
3+
use zerocopy::little_endian::U64 as U64LE;
4+
use zerocopy_derive::*;
75

86
/// This structure is used to define the backing device storage that compose a
97
/// [`Chunk`].
108
///
119
/// [`Chunk`]: crate::Chunk
12-
#[derive(Copy, Clone, Debug, Hash, PartialEq, AsBytes, FromBytes, Unaligned)]
10+
#[derive(
11+
Copy, Clone, Debug, Hash, PartialEq, IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable,
12+
)]
1313
#[repr(C, packed)]
1414
pub struct Stripe {
1515
/// The ID of the device that contains this stripe.
16-
pub devid: U64<LE>,
16+
pub devid: U64LE,
1717

1818
/// Location of the start of the stripe, in bytes.
1919
///
2020
/// The length is determined by the `stripe_len` field of the associated
2121
/// `Chunk`.
22-
pub offset: U64<LE>,
22+
pub offset: U64LE,
2323

2424
/// UUID of the device that contains this stripe.
2525
///

src/core/dev_item.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
use {
2-
crate::UuidBytes,
3-
byteorder::LE,
4-
static_assertions::const_assert_eq,
5-
zerocopy::{AsBytes, FromBytes, Unaligned, U32, U64},
6-
};
1+
use crate::UuidBytes;
2+
use static_assertions::const_assert_eq;
3+
use zerocopy::little_endian::{U32 as U32LE, U64 as U64LE};
4+
use zerocopy_derive::*;
75

86
/// Represents a complete block device.
9-
#[derive(Copy, Clone, Debug, AsBytes, FromBytes, Unaligned)]
7+
#[derive(Copy, Clone, Debug, Hash, IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
108
#[repr(C, packed)]
119
pub struct DevItem {
1210
/// The internal btrfs device ID.
1311
///
1412
/// This should match the devid found in the filesystem's list of devices.
15-
pub devid: U64<LE>,
13+
pub devid: U64LE,
1614

1715
/// The size of the device.
18-
pub total_bytes: U64<LE>,
16+
pub total_bytes: U64LE,
1917

2018
/// The bytes in use by the filesystem on the device.
21-
pub bytes_used: U64<LE>,
19+
pub bytes_used: U64LE,
2220

2321
/// The optimal I/O alignment for this device.
24-
pub io_align: U32<LE>,
22+
pub io_align: U32LE,
2523

2624
/// The optimal I/O width for this device.
27-
pub io_width: U32<LE>,
25+
pub io_width: U32LE,
2826

2927
/// The minimum I/O size for this device.
30-
pub sector_size: U32<LE>,
28+
pub sector_size: U32LE,
3129

3230
/// The type and info for this device.
33-
pub r#type: U64<LE>,
31+
pub dev_type: U64LE,
3432

3533
/// The expected generation for this device.
36-
pub generation: U64<LE>,
34+
pub generation: U64LE,
3735

3836
/// The starting byte of this partition on the device, to allow for stripe
3937
/// alignment.
40-
pub start_offset: U64<LE>,
38+
pub start_offset: U64LE,
4139

4240
/// Grouping information for allocation decisions.
43-
pub dev_group: U32<LE>,
41+
pub dev_group: U32LE,
4442

4543
/// The seek speed of the device on a scale from 0 to 100, where 100 is the
4644
/// fastest.

src/core/inode_item.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,59 @@
1-
use {
2-
crate::Time,
3-
bitflags::bitflags,
4-
byteorder::LE,
5-
static_assertions::const_assert_eq,
6-
zerocopy::{AsBytes, FromBytes, Unaligned, U32, U64},
7-
};
1+
use crate::Time;
2+
use bitflags::bitflags;
3+
use static_assertions::const_assert_eq;
4+
use zerocopy::little_endian::{U32 as U32LE, U64 as U64LE};
5+
use zerocopy_derive::*;
86

97
/// Contains traditional inode data and attributes.
10-
#[derive(Copy, Clone, Debug, AsBytes, FromBytes, Unaligned)]
8+
#[derive(Copy, Clone, Debug, Hash, IntoBytes, FromBytes, Unaligned, KnownLayout, Immutable)]
119
#[repr(C, packed)]
1210
pub struct InodeItem {
1311
// FIXME: add documentation!
14-
pub generation: U64<LE>,
12+
pub generation: U64LE,
1513

1614
// FIXME: add documentation!
17-
pub transid: U64<LE>,
15+
pub transid: U64LE,
1816

1917
/// The size of a file, in bytes.
20-
pub size: U64<LE>,
18+
pub size: U64LE,
2119

2220
/// The size allocated to the file, in bytes.
2321
///
2422
/// This is equal to the sum of all of the extent data for the inode.
2523
/// This is 0 for directories.
26-
pub nbytes: U64<LE>,
24+
pub nbytes: U64LE,
2725

2826
/// This contains the byte offset of a block group when structure is a free space inode.
2927
///
3028
/// This value is unused for normal inodes.
31-
pub block_group: U64<LE>,
29+
pub block_group: U64LE,
3230

3331
/// Count of inode references for the inode.
3432
///
3533
/// When used outside of a file tree, this value is 1.
36-
pub nlink: U32<LE>,
34+
pub nlink: U32LE,
3735

3836
/// The user ID of the owner in Unix.
39-
pub uid: U32<LE>,
37+
pub uid: U32LE,
4038

4139
/// The group ID of the group owner in Unix.
42-
pub gid: U32<LE>,
40+
pub gid: U32LE,
4341

4442
/// The Unix protection mode.
45-
pub mode: U32<LE>,
43+
pub mode: U32LE,
4644

4745
/// The device identifier (if a special file).
48-
pub rdev: U64<LE>,
46+
pub rdev: U64LE,
4947

5048
/// Flags for the inode. See [InodeFlags] for values.
51-
pub flags: U64<LE>,
49+
pub flags: U64LE,
5250

5351
/// A sequence number used for compatibility with NFS.
5452
///
5553
/// This value is initialized to 0 and incremented each time [mtime] is updated.
5654
///
5755
/// [mtime]: InodeItem::mtime
58-
pub sequence: U64<LE>,
56+
pub sequence: U64LE,
5957

6058
pub _unused: [u64; 4],
6159

0 commit comments

Comments
 (0)