33// SPDX-License-Identifier: MPL-2.0
44
55//! BTRFS superblock handling
6+ //!
7+ //! This module provides functionality for reading and parsing BTRFS filesystem superblocks,
8+ //! which contain critical metadata about the filesystem including UUIDs and labels.
69
710use crate :: { Error , Kind , Superblock } ;
811use log;
912use std:: io:: { self , Read } ;
1013use uuid:: Uuid ;
1114use zerocopy:: * ;
1215
13- /// BTRFS superblock definition (as seen in the kernel)
16+ /// BTRFS superblock definition that matches the on-disk format used by the Linux kernel.
17+ ///
18+ /// The superblock contains critical filesystem metadata including:
19+ /// - Filesystem UUID and label
20+ /// - Size and usage information
21+ /// - Root tree locations
22+ /// - Compatibility flags
1423#[ derive( FromBytes , Debug ) ]
1524#[ repr( C ) ]
1625pub struct Btrfs {
17- csum : [ u8 ; 32 ] ,
18- fsid : [ u8 ; 16 ] ,
19- bytenr : U64 < LittleEndian > ,
20- flags : U64 < LittleEndian > ,
21- magic : U64 < LittleEndian > ,
22- generation : U64 < LittleEndian > ,
23- root : U64 < LittleEndian > ,
24- chunk_root : U64 < LittleEndian > ,
25- log_root : U64 < LittleEndian > ,
26- log_root_transid : U64 < LittleEndian > ,
27- total_bytes : U64 < LittleEndian > ,
28- bytes_used : U64 < LittleEndian > ,
29- root_dir_objectid : U64 < LittleEndian > ,
30- num_devices : U64 < LittleEndian > ,
31- sectorsize : U32 < LittleEndian > ,
32- nodesize : U32 < LittleEndian > ,
33- leafsize : U32 < LittleEndian > ,
34- stripesize : U32 < LittleEndian > ,
35- sys_chunk_array_size : U32 < LittleEndian > ,
36- chunk_root_generation : U64 < LittleEndian > ,
37- compat_flags : U64 < LittleEndian > ,
38- compat_ro_flags : U64 < LittleEndian > ,
39- incompat_flags : U64 < LittleEndian > ,
40- csum_type : U16 < LittleEndian > ,
41- root_level : u8 ,
42- chunk_root_level : u8 ,
43- log_root_level : u8 ,
44- dev_item : [ u8 ; 98 ] ,
45- label : [ u8 ; 256 ] ,
46- cache_generation : U64 < LittleEndian > ,
47- uuid_tree_generation : U64 < LittleEndian > ,
48- metadata_uuid : [ u8 ; 16 ] ,
49- nr_global_roots : U64 < LittleEndian > ,
50- reserved : [ u8 ; 32 ] ,
51- sys_chunk_array : [ u8 ; 2048 ] ,
52- root_backup : [ u8 ; 256 ] ,
26+ /// Checksum of the superblock data
27+ pub csum : [ u8 ; 32 ] ,
28+ /// Filesystem UUID
29+ pub fsid : [ u8 ; 16 ] ,
30+ /// Physical byte number where this copy of the superblock is located
31+ pub bytenr : U64 < LittleEndian > ,
32+ /// Superblock flags
33+ pub flags : U64 < LittleEndian > ,
34+ /// Magic number identifying this as a BTRFS superblock
35+ pub magic : U64 < LittleEndian > ,
36+ /// Transaction ID of the filesystem
37+ pub generation : U64 < LittleEndian > ,
38+ /// Logical address of the root tree root
39+ pub root : U64 < LittleEndian > ,
40+ /// Logical address of the chunk tree root
41+ pub chunk_root : U64 < LittleEndian > ,
42+ /// Logical address of the log tree root
43+ pub log_root : U64 < LittleEndian > ,
44+ /// Transaction ID of the log tree
45+ pub log_root_transid : U64 < LittleEndian > ,
46+ /// Total size of the filesystem in bytes
47+ pub total_bytes : U64 < LittleEndian > ,
48+ /// Number of bytes used
49+ pub bytes_used : U64 < LittleEndian > ,
50+ /// Object ID of the root directory
51+ pub root_dir_objectid : U64 < LittleEndian > ,
52+ /// Number of devices making up the filesystem
53+ pub num_devices : U64 < LittleEndian > ,
54+ /// Size of a sector in bytes
55+ pub sectorsize : U32 < LittleEndian > ,
56+ /// Size of nodes in the filesystem trees
57+ pub nodesize : U32 < LittleEndian > ,
58+ /// Size of leaf nodes in the filesystem trees
59+ pub leafsize : U32 < LittleEndian > ,
60+ /// Stripe size for the filesystem
61+ pub stripesize : U32 < LittleEndian > ,
62+ /// Size of the system chunk array
63+ pub sys_chunk_array_size : U32 < LittleEndian > ,
64+ /// Generation of the chunk tree
65+ pub chunk_root_generation : U64 < LittleEndian > ,
66+ /// Compatible feature flags
67+ pub compat_flags : U64 < LittleEndian > ,
68+ /// Compatible read-only feature flags
69+ pub compat_ro_flags : U64 < LittleEndian > ,
70+ /// Incompatible feature flags
71+ pub incompat_flags : U64 < LittleEndian > ,
72+ /// Checksum algorithm type
73+ pub csum_type : U16 < LittleEndian > ,
74+ /// Level of the root tree
75+ pub root_level : u8 ,
76+ /// Level of the chunk tree
77+ pub chunk_root_level : u8 ,
78+ /// Level of the log tree
79+ pub log_root_level : u8 ,
80+ /// Device information
81+ pub dev_item : [ u8 ; 98 ] ,
82+ /// Volume label
83+ pub label : [ u8 ; 256 ] ,
84+ /// Cache generation number
85+ pub cache_generation : U64 < LittleEndian > ,
86+ /// UUID tree generation
87+ pub uuid_tree_generation : U64 < LittleEndian > ,
88+ /// Metadata UUID for the filesystem
89+ pub metadata_uuid : [ u8 ; 16 ] ,
90+ /// Number of global root entries
91+ pub nr_global_roots : U64 < LittleEndian > ,
92+ /// Reserved for future use
93+ pub reserved : [ u8 ; 32 ] ,
94+ /// System chunk array data
95+ pub sys_chunk_array : [ u8 ; 2048 ] ,
96+ /// Backup copy of root tree info
97+ pub root_backup : [ u8 ; 256 ] ,
5398}
5499
55- // Superblock starts at 65536 for btrfs.
56- const START_POSITION : u64 = 0x10000 ;
100+ /// Offset where the BTRFS superblock starts (65536 bytes)
101+ pub const START_POSITION : u64 = 0x10000 ;
57102
58- // "_BHRfS_M"
59- const MAGIC : U64 < LittleEndian > = U64 :: new ( 0x4D5F53665248425F ) ;
103+ /// Magic number identifying a BTRFS superblock ( "_BHRfS_M")
104+ pub const MAGIC : U64 < LittleEndian > = U64 :: new ( 0x4D5F53665248425F ) ;
60105
61- /// Attempt to decode the Superblock from the given read stream
106+ /// Attempt to decode the BTRFS superblock from the given read stream.
107+ ///
108+ /// This will read past the initial superblock offset and validate the magic number
109+ /// before returning the parsed superblock structure.
62110pub fn from_reader < R : Read > ( reader : & mut R ) -> Result < Btrfs , Error > {
63111 // Drop unwanted bytes (Seek not possible with zstd streamed inputs)
64112 io:: copy ( & mut reader. by_ref ( ) . take ( START_POSITION ) , & mut io:: sink ( ) ) ?;
@@ -78,16 +126,17 @@ pub fn from_reader<R: Read>(reader: &mut R) -> Result<Btrfs, Error> {
78126}
79127
80128impl Superblock for Btrfs {
81- /// Return the encoded UUID for this superblock
129+ /// Return the encoded UUID for this superblock as a string
82130 fn uuid ( & self ) -> Result < String , Error > {
83131 Ok ( Uuid :: from_bytes ( self . fsid ) . hyphenated ( ) . to_string ( ) )
84132 }
85133
134+ /// Return the filesystem type
86135 fn kind ( & self ) -> Kind {
87136 super :: Kind :: Btrfs
88137 }
89138
90- /// We don't yet support labels here.
139+ /// Return the volume label as a string
91140 fn label ( & self ) -> Result < String , Error > {
92141 Ok ( std:: str:: from_utf8 ( & self . label ) ?. trim_end_matches ( '\0' ) . to_owned ( ) )
93142 }
0 commit comments