Skip to content

Commit

Permalink
Merge pull request #1275 from hermit-os/virtio-fs-config
Browse files Browse the repository at this point in the history
refactor(virtio-spec): migrate `FsDevCfgRaw` to virtio-spec
  • Loading branch information
mkroening authored Jun 13, 2024
2 parents 920e4dc + a42446d commit 93cff11
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
21 changes: 16 additions & 5 deletions src/drivers/fs/virtio_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::str;

use pci_types::InterruptLine;
use virtio::fs::ConfigVolatileFieldAccess;
use virtio::FeatureBits;
use volatile::access::ReadOnly;
use volatile::VolatileRef;

use crate::config::VIRTIO_MAX_QUEUE_SIZE;
#[cfg(feature = "pci")]
use crate::drivers::fs::virtio_pci::FsDevCfgRaw;
use crate::drivers::virtio::error::VirtioFsError;
#[cfg(not(feature = "pci"))]
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg};
Expand All @@ -23,7 +25,7 @@ use crate::fs::fuse::{self, FuseInterface};
/// Handling the right access to fields, as some are read-only
/// for the driver.
pub(crate) struct FsDevCfg {
pub raw: &'static FsDevCfgRaw,
pub raw: VolatileRef<'static, virtio::fs::Config, ReadOnly>,
pub dev_id: u16,
pub features: virtio::fs::F,
}
Expand Down Expand Up @@ -112,7 +114,13 @@ impl VirtioFsDriver {
}

// 1 highprio queue, and n normal request queues
let vqnum = self.dev_cfg.raw.get_num_queues() + 1;
let vqnum = self
.dev_cfg
.raw
.as_ptr()
.num_request_queues()
.read()
.to_ne() + 1;
if vqnum == 0 {
error!("0 request queues requested from device. Aborting!");
return Err(VirtioFsError::Unknown);
Expand Down Expand Up @@ -160,7 +168,10 @@ impl FuseInterface for VirtioFsDriver {
}

fn get_mount_point(&self) -> String {
self.dev_cfg.raw.get_tag().to_string()
let tag = self.dev_cfg.raw.as_ptr().tag().read();
let tag = str::from_utf8(&tag).unwrap();
let tag = tag.split('\0').next().unwrap();
tag.to_string()
}
}

Expand Down
44 changes: 5 additions & 39 deletions src/drivers/fs/virtio_pci.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,23 @@
use alloc::vec::Vec;

use volatile::VolatileRef;

use crate::arch::pci::PciConfigRegion;
use crate::drivers::fs::virtio_fs::{FsDevCfg, VirtioFsDriver};
use crate::drivers::pci::PciDevice;
use crate::drivers::virtio::error::{self, VirtioError};
use crate::drivers::virtio::transport::pci;
use crate::drivers::virtio::transport::pci::{PciCap, UniCapsColl};

/// Virtio's network device configuration structure.
/// See specification v1.1. - 5.11.4
///
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub(crate) struct FsDevCfgRaw {
/// Tag is the name associated with this file system.
/// The tag is encoded in UTF-8 and padded with NUL bytes if shorter than the available space.
/// This field is not NUL-terminated if the encoded bytes take up the entire field.
tag: [u8; 36],
/// num_queues is the total number of request virtqueues exposed by the device.
/// Each virtqueue offers identical functionality and there are no ordering guarantees between
/// requests made available on different queues. Use of multiple queues is intended to increase
/// performance.
num_queues: i32,
}

impl FsDevCfgRaw {
pub fn get_tag(&self) -> &str {
let mut i: usize = 0;
let len = loop {
if i >= self.tag.len() {
break self.tag.len();
}
if self.tag[i] == 0 {
break i;
}
i += 1;
};

core::str::from_utf8(&self.tag[..len]).unwrap_or_default()
}

pub fn get_num_queues(&self) -> i32 {
self.num_queues
}
}

impl VirtioFsDriver {
fn map_cfg(cap: &PciCap) -> Option<FsDevCfg> {
let dev_cfg: &'static FsDevCfgRaw = match pci::map_dev_cfg::<FsDevCfgRaw>(cap) {
let dev_cfg = match pci::map_dev_cfg::<virtio::fs::Config>(cap) {
Some(cfg) => cfg,
None => return None,
};

let dev_cfg = VolatileRef::from_ref(dev_cfg);

Some(FsDevCfg {
raw: dev_cfg,
dev_id: cap.dev_id(),
Expand Down
37 changes: 37 additions & 0 deletions virtio-spec/src/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! File System Device

use volatile::access::ReadOnly;
use volatile_macro::VolatileFieldAccess;

pub use super::features::fs::F;
use super::le32;

/// Device configuration
#[doc(alias = "virtio_fs_config")]
#[cfg_attr(
feature = "zerocopy",
derive(zerocopy_derive::FromZeroes, zerocopy_derive::FromBytes)
)]
#[derive(VolatileFieldAccess)]
#[repr(C)]
pub struct Config {
/// This is the name associated with this file system. The tag is
/// encoded in UTF-8 and padded with NUL bytes if shorter than the
/// available space. This field is not NUL-terminated if the encoded bytes
/// take up the entire field.
#[access(ReadOnly)]
tag: [u8; 36],

/// This is the total number of request virtqueues
/// exposed by the device. Each virtqueue offers identical functionality and
/// there are no ordering guarantees between requests made available on
/// different queues. Use of multiple queues is intended to increase
/// performance.
#[access(ReadOnly)]
num_request_queues: le32,

/// This is the minimum number of bytes required for each
/// buffer in the notification queue.
#[access(ReadOnly)]
notify_buf_size: le32,
}
7 changes: 1 addition & 6 deletions virtio-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod bitflags;
#[macro_use]
pub mod volatile;
mod features;
pub mod fs;
pub mod mmio;
pub mod net;
pub mod pci;
Expand All @@ -19,12 +20,6 @@ use num_enum::{FromPrimitive, IntoPrimitive};

pub use self::features::{FeatureBits, F};

pub mod fs {
//! File System Device

pub use super::features::fs::F;
}

virtio_bitflags! {
/// Device Status Field
///
Expand Down

0 comments on commit 93cff11

Please sign in to comment.