btrfs_disk/lib.rs
1//! # Parsing and serializing of btrfs on-disk data structures
2//!
3//! This crate reads and parses btrfs on-disk structures directly from block
4//! devices or image files, without going through the kernel. Unlike
5//! `btrfs-uapi` (which wraps Linux-only ioctls), this crate is
6//! platform-independent: any system that can read raw bytes from a block
7//! device or image file can use it.
8//!
9//! ## Filesystem layout
10//!
11//! A btrfs filesystem is organized as a forest of copy-on-write B-trees stored
12//! on one or more block devices. Every tree block is `nodesize` bytes
13//! (typically 16 KiB). The filesystem uses logical addresses internally; a
14//! chunk tree maps logical address ranges to physical device offsets.
15//!
16//! **Superblock.** The entry point into the filesystem. A fixed-size 4 KiB
17//! structure written at known byte offsets on each device (primary at 64 KiB,
18//! mirrors at 64 MiB and 256 GiB). It stores the filesystem UUID, feature
19//! flags, sizes, the checksum algorithm, and root pointers for bootstrapping
20//! the tree hierarchy. It also embeds a copy of the system chunk array (enough
21//! chunk mappings to locate the chunk tree itself). See [`superblock`].
22//!
23//! **Trees.** Each tree is a B-tree whose internal nodes contain key pointers
24//! ([`tree::KeyPtr`]) to child blocks and whose leaves contain items
25//! ([`tree::Item`]) with typed data payloads. Every item is addressed by a
26//! three-part key: `(objectid, type, offset)` ([`tree::DiskKey`]). The major
27//! trees are:
28//!
29//! - Root tree (objectid 1): contains [`items::RootItem`] entries that point
30//! to the root block of every other tree.
31//! - Chunk tree (objectid 3): maps logical address ranges to physical device
32//! stripes ([`items::ChunkItem`]).
33//! - FS tree (objectid 5, plus one per subvolume/snapshot): holds the actual
34//! filesystem content: inodes ([`items::InodeItem`]), directory entries
35//! ([`items::DirItem`]), file extents ([`items::FileExtentItem`]), and
36//! extended attributes.
37//! - Extent tree (objectid 2): tracks space allocation and backreferences
38//! ([`items::ExtentItem`], [`items::BlockGroupItem`]).
39//! - Device tree (objectid 4): per-device extent allocation
40//! ([`items::DeviceExtent`]).
41//! - Checksum tree (objectid 7): per-block data checksums.
42//! - Quota tree (objectid 8): quota group accounting
43//! ([`items::QgroupInfo`], [`items::QgroupLimit`]).
44//! - UUID tree (objectid 9): fast subvolume UUID lookups.
45//! - Free space tree (objectid 10): free space tracking
46//! ([`items::FreeSpaceInfo`]).
47//! - Block group tree (objectid 11): block group items, separated from the
48//! extent tree for faster mount.
49//!
50//! ## Reading a filesystem
51//!
52//! Open a block device or image file and bootstrap the tree hierarchy with
53//! [`reader::filesystem_open`]. This reads the superblock, seeds the chunk
54//! cache from the `sys_chunk_array`, reads the full chunk tree, then walks the
55//! root tree to discover all tree roots. The returned
56//! [`reader::OpenFilesystem`] gives you a [`reader::BlockReader`] (for reading
57//! tree blocks by logical address) and a map of tree roots.
58//!
59//! To walk a tree, use [`reader::tree_walk`] with a visitor callback, or read
60//! individual blocks with [`reader::BlockReader::read_tree_block`] and match
61//! on [`tree::TreeBlock`]. Parse leaf item payloads with
62//! [`items::parse_item_payload`] or the individual struct `parse` methods.
63
64#![warn(clippy::pedantic)]
65#![allow(clippy::module_name_repetitions)]
66// Test code uses literal byte buffers and small cast conversions that
67// pedantic clippy flags but that are intentional in unit tests.
68#![cfg_attr(
69 test,
70 allow(
71 clippy::cast_lossless,
72 clippy::cast_possible_truncation,
73 clippy::cast_possible_wrap,
74 clippy::cast_sign_loss,
75 clippy::identity_op,
76 clippy::match_wildcard_for_single_variants,
77 clippy::semicolon_if_nothing_returned,
78 clippy::unreadable_literal,
79 )
80)]
81
82// Note: missing_docs is not enabled as a warning because bitflags! generates
83// undocumentable `const _ = !0` associated constants. All public items are
84// documented — this was verified manually.
85
86pub mod chunk;
87pub mod items;
88pub mod raid56;
89#[allow(missing_docs)]
90pub mod raw;
91pub mod reader;
92pub mod superblock;
93pub mod tree;
94pub mod util;