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 use std::fs::File;
8 use std::fs::OpenOptions;
9 use std::os::fd::AsRawFd;
10
11 use anyhow::Context;
12 use base::add_fd_flags;
13 use base::flock;
14 use base::open_file_or_duplicate;
15 use base::unix::iov_max;
16 use base::FlockOperation;
17 use cros_async::Executor;
18 use disk::DiskFile;
19
20 use crate::virtio::block::DiskOption;
21 use crate::virtio::BlockAsync;
22
get_seg_max(queue_size: u16) -> u3223 pub fn get_seg_max(queue_size: u16) -> u32 {
24 let seg_max = min(max(iov_max(), 1), u32::max_value() as usize) as u32;
25
26 // Since we do not currently support indirect descriptors, the maximum
27 // number of segments must be smaller than the queue size.
28 // In addition, the request header and status each consume a descriptor.
29 min(seg_max, u32::from(queue_size) - 2)
30 }
31
32 impl DiskOption {
33 /// Open the specified disk file.
open(&self) -> anyhow::Result<Box<dyn DiskFile>>34 pub fn open(&self) -> anyhow::Result<Box<dyn DiskFile>> {
35 let mut options = OpenOptions::new();
36 options.read(true).write(!self.read_only);
37
38 let raw_image: File = open_file_or_duplicate(&self.path, &options)
39 .with_context(|| format!("failed to load disk image {}", self.path.display()))?;
40 // Lock the disk image to prevent other crosvm instances from using it.
41 let lock_op = if self.read_only {
42 FlockOperation::LockShared
43 } else {
44 FlockOperation::LockExclusive
45 };
46 flock(&raw_image, lock_op, true)
47 .with_context(|| format!("failed to lock disk image {}", self.path.display()))?;
48
49 // If O_DIRECT is requested, set the flag via fcntl. It is not done at
50 // open_file_or_reuse time because it will reuse existing fd and will
51 // not actually use the given OpenOptions.
52 if self.direct {
53 add_fd_flags(raw_image.as_raw_fd(), libc::O_DIRECT)
54 .with_context(|| format!("failed to set O_DIRECT to {}", &self.path.display()))?;
55 }
56
57 disk::create_disk_file(raw_image, self.sparse, disk::MAX_NESTING_DEPTH, &self.path)
58 .context("create_disk_file failed")
59 }
60 }
61
62 impl BlockAsync {
create_executor(&self) -> Executor63 pub fn create_executor(&self) -> Executor {
64 Executor::with_executor_kind(self.executor_kind).expect("Failed to create an executor")
65 }
66 }
67