Skip to main content

alioth/virtio/dev/fs/
shared_dir.rs

1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::cmp::min;
16use std::path::Path;
17use std::sync::Arc;
18
19use serde::Deserialize;
20use serde_aco::Help;
21
22use crate::fuse::passthrough::Passthrough;
23use crate::virtio::Result;
24use crate::virtio::dev::DevParam;
25use crate::virtio::dev::fs::{Fs, FsConfig};
26
27#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Help)]
28pub struct SharedDirParam {
29    /// Mount tag seen by the guest.
30    pub tag: String,
31    /// Path to the shared dir.
32    pub path: Box<Path>,
33    /// Size of memory region for DAX in bytes.
34    /// 0 means no DAX. [default: 0]
35    #[serde(default)]
36    pub dax_window: usize,
37}
38
39impl DevParam for SharedDirParam {
40    type Device = Fs<Passthrough>;
41
42    fn build(self, name: impl Into<Arc<str>>) -> Result<Fs<Passthrough>> {
43        let passthrough = Passthrough::new(self.path)?;
44        let mut config = FsConfig {
45            tag: [0; 36],
46            num_request_queues: 1,
47            notify_buf_size: 0,
48        };
49        let tag_size = min(config.tag.len(), self.tag.len());
50        config.tag[0..tag_size].copy_from_slice(&self.tag.as_bytes()[0..tag_size]);
51        Fs::new(name, passthrough, config, self.dax_window)
52    }
53}