scsidisk/
resolver.rs

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Resolver for SCSI devices.

use crate::SimpleScsiDisk;
use crate::scsidvd::SimpleScsiDvd;
use anyhow::Context;
use async_trait::async_trait;
use disk_backend::resolve::ResolveDiskParameters;
use futures::StreamExt;
use pal_async::task::Spawn;
use scsi_core::ResolveScsiDeviceHandleParams;
use scsi_core::ResolvedScsiDevice;
use scsidisk_resources::SimpleScsiDiskHandle;
use scsidisk_resources::SimpleScsiDvdHandle;
use scsidisk_resources::SimpleScsiDvdRequest;
use std::sync::Arc;
use std::sync::Weak;
use thiserror::Error;
use vm_resource::AsyncResolveResource;
use vm_resource::ResolveError;
use vm_resource::ResourceResolver;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::ScsiDeviceHandleKind;

/// A resolver for [`SimpleScsiDiskHandle`] and [`SimpleScsiDvdHandle`].
pub struct SimpleScsiResolver;

declare_static_async_resolver! {
    SimpleScsiResolver,
    (ScsiDeviceHandleKind, SimpleScsiDiskHandle),
    (ScsiDeviceHandleKind, SimpleScsiDvdHandle),
}

#[derive(Debug, Error)]
pub enum Error {
    #[error("failed to resolve backing disk")]
    Disk(#[source] ResolveError),
}

#[async_trait]
impl AsyncResolveResource<ScsiDeviceHandleKind, SimpleScsiDiskHandle> for SimpleScsiResolver {
    type Output = ResolvedScsiDevice;
    type Error = Error;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: SimpleScsiDiskHandle,
        _: ResolveScsiDeviceHandleParams<'_>,
    ) -> Result<Self::Output, Self::Error> {
        let disk = resolver
            .resolve(
                resource.disk,
                ResolveDiskParameters {
                    read_only: resource.read_only,
                    _async_trait_workaround: &(),
                },
            )
            .await
            .map_err(Error::Disk)?;

        let disk = SimpleScsiDisk::new(disk.0, resource.parameters);
        Ok(disk.into())
    }
}

#[async_trait]
impl AsyncResolveResource<ScsiDeviceHandleKind, SimpleScsiDvdHandle> for SimpleScsiResolver {
    type Output = ResolvedScsiDevice;
    type Error = Error;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: SimpleScsiDvdHandle,
        input: ResolveScsiDeviceHandleParams<'_>,
    ) -> Result<Self::Output, Self::Error> {
        let media = if let Some(media) = resource.media {
            Some(
                resolver
                    .resolve(
                        media,
                        ResolveDiskParameters {
                            read_only: true,
                            _async_trait_workaround: &(),
                        },
                    )
                    .await
                    .map_err(Error::Disk)?
                    .0,
            )
        } else {
            None
        };
        let dvd = Arc::new(SimpleScsiDvd::new(media));

        // Start a task to handle incoming change media requests.
        if let Some(requests) = resource.requests {
            input
                .driver_source
                .simple()
                .spawn(
                    "dvd-requests",
                    handle_dvd_requests(Arc::downgrade(&dvd), resolver.clone(), requests),
                )
                .detach();
        }

        Ok(ResolvedScsiDevice(dvd))
    }
}

async fn handle_dvd_requests(
    dvd: Weak<SimpleScsiDvd>,
    resolver: ResourceResolver,
    mut requests: mesh::Receiver<SimpleScsiDvdRequest>,
) {
    while let Some(req) = requests.next().await {
        match req {
            SimpleScsiDvdRequest::ChangeMedia(rpc) => {
                rpc.handle_failable(async |resource| {
                    let media = if let Some(resource) = resource {
                        Some(
                            resolver
                                .resolve(
                                    resource,
                                    ResolveDiskParameters {
                                        read_only: true,
                                        _async_trait_workaround: &(),
                                    },
                                )
                                .await
                                .context("failed to resolve media")?
                                .0,
                        )
                    } else {
                        None
                    };
                    if let Some(dvd) = dvd.upgrade() {
                        dvd.change_media(media);
                    }
                    anyhow::Ok(())
                })
                .await
            }
        }
    }
}