From 44d2fccc662b686023c9fe540f3cd2e1937a35eb Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Sun, 19 Jan 2025 16:12:58 +0000 Subject: [PATCH] btrfs: Add missing fields and `label()` support Signed-off-by: Ikey Doherty --- crates/superblock/src/btrfs.rs | 41 ++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/superblock/src/btrfs.rs b/crates/superblock/src/btrfs.rs index 6b6c821..a608a54 100644 --- a/crates/superblock/src/btrfs.rs +++ b/crates/superblock/src/btrfs.rs @@ -3,8 +3,6 @@ // SPDX-License-Identifier: MPL-2.0 //! BTRFS superblock handling -//! TODO: Add full representation of the superblock to allow us to -//! discover volumes and the root label. use crate::{Error, Kind, Superblock}; use log; @@ -13,9 +11,6 @@ use uuid::Uuid; use zerocopy::*; /// BTRFS superblock definition (as seen in the kernel) -/// This is a PARTIAL representation that matches only the -/// first 72 bytes, verifies the magic, and permits extraction -/// of the UUID #[derive(FromBytes, Debug)] #[repr(C)] pub struct Btrfs { @@ -28,6 +23,33 @@ pub struct Btrfs { root: U64, chunk_root: U64, log_root: U64, + log_root_transid: U64, + total_bytes: U64, + bytes_used: U64, + root_dir_objectid: U64, + num_devices: U64, + sectorsize: U32, + nodesize: U32, + leafsize: U32, + stripesize: U32, + sys_chunk_array_size: U32, + chunk_root_generation: U64, + compat_flags: U64, + compat_ro_flags: U64, + incompat_flags: U64, + csum_type: U16, + root_level: u8, + chunk_root_level: u8, + log_root_level: u8, + dev_item: [u8; 98], + label: [u8; 256], + cache_generation: U64, + uuid_tree_generation: U64, + metadata_uuid: [u8; 16], + nr_global_roots: U64, + reserved: [u8; 32], + sys_chunk_array: [u8; 2048], + root_backup: [u8; 256], } // Superblock starts at 65536 for btrfs. @@ -46,7 +68,11 @@ pub fn from_reader(reader: &mut R) -> Result { if data.magic != MAGIC { Err(Error::InvalidMagic) } else { - log::trace!("valid magic field: UUID={}", data.uuid()?); + log::trace!( + "valid magic field: UUID={}, [volume label: \"{}\"]", + data.uuid()?, + data.label()? + ); Ok(data) } } @@ -63,7 +89,7 @@ impl Superblock for Btrfs { /// We don't yet support labels here. fn label(&self) -> Result { - Err(Error::UnsupportedFeature) + Ok(std::str::from_utf8(&self.label)?.trim_end_matches('\0').to_owned()) } } @@ -79,5 +105,6 @@ mod tests { let mut stream = zstd::stream::Decoder::new(&mut fi).expect("Unable to decode stream"); let sb = from_reader(&mut stream).expect("Cannot parse superblock"); assert_eq!(sb.uuid().unwrap(), "829d6a03-96a5-4749-9ea2-dbb6e59368b2"); + assert_eq!(sb.label().unwrap(), "blsforme testing"); } }