1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! This module contains the `FsInspect` trait which filesystems can implement in order to expose
//! Inspect metrics in a standardized hierarchy. Once `FsInspect` has been implemented, a
//! filesystem can attach itself to a root node via `FsInspectTree::new`.
//!
//! A filesystem's inspect tree can be tested via `fs_test` by enabling the `supports_inspect`
//! option. This will validate that the inspect tree hierarchy is consistent and that basic
//! information is reported correctly. See `src/storage/fs_test/inspect.cc` for details.

use {
    async_trait::async_trait,
    fuchsia_inspect::{LazyNode, Node},
    futures::FutureExt,
    std::{
        collections::hash_map::HashMap,
        sync::{Arc, Mutex, Weak},
    },
};

const INFO_NODE_NAME: &'static str = "fs.info";
const USAGE_NODE_NAME: &'static str = "fs.usage";
const VOLUMES_NODE_NAME: &'static str = "fs.volumes";

/// Trait that Rust filesystems should implement to expose required Inspect data.
///
/// Once implemented, a filesystem can attach the Inspect data to a given root node by calling
/// `FsInspectTree::new` which will return ownership of the attached nodes/properties.
#[async_trait]
pub trait FsInspect {
    fn get_info_data(&self) -> InfoData;
    async fn get_usage_data(&self) -> UsageData;
}

/// Trait that Rust filesystems which are multi-volume should implement for each volume.
#[async_trait]
pub trait FsInspectVolume {
    async fn get_volume_data(&self) -> VolumeData;
}

/// Maintains ownership of the various inspect nodes/properties. Will be removed from the root node
/// they were attached to when dropped.
pub struct FsInspectTree {
    _info: LazyNode,
    _usage: LazyNode,
    _volumes: LazyNode,
    volumes_tracker: Arc<Mutex<HashMap<String, Weak<dyn FsInspectVolume + Send + Sync + 'static>>>>,
}

impl FsInspectTree {
    /// Attaches Inspect nodes following a standard hierarchy, returning ownership of the newly
    /// created LazyNodes.
    pub fn new(fs: Weak<dyn FsInspect + Send + Sync + 'static>, root: &Node) -> FsInspectTree {
        let fs_clone = fs.clone();
        let info_node = root.create_lazy_child(INFO_NODE_NAME, move || {
            let fs_clone = fs_clone.clone();
            async move {
                let inspector = fuchsia_inspect::Inspector::default();
                if let Some(fs) = fs_clone.upgrade() {
                    fs.get_info_data().record_into(inspector.root());
                }
                Ok(inspector)
            }
            .boxed()
        });

        let fs_clone = fs.clone();
        let usage_node = root.create_lazy_child(USAGE_NODE_NAME, move || {
            let fs_clone = fs_clone.clone();
            async move {
                let inspector = fuchsia_inspect::Inspector::default();
                if let Some(fs) = fs_clone.upgrade() {
                    fs.get_usage_data().await.record_into(inspector.root());
                }
                Ok(inspector)
            }
            .boxed()
        });

        let volumes_tracker = Arc::new(Mutex::new(HashMap::<
            String,
            Weak<dyn FsInspectVolume + Send + Sync + 'static>,
        >::new()));
        let tracker_weak = Arc::downgrade(&volumes_tracker);
        let volumes_node = root.create_lazy_child(VOLUMES_NODE_NAME, move || {
            let tracker_ref = tracker_weak.clone();
            async move {
                let inspector = fuchsia_inspect::Inspector::default();
                let root = inspector.root();
                let tracker = match tracker_ref.upgrade() {
                    Some(tracker) => tracker,
                    // This probably shouldn't happen, but if it does then it would be during a
                    // shutdown race, so just return empty.
                    None => return Ok(inspector),
                };
                let volumes = {
                    let tracker = tracker.lock().unwrap();
                    let mut volumes = Vec::with_capacity(tracker.len());
                    for (name, volume) in tracker.iter() {
                        volumes.push((name.clone(), volume.clone()));
                    }
                    volumes
                };
                for (name, volume_weak) in volumes {
                    let volume = match volume_weak.upgrade() {
                        Some(v) => v,
                        None => continue,
                    };
                    let child = root.create_child(name.clone());
                    volume.get_volume_data().await.record_into(&child);
                    root.record(child);
                }
                Ok(inspector)
            }
            .boxed()
        });

        FsInspectTree {
            _info: info_node,
            _usage: usage_node,
            _volumes: volumes_node,
            volumes_tracker,
        }
    }

    /// Registers a provider for per-volume data.  If `volume` is dropped, the node will remain
    /// present in the inspect tree but yield no data, until `Self::unregister_volume` is called.
    pub fn register_volume(
        self: &Arc<Self>,
        name: String,
        volume: Weak<dyn FsInspectVolume + Send + Sync + 'static>,
    ) {
        self.volumes_tracker.lock().unwrap().insert(name, volume);
    }

    pub fn unregister_volume(&self, name: String) {
        self.volumes_tracker.lock().unwrap().remove(&name);
    }
}

/// fs.info Properties
pub struct InfoData {
    pub id: u64,
    pub fs_type: u64,
    pub name: String,
    pub version_major: u64,
    pub version_minor: u64,
    pub block_size: u64,
    pub max_filename_length: u64,
    pub oldest_version: Option<String>,
}

impl InfoData {
    fn record_into(self, node: &Node) {
        node.record_uint("id", self.id);
        node.record_uint("type", self.fs_type);
        node.record_string("name", self.name);
        node.record_uint("version_major", self.version_major);
        node.record_uint("version_minor", self.version_minor);
        node.record_string(
            "current_version",
            format!("{}.{}", self.version_major, self.version_minor),
        );
        node.record_uint("block_size", self.block_size);
        node.record_uint("max_filename_length", self.max_filename_length);
        if self.oldest_version.is_some() {
            node.record_string("oldest_version", self.oldest_version.as_ref().unwrap());
        }
    }
}

/// fs.usage Properties
pub struct UsageData {
    pub total_bytes: u64,
    pub used_bytes: u64,
    pub total_nodes: u64,
    pub used_nodes: u64,
}

impl UsageData {
    fn record_into(self, node: &Node) {
        node.record_uint("total_bytes", self.total_bytes);
        node.record_uint("used_bytes", self.used_bytes);
        node.record_uint("total_nodes", self.total_nodes);
        node.record_uint("used_nodes", self.used_nodes);
    }
}

/// fs.volume/{name} roperties
pub struct VolumeData {
    pub used_bytes: u64,
    pub bytes_limit: Option<u64>,
    pub used_nodes: u64,
    pub encrypted: bool,
}

impl VolumeData {
    fn record_into(self, node: &Node) {
        node.record_uint("used_bytes", self.used_bytes);
        if let Some(bytes_limit) = self.bytes_limit {
            node.record_uint("bytes_limit", bytes_limit);
        }
        node.record_uint("used_nodes", self.used_nodes);
        node.record_bool("encrypted", self.encrypted);
    }
}