devices/virtio/block/sys/
linux.rs

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
5use std::cmp::max;
6use std::cmp::min;
7
8use anyhow::Context;
9use base::unix::iov_max;
10use cros_async::Executor;
11use disk::DiskFile;
12
13use crate::virtio::block::DiskOption;
14use crate::virtio::BlockAsync;
15
16pub 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
25impl DiskOption {
26    /// Open the specified disk file.
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
41impl BlockAsync {
42    pub fn create_executor(&self) -> Executor {
43        Executor::with_executor_kind(self.executor_kind).expect("Failed to create an executor")
44    }
45}