Skip to main content

btrfs_uapi/
filesystem.rs

1//! # Filesystem-level metadata: UUID, device count, node size, and generation
2//!
3//! Reads the top-level filesystem information that applies to the filesystem as
4//! a whole rather than to any individual device or subvolume.
5
6use crate::raw::{BTRFS_FS_INFO_FLAG_GENERATION, btrfs_ioc_fs_info, btrfs_ioctl_fs_info_args};
7use std::{
8    mem,
9    os::{fd::AsRawFd, unix::io::BorrowedFd},
10};
11use uuid::Uuid;
12
13/// Information about a mounted btrfs filesystem, as returned by
14/// `BTRFS_IOC_FS_INFO`.
15#[derive(Debug, Clone)]
16pub struct FsInfo {
17    /// Filesystem UUID.
18    pub uuid: Uuid,
19    /// Number of devices in the filesystem.
20    pub num_devices: u64,
21    /// Highest device ID in the filesystem.
22    pub max_id: u64,
23    /// B-tree node size in bytes.
24    pub nodesize: u32,
25    /// Sector size in bytes.
26    pub sectorsize: u32,
27    /// Generation number of the filesystem.
28    pub generation: u64,
29}
30
31/// Query information about the btrfs filesystem referred to by `fd`.
32pub fn fs_info(fd: BorrowedFd) -> nix::Result<FsInfo> {
33    let mut raw: btrfs_ioctl_fs_info_args = unsafe { mem::zeroed() };
34    raw.flags = BTRFS_FS_INFO_FLAG_GENERATION as u64;
35    unsafe { btrfs_ioc_fs_info(fd.as_raw_fd(), &mut raw) }?;
36
37    Ok(FsInfo {
38        uuid: Uuid::from_bytes(raw.fsid),
39        num_devices: raw.num_devices,
40        max_id: raw.max_id,
41        nodesize: raw.nodesize,
42        sectorsize: raw.sectorsize,
43        generation: raw.generation,
44    })
45}