Skip to content

Commit

Permalink
Add D-Bus method to get filesystem metadata
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 26, 2024
1 parent 2670b4c commit 700b83f
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/dbus_api/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ pub fn create_dbus_pool<'a>(
.add_m(pool_3_0::rename_method(&f))
.add_m(pool_3_3::grow_physical_device_method(&f))
.add_m(pool_3_7::get_metadata_method(&f))
.add_m(pool_3_7::get_fs_metadata_method(&f))
.add_p(pool_3_0::name_property(&f))
.add_p(pool_3_0::uuid_property(&f))
.add_p(pool_3_0::encrypted_property(&f))
Expand Down
13 changes: 12 additions & 1 deletion src/dbus_api/pool/pool_3_7/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use dbus_tree::{Access, EmitsChangedSignal, Factory, MTSync, Method, Property};
use crate::dbus_api::{
consts,
pool::pool_3_7::{
methods::{destroy_filesystems, metadata},
methods::{destroy_filesystems, fs_metadata, metadata},
props::get_pool_metadata_version,
},
types::TData,
Expand Down Expand Up @@ -47,3 +47,14 @@ pub fn metadata_version_property(
.emits_changed(EmitsChangedSignal::Const)
.on_get(get_pool_metadata_version)
}

pub fn get_fs_metadata_method(f: &Factory<MTSync<TData>, TData>) -> Method<MTSync<TData>, TData> {
f.method("FilesystemMetadata", (), fs_metadata)
// A string representing the pool's filesystem metadata in serialized
// JSON format.
//
// Rust representation: String
.out_arg(("results", "s"))
.out_arg(("return_code", "q"))
.out_arg(("return_string", "s"))
}
36 changes: 36 additions & 0 deletions src/dbus_api/pool/pool_3_7/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,39 @@ pub fn metadata(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
};
Ok(vec![msg])
}

pub fn fs_metadata(m: &MethodInfo<'_, MTSync<TData>, TData>) -> MethodResult {
let default_return = String::new();

let message: &Message = m.msg;

let return_message = message.method_return();

let dbus_context = m.tree.get_data();
let object_path = m.path.get_name();

let pool_path = m
.tree
.get(object_path)
.expect("implicit argument must be in tree");
let pool_uuid = typed_uuid!(
get_data!(pool_path; default_return; return_message).uuid;
Pool;
default_return;
return_message
);

let guard = get_pool!(dbus_context.engine; pool_uuid; default_return; return_message);
let (_, _, pool) = guard.as_tuple();

let result = pool.last_metadata();

let msg = match result {
Ok(v) => return_message.append3(v, DbusErrorEnum::OK as u16, OK_STRING.to_string()),
Err(err) => {
let (rc, rs) = engine_to_dbus_err_tuple(&err);
return_message.append3(default_return, rc, rs)
}
};
Ok(vec![msg])
}
5 changes: 4 additions & 1 deletion src/dbus_api/pool/pool_3_7/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ mod api;
mod methods;
mod props;

pub use api::{destroy_filesystems_method, get_metadata_method, metadata_version_property};
pub use api::{
destroy_filesystems_method, get_fs_metadata_method, get_metadata_method,
metadata_version_property,
};
3 changes: 3 additions & 0 deletions src/engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ pub trait Pool: Debug + Send + Sync {

/// Get the metadata version for a given pool.
fn metadata_version(&self) -> StratSigblockVersion;

/// Get the last written filesystem metadata.
fn last_fs_metadata(&self) -> StratisResult<String>;
}

pub type HandleEvents<P> = (
Expand Down
26 changes: 25 additions & 1 deletion src/engine/sim_engine/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ use serde_json::{Map, Value};
use devicemapper::{Bytes, Sectors};

use crate::{
engine::{types::FilesystemUuid, Filesystem},
engine::{
types::{FilesystemUuid, Name},
Filesystem,
},
stratis::{StratisError, StratisResult},
};

#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct FilesystemSave {
name: String,
uuid: FilesystemUuid,
size: Sectors,
created: u64,
fs_size_limit: Option<Sectors>,
origin: Option<FilesystemUuid>,
}

#[derive(Debug)]
pub struct SimFilesystem {
rand: u32,
Expand Down Expand Up @@ -73,6 +86,17 @@ impl SimFilesystem {
self.origin = None;
changed
}

pub fn record(&self, name: &Name, uuid: FilesystemUuid) -> FilesystemSave {
FilesystemSave {
name: name.to_owned(),
uuid,
size: self.size,
created: self.created.timestamp() as u64,
fs_size_limit: self.size_limit,
origin: self.origin,
}
}
}

impl Filesystem for SimFilesystem {
Expand Down
10 changes: 10 additions & 0 deletions src/engine/sim_engine/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,16 @@ impl Pool for SimPool {
fn metadata_version(&self) -> StratSigblockVersion {
StratSigblockVersion::V2
}

fn last_fs_metadata(&self) -> StratisResult<String> {
self.filesystems
.iter()
.map(|(name, uuid, fs)| {
serde_json::to_string(&fs.record(name, *uuid)).map_err(|e| e.into())
})
.collect::<StratisResult<Vec<_>>>()
.map(|v| v.join("\n"))
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions src/engine/strat_engine/pool/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,11 @@ impl Pool for AnyPool {
AnyPool::V2(p) => p.metadata_version(),
}
}

fn last_fs_metadata(&self) -> StratisResult<String> {
match self {
AnyPool::V1(p) => p.last_fs_metadata(),
AnyPool::V2(p) => p.last_fs_metadata(),
}
}
}
11 changes: 11 additions & 0 deletions src/engine/strat_engine/pool/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,17 @@ impl Pool for StratPool {
fn metadata_version(&self) -> StratSigblockVersion {
StratSigblockVersion::V1
}

fn last_fs_metadata(&self) -> StratisResult<String> {
self.thin_pool
.filesystems()
.iter()
.map(|(name, uuid, fs)| {
serde_json::to_string(&fs.record(name, *uuid)).map_err(|e| e.into())
})
.collect::<StratisResult<Vec<_>>>()
.map(|v| v.join("\n"))
}
}

pub struct StratPoolState {
Expand Down
11 changes: 11 additions & 0 deletions src/engine/strat_engine/pool/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,17 @@ impl Pool for StratPool {
fn metadata_version(&self) -> StratSigblockVersion {
StratSigblockVersion::V2
}

fn last_fs_metadata(&self) -> StratisResult<String> {
self.thin_pool
.filesystems()
.iter()
.map(|(name, uuid, fs)| {
serde_json::to_string(&fs.record(name, *uuid)).map_err(|e| e.into())
})
.collect::<StratisResult<Vec<_>>>()
.map(|v| v.join("\n"))
}
}

pub struct StratPoolState {
Expand Down

0 comments on commit 700b83f

Please sign in to comment.