libnvme 0.8.0

Safe, idiomatic Rust bindings for the Linux libnvme C library
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! NVMe Reservation commands (Acquire / Register / Release / Report).
//!
//! Reservations let multiple host controllers coordinate access to a shared
//! namespace — the NVMe analogue of SCSI Persistent Reservations. Typical
//! use is multi-host NVMe-oF clustering where two or more hosts back the
//! same namespace and need to negotiate which one has write access.
//!
//! All four commands are namespace-scoped. Build them via the methods on
//! [`Namespace`] (`reservation_acquire`, `reservation_register`,
//! `reservation_release`, `reservation_report`) and call `.execute()`.
//!
//! See NVMe spec §8.19 for the full semantics of each action.

use std::ffi::c_void;

use libnvme_sys::{
    nvme_resv_acquire, nvme_resv_acquire_args, nvme_resv_register, nvme_resv_register_args,
    nvme_resv_release, nvme_resv_release_args, nvme_resv_report, nvme_resv_report_args,
    nvme_resv_status,
};

use crate::error::{check_ret, Error, Result};
use crate::namespace::Namespace;

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

/// Reservation type (rtype) — sets the access semantics for the reservation
/// holder vs. registered hosts vs. non-registered hosts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ReservationType {
    /// Write Exclusive — only the holder can write; all hosts can read.
    WriteExclusive = 1,
    /// Exclusive Access — only the holder can read or write.
    ExclusiveAccess = 2,
    /// Write Exclusive, Registrants Only.
    WriteExclusiveRegistrantsOnly = 3,
    /// Exclusive Access, Registrants Only.
    ExclusiveAccessRegistrantsOnly = 4,
    /// Write Exclusive, All Registrants.
    WriteExclusiveAllRegistrants = 5,
    /// Exclusive Access, All Registrants.
    ExclusiveAccessAllRegistrants = 6,
}

impl ReservationType {
    fn as_raw(self) -> u32 {
        self as u8 as u32
    }
}

/// Reservation Acquire action (racqa).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum ReservationAcquireAction {
    /// Acquire the reservation.
    #[default]
    Acquire = 0,
    /// Preempt the current holder.
    Preempt = 1,
    /// Preempt and abort outstanding commands from the current holder.
    PreemptAndAbort = 2,
}

impl ReservationAcquireAction {
    fn as_raw(self) -> u32 {
        self as u8 as u32
    }
}

/// Reservation Register action (rrega).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum ReservationRegisterAction {
    /// Register a new reservation key.
    #[default]
    Register = 0,
    /// Unregister this host's reservation key.
    Unregister = 1,
    /// Replace the existing reservation key.
    Replace = 2,
}

impl ReservationRegisterAction {
    fn as_raw(self) -> u32 {
        self as u8 as u32
    }
}

/// Reservation Release action (rrela).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum ReservationReleaseAction {
    /// Release the reservation.
    #[default]
    Release = 0,
    /// Clear all reservations and registrations.
    Clear = 1,
}

impl ReservationReleaseAction {
    fn as_raw(self) -> u32 {
        self as u8 as u32
    }
}

/// Change-Persist-Through-Power-Loss (cptpl) for Reservation Register.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum PtplChange {
    /// No change to PTPL state.
    #[default]
    NoChange = 0,
    /// Reservations and registrations are released on power-on.
    Clear = 2,
    /// Reservations and registrations persist across power loss.
    Persist = 3,
}

impl PtplChange {
    fn as_raw(self) -> u32 {
        self as u8 as u32
    }
}

// ---------------------------------------------------------------------------
// Acquire
// ---------------------------------------------------------------------------

/// Builder returned by [`Namespace::reservation_acquire`].
pub struct ReservationAcquire<'a, 'r> {
    ns: &'a Namespace<'r>,
    crkey: u64,
    nrkey: u64,
    rtype: ReservationType,
    action: ReservationAcquireAction,
    iekey: bool,
    timeout_ms: u32,
}

impl<'a, 'r> ReservationAcquire<'a, 'r> {
    pub(crate) fn new(ns: &'a Namespace<'r>) -> Self {
        ReservationAcquire {
            ns,
            crkey: 0,
            nrkey: 0,
            rtype: ReservationType::WriteExclusive,
            action: ReservationAcquireAction::default(),
            iekey: false,
            timeout_ms: 0,
        }
    }

    /// Current Reservation Key associated with this host.
    pub fn key(mut self, crkey: u64) -> Self {
        self.crkey = crkey;
        self
    }

    /// New Reservation Key — used only when `action` is
    /// [`ReservationAcquireAction::Preempt`] or `PreemptAndAbort`.
    pub fn new_key(mut self, nrkey: u64) -> Self {
        self.nrkey = nrkey;
        self
    }

    /// Reservation type to acquire.
    pub fn rtype(mut self, rtype: ReservationType) -> Self {
        self.rtype = rtype;
        self
    }

    /// Which acquire action to perform.
    pub fn action(mut self, action: ReservationAcquireAction) -> Self {
        self.action = action;
        self
    }

    /// Ignore Existing Key — bypass the host-registered key check.
    pub fn ignore_existing_key(mut self) -> Self {
        self.iekey = true;
        self
    }

    /// Per-command timeout in milliseconds.
    pub fn timeout_ms(mut self, ms: u32) -> Self {
        self.timeout_ms = ms;
        self
    }

    /// Issue the Reservation Acquire command.
    pub fn execute(self) -> Result<u32> {
        let fd = ns_fd(self.ns)?;
        let mut result: u32 = 0;
        let mut args = nvme_resv_acquire_args {
            crkey: self.crkey,
            nrkey: self.nrkey,
            result: &mut result,
            args_size: std::mem::size_of::<nvme_resv_acquire_args>() as i32,
            fd,
            timeout: self.timeout_ms,
            nsid: self.ns.nsid(),
            rtype: self.rtype.as_raw(),
            racqa: self.action.as_raw(),
            iekey: self.iekey,
        };
        // SAFETY: args is fully-initialized on the stack; fd is a valid
        // file descriptor for this namespace.
        let ret = unsafe { nvme_resv_acquire(&mut args) };
        check_ret(ret)?;
        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// Register
// ---------------------------------------------------------------------------

/// Builder returned by [`Namespace::reservation_register`].
pub struct ReservationRegister<'a, 'r> {
    ns: &'a Namespace<'r>,
    crkey: u64,
    nrkey: u64,
    action: ReservationRegisterAction,
    cptpl: PtplChange,
    iekey: bool,
    timeout_ms: u32,
}

impl<'a, 'r> ReservationRegister<'a, 'r> {
    pub(crate) fn new(ns: &'a Namespace<'r>) -> Self {
        ReservationRegister {
            ns,
            crkey: 0,
            nrkey: 0,
            action: ReservationRegisterAction::default(),
            cptpl: PtplChange::default(),
            iekey: false,
            timeout_ms: 0,
        }
    }

    /// Current Reservation Key — only required when replacing or
    /// unregistering an existing key.
    pub fn key(mut self, crkey: u64) -> Self {
        self.crkey = crkey;
        self
    }

    /// New Reservation Key to register (or replace with).
    pub fn new_key(mut self, nrkey: u64) -> Self {
        self.nrkey = nrkey;
        self
    }

    /// Which register action to perform.
    pub fn action(mut self, action: ReservationRegisterAction) -> Self {
        self.action = action;
        self
    }

    /// Change Persist-Through-Power-Loss state.
    pub fn ptpl(mut self, cptpl: PtplChange) -> Self {
        self.cptpl = cptpl;
        self
    }

    /// Ignore Existing Key — bypass the host-registered key check.
    pub fn ignore_existing_key(mut self) -> Self {
        self.iekey = true;
        self
    }

    pub fn timeout_ms(mut self, ms: u32) -> Self {
        self.timeout_ms = ms;
        self
    }

    pub fn execute(self) -> Result<u32> {
        let fd = ns_fd(self.ns)?;
        let mut result: u32 = 0;
        let mut args = nvme_resv_register_args {
            crkey: self.crkey,
            nrkey: self.nrkey,
            result: &mut result,
            args_size: std::mem::size_of::<nvme_resv_register_args>() as i32,
            fd,
            timeout: self.timeout_ms,
            nsid: self.ns.nsid(),
            rrega: self.action.as_raw(),
            cptpl: self.cptpl.as_raw(),
            iekey: self.iekey,
        };
        // SAFETY: args is fully-initialized on the stack; fd is valid.
        let ret = unsafe { nvme_resv_register(&mut args) };
        check_ret(ret)?;
        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// Release
// ---------------------------------------------------------------------------

/// Builder returned by [`Namespace::reservation_release`].
pub struct ReservationRelease<'a, 'r> {
    ns: &'a Namespace<'r>,
    crkey: u64,
    rtype: ReservationType,
    action: ReservationReleaseAction,
    iekey: bool,
    timeout_ms: u32,
}

impl<'a, 'r> ReservationRelease<'a, 'r> {
    pub(crate) fn new(ns: &'a Namespace<'r>) -> Self {
        ReservationRelease {
            ns,
            crkey: 0,
            rtype: ReservationType::WriteExclusive,
            action: ReservationReleaseAction::default(),
            iekey: false,
            timeout_ms: 0,
        }
    }

    /// Current Reservation Key being released.
    pub fn key(mut self, crkey: u64) -> Self {
        self.crkey = crkey;
        self
    }

    /// Reservation type being released (must match the held type).
    pub fn rtype(mut self, rtype: ReservationType) -> Self {
        self.rtype = rtype;
        self
    }

    /// Release action.
    pub fn action(mut self, action: ReservationReleaseAction) -> Self {
        self.action = action;
        self
    }

    /// Ignore Existing Key.
    pub fn ignore_existing_key(mut self) -> Self {
        self.iekey = true;
        self
    }

    pub fn timeout_ms(mut self, ms: u32) -> Self {
        self.timeout_ms = ms;
        self
    }

    pub fn execute(self) -> Result<u32> {
        let fd = ns_fd(self.ns)?;
        let mut result: u32 = 0;
        let mut args = nvme_resv_release_args {
            crkey: self.crkey,
            result: &mut result,
            args_size: std::mem::size_of::<nvme_resv_release_args>() as i32,
            fd,
            timeout: self.timeout_ms,
            nsid: self.ns.nsid(),
            rtype: self.rtype.as_raw(),
            rrela: self.action.as_raw(),
            iekey: self.iekey,
        };
        // SAFETY: args is fully-initialized on the stack; fd is valid.
        let ret = unsafe { nvme_resv_release(&mut args) };
        check_ret(ret)?;
        Ok(result)
    }
}

// ---------------------------------------------------------------------------
// Report
// ---------------------------------------------------------------------------

/// Builder returned by [`Namespace::reservation_report`].
///
/// Reads into a caller-supplied buffer. The buffer holds an
/// `nvme_resv_status` header followed by a variable number of registered
/// controller entries; callers cast to the bindgen type or use the
/// convenience [`ReservationReport::execute_to_vec`] for a default-sized
/// allocation.
pub struct ReservationReport<'a, 'r> {
    ns: &'a Namespace<'r>,
    buf: Option<&'a mut [u8]>,
    eds: bool,
    timeout_ms: u32,
}

impl<'a, 'r> ReservationReport<'a, 'r> {
    pub(crate) fn new(ns: &'a Namespace<'r>) -> Self {
        ReservationReport {
            ns,
            buf: None,
            eds: false,
            timeout_ms: 0,
        }
    }

    /// Read the report into this buffer. Must be at least
    /// `size_of::<nvme_resv_status>()` bytes; bigger buffers will read
    /// the variable controller-entry tail too.
    pub fn into(mut self, buf: &'a mut [u8]) -> Self {
        self.buf = Some(buf);
        self
    }

    /// Request the Extended Data Structure (64-bit reservation keys
    /// instead of the legacy 16-bit registrant IDs).
    pub fn extended(mut self) -> Self {
        self.eds = true;
        self
    }

    pub fn timeout_ms(mut self, ms: u32) -> Self {
        self.timeout_ms = ms;
        self
    }

    /// Execute against the user-provided buffer.
    pub fn execute(self) -> Result<u32> {
        let buf = self.buf.ok_or(Error::InvalidArgument(
            "ReservationReport requires a buffer via .into()",
        ))?;
        if buf.len() < std::mem::size_of::<nvme_resv_status>() {
            return Err(Error::InvalidArgument(
                "ReservationReport buffer smaller than nvme_resv_status header",
            ));
        }
        let fd = ns_fd(self.ns)?;
        let mut result: u32 = 0;
        let mut args = nvme_resv_report_args {
            result: &mut result,
            report: buf.as_mut_ptr() as *mut nvme_resv_status,
            args_size: std::mem::size_of::<nvme_resv_report_args>() as i32,
            fd,
            timeout: self.timeout_ms,
            nsid: self.ns.nsid(),
            len: buf.len() as u32,
            eds: self.eds,
        };
        // SAFETY: args is fully-initialized on the stack; report points
        // to a caller-owned buffer of `len` bytes (verified above).
        let ret = unsafe { nvme_resv_report(&mut args) };
        check_ret(ret)?;
        Ok(result)
    }

    /// Convenience: allocate a 4 KiB buffer and execute. Returns the
    /// raw bytes; callers cast to `nvme_resv_status` themselves.
    pub fn execute_to_vec(self) -> Result<Vec<u8>> {
        let eds = self.eds;
        let timeout_ms = self.timeout_ms;
        let ns = self.ns;
        let mut buf = vec![0u8; 4096];
        ReservationReport {
            ns,
            buf: Some(&mut buf),
            eds,
            timeout_ms,
        }
        .execute()?;
        Ok(buf)
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn ns_fd(ns: &Namespace<'_>) -> Result<std::os::raw::c_int> {
    // SAFETY: ns.raw_handle() is a non-null nvme_ns_t tied to the Root tree
    // via 'r; libnvme opens the device lazily and returns -1 on failure.
    let fd = unsafe { libnvme_sys::nvme_ns_get_fd(ns.raw_handle()) };
    if fd < 0 {
        return Err(Error::Os(std::io::Error::last_os_error()));
    }
    Ok(fd)
}

// Silence unused-import lints when `c_void` is only referenced through
// type-system equivalences (kept for future buffer-bearing report variants).
#[allow(dead_code)]
const _C_VOID_HINT: Option<*const c_void> = None;