use std::collections::BTreeMap;
use std::io;
use std::sync::Arc;
use anyhow::anyhow;
use base::error;
use base::warn;
use base::AsRawDescriptor;
use base::Error as SysError;
use base::RawDescriptor;
use base::Tube;
use base::WorkerThread;
use data_model::Le32;
use remain::sorted;
use resources::Alloc;
use sync::Mutex;
use thiserror::Error;
use virtio_sys::virtio_fs::virtio_fs_config;
use virtio_sys::virtio_fs::VIRTIO_FS_SHMCAP_ID_CACHE;
use vm_control::FsMappingRequest;
use vm_control::VmResponse;
use vm_memory::GuestMemory;
use zerocopy::AsBytes;
use crate::pci::PciAddress;
use crate::pci::PciBarConfiguration;
use crate::pci::PciBarPrefetchable;
use crate::pci::PciBarRegionType;
use crate::pci::PciCapability;
use crate::virtio::copy_config;
use crate::virtio::device_constants::fs::FS_MAX_TAG_LEN;
use crate::virtio::DeviceType;
use crate::virtio::Interrupt;
use crate::virtio::PciCapabilityType;
use crate::virtio::Queue;
use crate::virtio::VirtioDevice;
use crate::virtio::VirtioPciShmCap;
#[cfg(feature = "arc_quota")]
mod arc_ioctl;
mod caps;
mod config;
mod expiring_map;
mod multikey;
pub mod passthrough;
mod read_dir;
mod worker;
pub use config::CachePolicy;
pub use config::Config;
use fuse::Server;
use passthrough::PassthroughFs;
pub use worker::Worker;
const QUEUE_SIZE: u16 = 1024;
const FS_BAR_NUM: u8 = 4;
const FS_BAR_OFFSET: u64 = 0;
const FS_BAR_SIZE: u64 = 1 << 33;
#[sorted]
#[derive(Error, Debug)]
pub enum Error {
#[error("failed to create file system: {0}")]
CreateFs(io::Error),
#[error("failed to create WaitContext: {0}")]
CreateWaitContext(SysError),
#[error("fuse error: {0}")]
FuseError(fuse::Error),
#[error("failed to get uids for the worker thread: {0}")]
GetResuid(SysError),
#[error("failed to get securebits for the worker thread: {0}")]
GetSecurebits(SysError),
#[error("request does not have any readable descriptors")]
NoReadableDescriptors,
#[error("request does not have any writable descriptors")]
NoWritableDescriptors,
#[error("failed to read from virtio queue Event: {0}")]
ReadQueueEvent(SysError),
#[error("failed to set securebits for the worker thread: {0}")]
SetSecurebits(SysError),
#[error("failed to signal used queue: {0}")]
SignalUsedQueue(SysError),
#[error("Fs device tag is too long: len = {0}, max = {FS_MAX_TAG_LEN}")]
TagTooLong(usize),
#[error("failed to unshare fs from parent: {0}")]
UnshareFromParent(SysError),
#[error("failed to wait for events: {0}")]
WaitError(SysError),
}
impl From<fuse::Error> for Error {
fn from(err: fuse::Error) -> Error {
Error::FuseError(err)
}
}
pub type Result<T> = ::std::result::Result<T, Error>;
pub struct Fs {
cfg: virtio_fs_config,
tag: String,
fs: Option<PassthroughFs>,
queue_sizes: Box<[u16]>,
avail_features: u64,
acked_features: u64,
use_dax: bool,
pci_bar: Option<Alloc>,
tube: Option<Tube>,
workers: Vec<WorkerThread<Result<()>>>,
}
impl Fs {
pub fn new(
base_features: u64,
tag: &str,
num_workers: usize,
fs_cfg: Config,
tube: Tube,
) -> Result<Fs> {
if tag.len() > FS_MAX_TAG_LEN {
return Err(Error::TagTooLong(tag.len()));
}
let mut cfg_tag = [0u8; FS_MAX_TAG_LEN];
cfg_tag[..tag.len()].copy_from_slice(tag.as_bytes());
let cfg = virtio_fs_config {
tag: cfg_tag,
num_request_queues: Le32::from(num_workers as u32),
};
let fs = PassthroughFs::new(tag, fs_cfg).map_err(Error::CreateFs)?;
let num_queues = num_workers + 1;
let use_dax = cfg!(target_arch = "x86_64") && fs.cfg().use_dax;
Ok(Fs {
cfg,
tag: tag.to_string(),
fs: Some(fs),
queue_sizes: vec![QUEUE_SIZE; num_queues].into_boxed_slice(),
avail_features: base_features,
acked_features: 0,
use_dax,
pci_bar: None,
tube: Some(tube),
workers: Vec::with_capacity(num_workers + 1),
})
}
}
impl VirtioDevice for Fs {
fn keep_rds(&self) -> Vec<RawDescriptor> {
let mut fds = self
.fs
.as_ref()
.map(PassthroughFs::keep_rds)
.unwrap_or_default();
if let Some(rd) = self.tube.as_ref().map(|s| s.as_raw_descriptor()) {
fds.push(rd);
}
fds
}
fn device_type(&self) -> DeviceType {
DeviceType::Fs
}
fn queue_max_sizes(&self) -> &[u16] {
&self.queue_sizes
}
fn features(&self) -> u64 {
self.avail_features
}
fn ack_features(&mut self, mut v: u64) {
let unrequested_features = v & !self.avail_features;
if unrequested_features != 0 {
warn!("virtio_fs got unknown feature ack: {:x}", v);
v &= !unrequested_features;
}
self.acked_features |= v;
}
fn read_config(&self, offset: u64, data: &mut [u8]) {
copy_config(data, 0, self.cfg.as_bytes(), offset)
}
fn activate(
&mut self,
_guest_mem: GuestMemory,
interrupt: Interrupt,
queues: BTreeMap<usize, Queue>,
) -> anyhow::Result<()> {
if queues.len() != self.queue_sizes.len() {
return Err(anyhow!(
"expected {} queues, got {}",
self.queue_sizes.len(),
queues.len()
));
}
let fs = self.fs.take().expect("missing file system implementation");
let server = Arc::new(Server::new(fs));
let socket = self.tube.take().expect("missing mapping socket");
let mut slot = 0;
if let Some(pci_bar) = self.pci_bar {
let request = FsMappingRequest::AllocateSharedMemoryRegion(pci_bar);
socket
.send(&request)
.expect("failed to send allocation message");
slot = match socket.recv() {
Ok(VmResponse::RegisterMemory { slot }) => slot,
Ok(VmResponse::Err(e)) => panic!("failed to allocate shared memory region: {}", e),
r => panic!(
"unexpected response to allocate shared memory region: {:?}",
r
),
};
}
let socket = Arc::new(Mutex::new(socket));
let mut watch_resample_event = true;
self.workers = queues
.into_iter()
.map(|(idx, queue)| {
let server = server.clone();
let irq = interrupt.clone();
let socket = Arc::clone(&socket);
let worker =
WorkerThread::start(format!("v_fs:{}:{}", self.tag, idx), move |kill_evt| {
let mut worker = Worker::new(queue, server, irq, socket, slot);
worker.run(kill_evt, watch_resample_event)
});
if watch_resample_event {
watch_resample_event = false;
}
worker
})
.collect();
Ok(())
}
fn get_device_bars(&mut self, address: PciAddress) -> Vec<PciBarConfiguration> {
if !self.use_dax {
return vec![];
}
self.pci_bar = Some(Alloc::PciBar {
bus: address.bus,
dev: address.dev,
func: address.func,
bar: FS_BAR_NUM,
});
vec![PciBarConfiguration::new(
FS_BAR_NUM as usize,
FS_BAR_SIZE,
PciBarRegionType::Memory64BitRegion,
PciBarPrefetchable::Prefetchable,
)]
}
fn get_device_caps(&self) -> Vec<Box<dyn PciCapability>> {
if !self.use_dax {
return vec![];
}
vec![Box::new(VirtioPciShmCap::new(
PciCapabilityType::SharedMemoryConfig,
FS_BAR_NUM,
FS_BAR_OFFSET,
FS_BAR_SIZE,
VIRTIO_FS_SHMCAP_ID_CACHE as u8,
))]
}
}