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
get_seg_max(queue_size: u16) -> u3216 pub fn get_seg_max(queue_size: u16) -> u32 {
17 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 min(seg_max, u32::from(queue_size) - 2)
23 }
24
25 impl DiskOption {
26 /// Open the specified disk file.
open(&self) -> anyhow::Result<Box<dyn DiskFile>>27 pub fn open(&self) -> anyhow::Result<Box<dyn DiskFile>> {
28 disk::open_disk_file(disk::DiskFileParams {
29 path: self.path.clone(),
30 is_read_only: self.read_only,
31 is_sparse_file: self.sparse,
32 is_overlapped: false,
33 is_direct: self.direct,
34 lock: self.lock,
35 depth: 0,
36 })
37 .context("open_disk_file failed")
38 }
39 }
40
41 impl BlockAsync {
create_executor(&self) -> Executor42 pub fn create_executor(&self) -> Executor {
43 Executor::with_executor_kind(self.executor_kind).expect("Failed to create an executor")
44 }
45 }
46