/src/crosvm/devices/src/virtio/block/sys/linux.rs
Line | Count | Source |
1 | | // Copyright 2022 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 std::cmp::max; |
6 | | use std::cmp::min; |
7 | | |
8 | | use anyhow::Context; |
9 | | use base::unix::iov_max; |
10 | | use cros_async::Executor; |
11 | | use disk::DiskFile; |
12 | | |
13 | | use crate::virtio::block::DiskOption; |
14 | | use crate::virtio::BlockAsync; |
15 | | |
16 | 496 | pub fn get_seg_max(queue_size: u16) -> u32 { |
17 | 496 | let seg_max = min(max(iov_max(), 1), u32::MAX as usize) as u32; |
18 | | |
19 | | // Since we do not currently support indirect descriptors, the maximum |
20 | | // number of segments must be smaller than the queue size. |
21 | | // In addition, the request header and status each consume a descriptor. |
22 | 496 | min(seg_max, u32::from(queue_size) - 2) |
23 | 496 | } |
24 | | |
25 | | impl DiskOption { |
26 | | /// Open the specified disk file. |
27 | 0 | pub fn open(&self) -> anyhow::Result<Box<dyn DiskFile>> { |
28 | 0 | disk::open_disk_file(disk::DiskFileParams { |
29 | 0 | path: self.path.clone(), |
30 | 0 | is_read_only: self.read_only, |
31 | 0 | is_sparse_file: self.sparse, |
32 | 0 | is_overlapped: false, |
33 | 0 | is_direct: self.direct, |
34 | 0 | lock: self.lock, |
35 | 0 | depth: 0, |
36 | 0 | }) |
37 | 0 | .context("open_disk_file failed") |
38 | 0 | } |
39 | | } |
40 | | |
41 | | impl BlockAsync { |
42 | 496 | pub fn create_executor(&self) -> Executor { |
43 | 496 | Executor::with_executor_kind(self.executor_kind).expect("Failed to create an executor") |
44 | 496 | } |
45 | | } |