nvme/namespace/
reservations.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Persistent reservation support

use super::Namespace;
use super::map_disk_error;
use crate::error::NvmeError;
use crate::prp::PrpRange;
use crate::spec;
use crate::spec::nvm;
use disk_backend::pr::PersistentReservation;
use nvme_common::to_nvme_reservation_type;
use zerocopy::FromZeros;
use zerocopy::IntoBytes;

impl Namespace {
    pub(super) async fn reservation_register(
        &self,
        pr: &dyn PersistentReservation,
        command: &spec::Command,
    ) -> Result<(), NvmeError> {
        let cdw10 = nvm::Cdw10ReservationRegister::from(command.cdw10);
        let mut data = nvm::ReservationRegister::new_zeroed();
        let range = PrpRange::parse(&self.mem, size_of_val(&data), command.dptr)?;
        range.read(&self.mem, data.as_mut_bytes())?;

        let current_key = (!cdw10.iekey()).then_some(data.crkey);
        let ptpl = if pr.capabilities().persist_through_power_loss {
            match nvm::ChangePersistThroughPowerLoss(cdw10.cptpl()) {
                nvm::ChangePersistThroughPowerLoss::NO_CHANGE => None,
                nvm::ChangePersistThroughPowerLoss::CLEAR => Some(false),
                nvm::ChangePersistThroughPowerLoss::SET => Some(true),
                _ => return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into()),
            }
        } else {
            None
        };

        match nvm::ReservationRegisterAction(cdw10.rrega()) {
            nvm::ReservationRegisterAction::REGISTER => {
                pr.register(None, data.nrkey, ptpl)
                    .await
                    .map_err(map_disk_error)?;
            }
            nvm::ReservationRegisterAction::UNREGISTER => {
                pr.register(current_key, 0, ptpl)
                    .await
                    .map_err(map_disk_error)?;
            }
            nvm::ReservationRegisterAction::REPLACE => {
                pr.register(current_key, data.nrkey, ptpl)
                    .await
                    .map_err(map_disk_error)?;
            }
            action => {
                tracelimit::warn_ratelimited!(?action, "unsupported reservation register action");
                return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into());
            }
        }
        Ok(())
    }

    pub(super) async fn reservation_report(
        &self,
        pr: &dyn PersistentReservation,
        command: &spec::Command,
    ) -> Result<(), NvmeError> {
        let cdw10 = nvm::Cdw10ReservationReport::from(command.cdw10);
        let cdw11 = nvm::Cdw11ReservationReport::from(command.cdw11);
        let numd = cdw10.numd_z().saturating_add(1) as usize;
        let len = numd * 4;
        let range = PrpRange::parse(&self.mem, len, command.dptr)?;

        let report = pr.report().await.map_err(map_disk_error)?;

        let report_header = nvm::ReservationReportExtended {
            report: nvm::ReservationReport {
                generation: report.generation,
                rtype: report
                    .reservation_type
                    .map_or(nvm::ReservationType(0), to_nvme_reservation_type),
                regctl: (report.controllers.len() as u16).into(),
                ptpls: 0,
                ..FromZeros::new_zeroed()
            },
            ..FromZeros::new_zeroed()
        };

        let controllers = report.controllers.iter().map(|controller| {
            let mut hostid = [0; 16];
            let hostid_len = controller.host_id.len().min(hostid.len());
            hostid[..hostid_len].copy_from_slice(&controller.host_id[..hostid_len]);
            nvm::RegisteredControllerExtended {
                cntlid: controller.controller_id,
                rcsts: nvm::ReservationStatus::new()
                    .with_holds_reservation(controller.holds_reservation),
                hostid,
                rkey: controller.key,
                ..FromZeros::new_zeroed()
            }
        });

        let mut data;
        if cdw11.eds() {
            data = report_header.as_bytes().to_vec();
            for controller in controllers {
                data.extend(controller.as_bytes());
            }
        } else {
            data = report_header.report.as_bytes().to_vec();
            for controller in controllers {
                data.extend(
                    nvm::RegisteredController {
                        cntlid: controller.cntlid,
                        rcsts: controller.rcsts,
                        hostid: controller.hostid[..8].try_into().unwrap(),
                        rkey: controller.rkey,
                        ..FromZeros::new_zeroed()
                    }
                    .as_bytes(),
                );
            }
        };

        range.write(&self.mem, &data)?;
        Ok(())
    }

    pub(super) async fn reservation_acquire(
        &self,
        pr: &dyn PersistentReservation,
        command: &spec::Command,
    ) -> Result<(), NvmeError> {
        let cdw10 = nvm::Cdw10ReservationAcquire::from(command.cdw10);
        let mut data = nvm::ReservationAcquire::new_zeroed();
        let range = PrpRange::parse(&self.mem, size_of_val(&data), command.dptr)?;
        range.read(&self.mem, data.as_mut_bytes())?;

        // According to the spec, this is never to be set.
        if cdw10.iekey() {
            return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into());
        }

        let reservation_type = from_nvme_reservation_type(cdw10.rtype())?;
        match nvm::ReservationAcquireAction(cdw10.racqa()) {
            nvm::ReservationAcquireAction::ACQUIRE => {
                pr.reserve(data.crkey, reservation_type)
                    .await
                    .map_err(map_disk_error)?;
            }
            nvm::ReservationAcquireAction::PREEMPT => {
                pr.preempt(data.crkey, data.prkey, reservation_type, false)
                    .await
                    .map_err(map_disk_error)?;
            }
            nvm::ReservationAcquireAction::PREEMPT_AND_ABORT => {
                pr.preempt(data.crkey, data.prkey, reservation_type, true)
                    .await
                    .map_err(map_disk_error)?;
            }
            action => {
                tracelimit::warn_ratelimited!(?action, "unsupported reservation acquire action");
                return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into());
            }
        }

        Ok(())
    }

    pub(super) async fn reservation_release(
        &self,
        pr: &dyn PersistentReservation,
        command: &spec::Command,
    ) -> Result<(), NvmeError> {
        let cdw10 = nvm::Cdw10ReservationRelease::from(command.cdw10);
        let mut data = nvm::ReservationRelease::new_zeroed();
        let range = PrpRange::parse(&self.mem, size_of_val(&data), command.dptr)?;
        range.read(&self.mem, data.as_mut_bytes())?;

        // According to the spec, this is never to be set.
        if cdw10.iekey() {
            return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into());
        }

        match nvm::ReservationReleaseAction(cdw10.rrela()) {
            nvm::ReservationReleaseAction::RELEASE => pr
                .release(data.crkey, from_nvme_reservation_type(cdw10.rtype())?)
                .await
                .map_err(map_disk_error)?,
            nvm::ReservationReleaseAction::CLEAR => {
                pr.clear(data.crkey).await.map_err(map_disk_error)?
            }
            action => {
                tracelimit::warn_ratelimited!(?action, "unsupported reservation release action");
                return Err(spec::Status::INVALID_FIELD_IN_COMMAND.into());
            }
        }

        Ok(())
    }

    pub(super) async fn get_reservation_persistence(
        &self,
        pr: &dyn PersistentReservation,
    ) -> Result<nvme_spec::Cdw11FeatureReservationPersistence, NvmeError> {
        let report = pr.report().await.map_err(map_disk_error)?;
        Ok(nvme_spec::Cdw11FeatureReservationPersistence::new()
            .with_ptpl(report.persist_through_power_loss))
    }
}

fn from_nvme_reservation_type(
    nvme_type: u8,
) -> Result<disk_backend::pr::ReservationType, NvmeError> {
    let reservation_type = nvm::ReservationType(nvme_type);
    nvme_common::from_nvme_reservation_type(reservation_type)
        .map_err(|err| NvmeError::new(spec::Status::INVALID_FIELD_IN_COMMAND, err))
}