xref: /aosp_15_r20/external/crosvm/devices/src/virtio/vhost_user_frontend/fs.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2021 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use data_model::Le32;
6 use virtio_sys::virtio_fs::virtio_fs_config;
7 use zerocopy::AsBytes;
8 
9 use crate::virtio::device_constants::fs::FS_MAX_TAG_LEN;
10 use crate::virtio::vhost_user_frontend::Error;
11 use crate::virtio::vhost_user_frontend::Result;
12 use crate::virtio::vhost_user_frontend::VhostUserFrontend;
13 use crate::virtio::DeviceType;
14 
15 impl VhostUserFrontend {
new_fs( base_features: u64, connection: vmm_vhost::Connection<vmm_vhost::FrontendReq>, max_queue_size: Option<u16>, tag: Option<&str>, ) -> Result<VhostUserFrontend>16     pub fn new_fs(
17         base_features: u64,
18         connection: vmm_vhost::Connection<vmm_vhost::FrontendReq>,
19         max_queue_size: Option<u16>,
20         tag: Option<&str>,
21     ) -> Result<VhostUserFrontend> {
22         let cfg = if let Some(tag) = tag {
23             if tag.len() > FS_MAX_TAG_LEN {
24                 return Err(Error::TagTooLong {
25                     len: tag.len(),
26                     max: FS_MAX_TAG_LEN,
27                 });
28             }
29 
30             let mut cfg_tag = [0u8; FS_MAX_TAG_LEN];
31             cfg_tag[..tag.len()].copy_from_slice(tag.as_bytes());
32 
33             Some(
34                 virtio_fs_config {
35                     tag: cfg_tag,
36                     // Only count the request queue, exclude the high prio queue
37                     num_request_queues: Le32::from(1),
38                 }
39                 .as_bytes()
40                 .to_vec(),
41             )
42         } else {
43             None
44         };
45 
46         VhostUserFrontend::new_internal(
47             connection,
48             DeviceType::Fs,
49             max_queue_size,
50             base_features,
51             cfg.as_deref(),
52             None, // pci_address
53         )
54     }
55 }
56