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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) Facebook, Inc. and its affiliates.
use anyhow::bail;
use anyhow::Result;
use glob::glob;
use log::{debug, trace, warn};
use proc_mounts::{MountInfo, MountList, SwapIter};
use scan_fmt::scan_fmt;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::Read;
use std::os::linux::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::process::Command;

lazy_static::lazy_static! {
    static ref MOUNT_LIST: Result<MountList> = Ok(MountList::new()?);
    static ref FINDMNT_BIN: String = super::find_bin("findmnt", Option::<&str>::None)
        .expect("Failed to find findmnt bin")
        .to_str()
        .unwrap()
        .to_owned();
}

/// Given a path, find out the containing mountpoint.
pub fn path_to_mountpoint<P: AsRef<Path>>(path_in: P) -> Result<MountInfo> {
    let path = path_in.as_ref();
    let mut abs_path = fs::canonicalize(&path)?;
    let mounts = match MOUNT_LIST.as_ref() {
        Ok(v) => v,
        Err(e) => bail!("Failed to list mount points ({:?})", &e),
    };

    loop {
        if let Some(mi) = mounts.get_mount_by_dest(&abs_path) {
            debug!("found mount point {:?} for {:?}", &mi.dest, &path);
            return Ok(mi.clone());
        }
        if !abs_path.pop() {
            bail!("Failed to find mount point for {:?}", path);
        }
    }
}

fn match_devnr<P: AsRef<Path>>(path_in: P, devnr: u64) -> bool {
    let path = path_in.as_ref();
    let mut buf = String::new();
    if let Err(err) = fs::File::open(&path).and_then(|mut f| f.read_to_string(&mut buf)) {
        warn!("Failed to open {:?} ({:?})", &path, &err);
        return false;
    }

    trace!("matching {:?} content '{}'", &path, buf.trim());
    let (maj, min) = match scan_fmt!(&buf, "{d}:{d}", u32, u32) {
        Ok(v) => v,
        Err(e) => {
            warn!("Failed to parse '{}' ({:?})", buf.trim(), &e);
            return false;
        }
    };

    if devnr == libc::makedev(maj, min) {
        trace!("matched {:?}", &path);
        return true;
    }
    return false;
}

/// Given a device number, find the kernel device name.
fn devnr_to_devname(devnr: u64) -> Result<OsString> {
    let blk_dir = Path::new("/sys/block");
    let blk_iter = blk_dir.read_dir()?;

    for blk_path in blk_iter.filter_map(|r| r.ok()).map(|e| e.path()) {
        let mut dev_path = PathBuf::from(&blk_path);

        dev_path.push("dev");
        if match_devnr(&dev_path, devnr) {
            dev_path.pop();
            return Ok(dev_path.file_name().unwrap().into());
        }
        dev_path.pop();

        let pattern = format!(
            "{}/{}*/dev",
            dev_path.to_str().unwrap().to_string(),
            dev_path.file_name().unwrap().to_str().unwrap()
        );
        for part in glob(&pattern).unwrap().filter_map(|x| x.ok()) {
            if match_devnr(&part, devnr) {
                return Ok(dev_path.file_name().unwrap().into());
            }
        }
    }
    bail!("No matching dev for 0x{:x}", devnr);
}

/// Given a device name, find the device number.
pub fn devname_to_devnr<D: AsRef<OsStr>>(name_in: D) -> Result<(u32, u32)> {
    let mut path = PathBuf::from("/dev");
    path.push(name_in.as_ref());
    let rdev = fs::metadata(path)?.st_rdev();
    Ok((unsafe { libc::major(rdev) }, unsafe { libc::minor(rdev) }))
}

/// Given a path, find the underlying device.
pub fn path_to_devname<P: AsRef<Path>>(path: P) -> Result<OsString> {
    let mtp = path_to_mountpoint(path.as_ref())?;

    // Ideally, mtp.source should point to the device node which can be
    // canonicalized by reading its devnr and mapping that to devname.
    // Unfortunately, determining the device for a filesystem, especially
    // root, is not trivial. For example, $mtp.source might point to
    // "/dev/root" which doesn't exist. `findmnt` has a bunch of heuristics
    // to map mount point to device. Let's use that instead.
    let output = Command::new(&*FINDMNT_BIN)
        .arg("-no")
        .arg("SOURCE")
        .arg(&mtp.dest)
        .output()?;

    if !output.status.success() {
        bail!("findmnt on {:?} failed with {:?}", &mtp.dest, &output);
    }

    let source = String::from_utf8(output.stdout).unwrap();
    trace!("path_to_devname path={:?} source={:?}", path.as_ref(), source.trim());
    devnr_to_devname(fs::metadata(source.trim())?.st_rdev())
}

fn read_model(dev_path: &Path) -> Result<String> {
    let mut path = PathBuf::from(dev_path);
    path.push("device");
    path.push("model");

    let mut model = String::new();
    let mut f = fs::File::open(&path)?;
    f.read_to_string(&mut model)?;
    Ok(model.trim_end().to_string())
}

fn read_fwrev(dev_path: &Path) -> Result<String> {
    let mut path = PathBuf::from(dev_path);
    path.push("device");
    path.push("firmware_rev");

    let mut fwrev = String::new();
    trace!("trying {:?}", &path);
    if path.exists() {
        let mut f = fs::File::open(&path)?;
        f.read_to_string(&mut fwrev)?;
    } else {
        path.pop();
        path.push("rev");
        trace!("trying {:?}", &path);
        if path.exists() {
            let mut f = fs::File::open(&path)?;
            f.read_to_string(&mut fwrev)?;
        } else {
            bail!("neither \"firmware_dev\" or \"rev\" is found");
        }
    }
    Ok(fwrev.trim_end().to_string())
}

/// Given a device name, determine its model, firmware version and size.
pub fn devname_to_model_fwrev_size<D: AsRef<OsStr>>(name_in: D) -> Result<(String, String, u64)> {
    let unknown = "<UNKNOWN>";
    let mut dev_path = PathBuf::from("/sys/block");
    dev_path.push(name_in.as_ref());

    let model = match read_model(&dev_path) {
        Ok(v) => v,
        Err(e) => {
            warn!(
                "storage_info: Failed to read model string for {:?} ({:#}), \
                 dm / md devices are not supported",
                &dev_path, &e
            );
            unknown.to_string()
        }
    };

    let fwrev = match read_fwrev(&dev_path) {
        Ok(v) => v,
        Err(e) => {
            warn!(
                "storage_info: Failed to read firmware revision for {:?} ({:#}), \
                 dm / md devices are not supported",
                &dev_path, &e
            );
            unknown.to_string()
        }
    };

    let mut size_path = dev_path.clone();
    size_path.push("size");

    let mut size = String::new();
    let mut f = fs::File::open(&size_path)?;
    f.read_to_string(&mut size)?;
    let size = size.trim().parse::<u64>()? * 512;

    Ok((model, fwrev, size))
}

/// Find all devices hosting swap
pub fn swap_devnames() -> Result<Vec<OsString>> {
    let mut devnames = Vec::new();
    for swap in SwapIter::new()?.filter_map(|sw| sw.ok()) {
        if swap.kind == "partition" {
            devnames.push(devnr_to_devname(fs::metadata(&swap.source)?.st_rdev())?);
        } else {
            devnames.push(path_to_devname(&swap.source)?);
        }
    }
    Ok(devnames)
}

/// Given a device name, determine whether it's rotational.
pub fn is_devname_rotational<P: AsRef<OsStr>>(devname: P) -> Result<bool> {
    let mut sysblk_path = PathBuf::from("/sys/block");
    sysblk_path.push(devname.as_ref());
    sysblk_path.push("queue");
    sysblk_path.push("rotational");

    let mut buf = String::new();
    fs::File::open(&sysblk_path).and_then(|mut f| f.read_to_string(&mut buf))?;

    trace!("read {:?} content '{}'", &sysblk_path, buf.trim());
    match scan_fmt!(&buf, "{d}", u32) {
        Ok(v) => Ok(v != 0),
        Err(e) => bail!("parse error: '{}' ({:?})", &buf, &e),
    }
}

/// Give a path, determine whether it's backed by a HDD.
pub fn is_path_rotational<P: AsRef<Path>>(path_in: P) -> bool {
    let path = path_in.as_ref();

    let devname = match path_to_devname(path) {
        Ok(v) => v,
        Err(e) => {
            warn!(
                "Failed to determine device name for {:?} ({:?}), assuming SSD",
                path, &e
            );
            return false;
        }
    };

    match is_devname_rotational(&devname) {
        Ok(v) => v,
        Err(e) => {
            warn!(
                "Failed to determine whether {:?} is rotational ({:?}), assuming SSD",
                &path, &e
            );
            false
        }
    }
}

/// Is any of the swaps rotational?
pub fn is_swap_rotational() -> bool {
    match swap_devnames() {
        Ok(devnames) => {
            debug!("swap devices: {:?}", &devnames);
            for devname in devnames {
                match is_devname_rotational(&devname) {
                    Ok(true) => {
                        debug!("swap device {:?} is rotational", &devname);
                        return true;
                    }
                    Ok(false) => debug!("swap device {:?} is not rotational", &devname),
                    Err(e) => warn!(
                        "Failed to determine whether {:?} is rotational ({:?})",
                        &devname, &e
                    ),
                }
            }
        }
        Err(e) => warn!("Failed to determine swap devices ({:?}), assuming SSD", &e),
    }
    false
}

#[cfg(test)]
mod tests {
    use std::env;

    #[test]
    fn test() {
        let _ = ::env_logger::try_init();
        if let Ok(v) = env::var("ROTATIONAL_TARGET") {
            println!("{} rotational={}", &v, super::is_path_rotational(&v));
        }
        println!("CWD rotational={}", super::is_path_rotational("."));
        println!("swap rotational={}", super::is_swap_rotational());
    }
}