Skip to content

Commit 44d2fcc

Browse files
committed
btrfs: Add missing fields and label() support
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 86f9c39 commit 44d2fcc

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

crates/superblock/src/btrfs.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// SPDX-License-Identifier: MPL-2.0
44

55
//! BTRFS superblock handling
6-
//! TODO: Add full representation of the superblock to allow us to
7-
//! discover volumes and the root label.
86
97
use crate::{Error, Kind, Superblock};
108
use log;
@@ -13,9 +11,6 @@ use uuid::Uuid;
1311
use zerocopy::*;
1412

1513
/// BTRFS superblock definition (as seen in the kernel)
16-
/// This is a PARTIAL representation that matches only the
17-
/// first 72 bytes, verifies the magic, and permits extraction
18-
/// of the UUID
1914
#[derive(FromBytes, Debug)]
2015
#[repr(C)]
2116
pub struct Btrfs {
@@ -28,6 +23,33 @@ pub struct Btrfs {
2823
root: U64<LittleEndian>,
2924
chunk_root: U64<LittleEndian>,
3025
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],
3153
}
3254

3355
// Superblock starts at 65536 for btrfs.
@@ -46,7 +68,11 @@ pub fn from_reader<R: Read>(reader: &mut R) -> Result<Btrfs, Error> {
4668
if data.magic != MAGIC {
4769
Err(Error::InvalidMagic)
4870
} else {
49-
log::trace!("valid magic field: UUID={}", data.uuid()?);
71+
log::trace!(
72+
"valid magic field: UUID={}, [volume label: \"{}\"]",
73+
data.uuid()?,
74+
data.label()?
75+
);
5076
Ok(data)
5177
}
5278
}
@@ -63,7 +89,7 @@ impl Superblock for Btrfs {
6389

6490
/// We don't yet support labels here.
6591
fn label(&self) -> Result<String, Error> {
66-
Err(Error::UnsupportedFeature)
92+
Ok(std::str::from_utf8(&self.label)?.trim_end_matches('\0').to_owned())
6793
}
6894
}
6995

@@ -79,5 +105,6 @@ mod tests {
79105
let mut stream = zstd::stream::Decoder::new(&mut fi).expect("Unable to decode stream");
80106
let sb = from_reader(&mut stream).expect("Cannot parse superblock");
81107
assert_eq!(sb.uuid().unwrap(), "829d6a03-96a5-4749-9ea2-dbb6e59368b2");
108+
assert_eq!(sb.label().unwrap(), "blsforme testing");
82109
}
83110
}

0 commit comments

Comments
 (0)