-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathstorage.rs
More file actions
249 lines (205 loc) · 5.95 KB
/
Copy pathstorage.rs
File metadata and controls
249 lines (205 loc) · 5.95 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! System utilities related to devices/peripherals
//!
//! Discovery reads sysfs under `/sys/dev/block/` and stats the device through
//! `MetadataExt`, neither of which exists outside the unix family. Non-unix
//! targets get arms that report nothing found, so callers need no condition of
//! their own.
use std::path::Path;
#[cfg(unix)]
use std::{
ffi::OsStr,
fs,
fs::{FileType, read_to_string},
path::PathBuf,
};
#[cfg(unix)]
use itertools::Itertools;
#[cfg(unix)]
use libc::dev_t;
use crate::Result;
#[cfg(unix)]
use crate::{
result::FlatOk,
utils::{result::LogDebugErr, string::SplitInfallible},
};
/// Multi-Device (md) i.e. software raid properties.
#[derive(Clone, Debug, Default)]
pub struct MultiDevice {
/// Type of raid (i.e. `raid1`); None if no raid present or detected.
pub level: Option<String>,
/// Number of participating devices.
pub raid_disks: usize,
/// The MQ's discovered on the devices; or empty.
pub md: Vec<MultiQueue>,
}
/// Multi-Queue (mq) characteristics.
#[derive(Clone, Debug, Default)]
pub struct MultiQueue {
/// Number of requests for the device.
pub nr_requests: Option<usize>,
/// Individual queue characteristics.
pub mq: Vec<Queue>,
}
/// Single-queue characteristics
#[derive(Clone, Debug, Default)]
pub struct Queue {
/// Queue's indice.
pub id: usize,
/// Number of requests for the queue.
pub nr_tags: Option<usize>,
/// CPU affinities for the queue.
pub cpu_list: Vec<usize>,
}
/// Get properties of a MultiDevice (md) storage system
#[cfg(unix)]
#[must_use]
pub fn md_discover(path: &Path) -> MultiDevice {
let dev_id = dev_from_path(path)
.log_debug_err()
.unwrap_or_default();
let md_path = block_path(dev_id).join("md/");
let raid_disks_path = md_path.join("raid_disks");
let raid_disks: usize = read_to_string(&raid_disks_path)
.ok()
.as_deref()
.map(str::trim)
.map(str::parse)
.flat_ok()
.unwrap_or(0);
let single_fallback = raid_disks.eq(&0).then(|| block_path(dev_id));
MultiDevice {
raid_disks,
level: read_to_string(md_path.join("level"))
.ok()
.as_deref()
.map(str::trim)
.map(ToOwned::to_owned),
md: (0..raid_disks)
.map(|i| format!("rd{i}/block"))
.map(|path| md_path.join(&path))
.filter_map(|ref path| path.canonicalize().ok())
.map(|mut path| {
path.pop();
path
})
.chain(single_fallback)
.map(|path| mq_discover(&path))
.filter(|mq| !mq.mq.is_empty())
.collect(),
}
}
/// Get properties of a MultiDevice (md) storage system.
///
/// Reports no raid on a target without sysfs, which is what the unix arm also
/// returns for a path that is not on one.
#[cfg(not(unix))]
#[must_use]
pub fn md_discover(_path: &Path) -> MultiDevice { MultiDevice::default() }
/// Get properties of a MultiQueue within a MultiDevice.
#[cfg(unix)]
#[must_use]
fn mq_discover(path: &Path) -> MultiQueue {
let mq_path = path.join("mq/");
let nr_requests_path = path.join("queue/nr_requests");
MultiQueue {
nr_requests: read_to_string(&nr_requests_path)
.ok()
.as_deref()
.map(str::trim)
.map(str::parse)
.flat_ok(),
mq: fs::read_dir(&mq_path)
.into_iter()
.flat_map(IntoIterator::into_iter)
.filter_map(Result::ok)
.filter(|entry| {
entry
.file_type()
.as_ref()
.is_ok_and(FileType::is_dir)
})
.map(|dir| queue_discover(&dir.path()))
.sorted_by_key(|mq| mq.id)
.collect::<Vec<_>>(),
}
}
/// Get properties of a Queue within a MultiQueue.
#[cfg(unix)]
fn queue_discover(dir: &Path) -> Queue {
let queue_id = dir.file_name();
let nr_tags_path = dir.join("nr_tags");
let cpu_list_path = dir.join("cpu_list");
Queue {
id: queue_id
.and_then(OsStr::to_str)
.map(str::parse)
.flat_ok()
.expect("queue has some numerical identifier"),
nr_tags: read_to_string(&nr_tags_path)
.ok()
.as_deref()
.map(str::trim)
.map(str::parse)
.flat_ok(),
cpu_list: read_to_string(&cpu_list_path)
.iter()
.flat_map(|list| list.trim().split(','))
.map(str::trim)
.map(str::parse)
.filter_map(Result::ok)
.collect(),
}
}
/// Get the name of the block device on which Path is mounted.
#[cfg(unix)]
pub fn name_from_path(path: &Path) -> Result<String> {
use std::io::{Error, ErrorKind::NotFound};
let (major, minor) = dev_from_path(path)?;
let path = block_path((major, minor)).join("uevent");
read_to_string(path)
.iter()
.map(String::as_str)
.flat_map(str::lines)
.map(|line| line.split_once_infallible("="))
.find_map(|(key, val)| (key == "DEVNAME").then_some(val))
.ok_or_else(|| Error::new(NotFound, "DEVNAME not found."))
.map_err(Into::into)
.map(Into::into)
}
/// Get the name of the block device on which Path is mounted.
///
/// Naming the device requires sysfs, so this reports the target cannot do it.
/// The unix arm already returns an error when the name is not there to be read.
#[cfg(not(unix))]
pub fn name_from_path(_path: &Path) -> Result<String> {
use std::io::{Error, ErrorKind::Unsupported};
Err(Error::new(Unsupported, "Block device discovery requires sysfs.").into())
}
/// Get the (major, minor) of the block device on which Path is mounted.
#[cfg(unix)]
fn dev_from_path(path: &Path) -> Result<(dev_t, dev_t)> {
use std::os::unix::fs::MetadataExt;
let stat = fs::metadata(path)?;
// Metadata::dev() is u64 on every unix; dev_t itself is not, so the
// conversions below differ per platform.
#[cfg(target_os = "linux")]
let dev_id = stat.dev();
#[cfg(not(target_os = "linux"))]
let dev_id = stat.dev().try_into()?;
let (major, minor) = (libc::major(dev_id), libc::minor(dev_id));
#[cfg(target_os = "linux")]
let (major, minor) = (major.into(), minor.into());
#[cfg(target_os = "android")]
let (major, minor) = (major.try_into()?, minor.try_into()?);
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_vendor = "apple"
)))]
let (major, minor) = (major.try_into()?, minor.try_into()?);
Ok((major, minor))
}
#[cfg(unix)]
fn block_path((major, minor): (dev_t, dev_t)) -> PathBuf {
format!("/sys/dev/block/{major}:{minor}/").into()
}