aboutsummaryrefslogtreecommitdiff
path: root/devices/src/virtio/block/sys/windows.rs
blob: 4c215290f0bb6d0ae0552f87ba8c669a92ff6ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::fs::File;
use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt;

use anyhow::Context;
use base::warn;
use cros_async::sys::windows::ExecutorKindSys;
use cros_async::Executor;
use winapi::um::winbase::FILE_FLAG_NO_BUFFERING;
use winapi::um::winbase::FILE_FLAG_OVERLAPPED;
use winapi::um::winnt::FILE_SHARE_READ;
use winapi::um::winnt::FILE_SHARE_WRITE;

use crate::virtio::block::DiskOption;
use crate::virtio::BlockAsync;

pub fn get_seg_max(_queue_size: u16) -> u32 {
    // Allow a single segment per request, since vectored I/O is not implemented for Windows yet.
    1
}

impl DiskOption {
    /// Open the specified disk file.
    pub fn open(&self) -> anyhow::Result<Box<dyn disk::DiskFile>> {
        let mut open_option = OpenOptions::new();
        open_option
            .read(true)
            .write(!self.read_only)
            .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE);

        let mut flags = 0;
        if self.direct {
            warn!("Opening disk file with no buffering");
            flags |= FILE_FLAG_NO_BUFFERING;
        }

        if self.async_executor == Some(ExecutorKindSys::Overlapped.into()) {
            warn!("Opening disk file for overlapped IO");
            flags |= FILE_FLAG_OVERLAPPED;
        }

        if flags != 0 {
            open_option.custom_flags(flags);
        }

        let file = open_option
            .open(&self.path)
            .context("Failed to open disk file")?;
        let image_type = disk::detect_image_type(
            &file,
            self.async_executor == Some(ExecutorKindSys::Overlapped.into()),
        )?;
        Ok(disk::create_disk_file_of_type(
            file,
            self.sparse,
            disk::MAX_NESTING_DEPTH,
            &self.path,
            image_type,
        )?)
    }
}

impl BlockAsync {
    pub fn create_executor(&self) -> Executor {
        Executor::with_kind_and_concurrency(self.executor_kind, self.io_concurrency)
            .expect("Failed to create an executor")
    }
}