lamzfs 0.1.1

no_std read-only ZFS reader for UEFI bootloaders (single/mirror/raidz1, unencrypted bpool)
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
// SPDX-License-Identifier: MIT
// Adapted from rzfs @ea41cf0b5b29 (dual GPL-2.0 OR MIT; MIT elected); see NOTICE.

use core::fmt;

#[cfg(feature = "std")]
use std::error;

use crate::phys::{
    BinaryDecodeError, BinaryDecoder, BinaryEncodeError, BinaryEncoder, SECTOR_SHIFT,
};

////////////////////////////////////////////////////////////////////////////////

/** Data Virtual Address.
 *
 * ### Byte layout.
 *
 * - Bytes: 16
 *
 * ```text
 * +----------+------+
 * | Field    | Size |
 * +----------+------+
 * | flags    |    8 |
 * | offset   |    8 |
 * +----------+------+
 * ```
 *
 * ### Bit layout.
 *
 * ```text
 *        6                   5                   4                   3                   2                   1                   0
 *  3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
 * +-------------------------------------------------------------------------------------------------------------------------------+
 * |  padding (8)  |                   vdev (24)                   |    grid (8)   |                   asize (24)                  |
 * +-------------------------------------------------------------------------------------------------------------------------------+
 * |g|                                                         offset (63)                                                         |
 * +-------------------------------------------------------------------------------------------------------------------------------+
 *
 * asize  Dva allocated
 * g      Dva is_gang
 * grid   RAID-Z layout, reserved for future use
 * offset Dva offset
 * vdev   Dva vdev
 * ```
 *
 * ### padding
 *
 * Unused.
 *
 * ### grid
 *
 * `grid` has been present since V1, but is unused.
 */
#[derive(Debug)]
pub struct Dva {
    /** Number of sectors (512 bytes) allocated.
     *
     * - Range is [`Dva::ALLOCATED_MIN`] to [`Dva::ALLOCATED_MAX`].
     * - A [`Dva`] must have at least one allocated sector.
     * - This includes sectors for for RAIDZ parity data, and gang block headers.
     * - The encoded value is limited to 24 bits. When shifted by
     *   [`SECTOR_SHIFT`], this computes to 512 bytes short of 8 GiB.
     */
    pub allocated: u32,

    /** Offset in sectors (512 bytes) from start of the virtual device.
     *
     * - Range is 0 to [`Dva::OFFSET_MAX`].
     * - Note that this does not mean the offset in sectors from the start of
     *   the physical device.
     * - For a single disk, RAID1 (mirror), RAID0 (stripe), or RAID10 virtual
     *   device, this offset is the number of sectors after the `L1` label of
     *   [`crate::phys::Label`].
     * * For a RAIDZ (RAID5, RAID6, RAID7) virtual device, this is the logical
     *   offset into the entire RAIDZ pool, and maps to multiple sectors across
     *   the child virtual devices.
     * * For a stripped RAIDZ (RAID50, RAID60, RAID70), this is the number of
     *   sectors from the start of the child virtual RAIDZ device.
     * - The value is limited to 63 bits. With a [`SECTOR_SHIFT`], this computes
     *   to 512 bytes short of 4 ZiB.
     */
    pub offset: u64,

    /** Is gang block.
     *
     * A gang block contains block pointers to data.
     *
     * TODO: Document.
     */
    pub is_gang: bool,

    /** Virtual device index.
     *
     * - Range is 0 to [`Dva::VDEV_MAX`].
     * - For a single disk, the `vdev` is 0.
     * - For a RAID1 (mirror) pool, the `vdev` is 0, and the mirror module
     *   handles replicating writes to all leaf devices.
     * - For a RAID0 (stripe) pool, the `vdev` specifies the leaf device that
     *   holds all the data for this [`Dva`].
     * - For a RAIDZ pool, the `vdev` is 0, and the raidz module handles reads
     *   and writes to leaf devices.
     * - For a stripped RAIDZ, the `vdev` specifies the child RAIDZ device that
     *   holds all the data for this [`Dva`].
     */
    pub vdev: u32,
}

impl Dva {
    /// Byte length of an encoded [`Dva`].
    pub const SIZE: usize = 16;

    /// Minimum number of allocated sectors.
    pub const ALLOCATED_MIN: u32 = 1;

    /// Maximimum number of allocated sectors.
    pub const ALLOCATED_MAX: u32 = (1 << 24) - 1;

    /// Maximum offset in sectors.
    pub const OFFSET_MAX: u64 = Dva::OFFSET_MASK;

    /// Maximum vdev.
    pub const VDEV_MAX: u32 = (1 << 24) - 1;

    /// Mask for asize field.
    const ASIZE_MASK_DOWN_SHIFTED: u64 = (1 << 24) - 1;

    /// Shift for asize field.
    const ASIZE_SHIFT: usize = 0;

    /// Shift for grid field.
    const GRID_SHIFT: usize = 24;

    /// Mask for vdev field.
    const VDEV_MASK_DOWN_SHIFTED: u64 = (1 << 24) - 1;

    /// Shift for vdev field.
    const VDEV_SHIFT: usize = 32;

    /// Shift for padding field.
    const PADDING_SHIFT: usize = 56;

    /// Mask for gang bit.
    const GANG_MASK_BIT_FLAG: u64 = 0x8000000000000000;

    /// Mask for offset field.
    const OFFSET_MASK: u64 = (1 << 63) - 1;

    /// Gets the byte size of the [`Dva`], or [None] if the size is larger than [usize].
    pub fn get_size(&self) -> Option<usize> {
        if let Ok(size) = usize::try_from(self.allocated) {
            return size.checked_shl(SECTOR_SHIFT);
        }

        None
    }

    /** Decodes a [`Dva`]. Returns [`None`] if [`Dva`] is empty.
     *
     * # Errors
     *
     * Returns [`DvaDecodeError`] in case of decoding error.
     */
    pub fn from_decoder(
        decoder: &mut dyn BinaryDecoder<'_>,
    ) -> Result<Option<Dva>, DvaDecodeError> {
        ////////////////////////////////
        // Decode values.
        let a = decoder.get_u64()?;
        let b = decoder.get_u64()?;

        ////////////////////////////////
        // Check for empty Dva.
        if a == 0 && b == 0 {
            return Ok(None);
        }

        ////////////////////////////////
        // Check for non-zero grid.
        let grid = (a >> Dva::GRID_SHIFT) as u8;
        if grid != 0 {
            return Err(DvaDecodeError::NonZeroGrid { grid });
        }

        ////////////////////////////////
        // Check for non-zero padding.
        let padding = (a >> Dva::PADDING_SHIFT) as u8;
        if padding != 0 {
            return Err(DvaDecodeError::NonZeroPadding { padding });
        }

        ////////////////////////////////
        // Success!
        Ok(Some(Dva {
            vdev: ((a >> Dva::VDEV_SHIFT) & Dva::VDEV_MASK_DOWN_SHIFTED) as u32,
            allocated: ((a >> Dva::ASIZE_SHIFT) & Dva::ASIZE_MASK_DOWN_SHIFTED) as u32,
            offset: (b & Dva::OFFSET_MASK),
            is_gang: (b & Dva::GANG_MASK_BIT_FLAG) != 0,
        }))
    }

    /** Encodes a non empty [`Dva`].
     *
     * # Errors
     *
     * Returns [`DvaEncodeError`] on error.
     */
    pub fn to_encoder(&self, encoder: &mut dyn BinaryEncoder<'_>) -> Result<(), DvaEncodeError> {
        ////////////////////////////////
        // Check vdev.
        if self.vdev > Dva::VDEV_MAX {
            return Err(DvaEncodeError::InvalidVdev { vdev: self.vdev });
        }

        ////////////////////////////////
        // Check allocated.
        if self.allocated > Dva::ALLOCATED_MAX || self.allocated < Dva::ALLOCATED_MIN {
            return Err(DvaEncodeError::InvalidAllocated {
                allocated: self.allocated,
            });
        }

        ////////////////////////////////
        // Check offset.
        if self.offset > Dva::OFFSET_MAX {
            return Err(DvaEncodeError::InvalidOffset {
                offset: self.offset,
            });
        }

        ////////////////////////////////
        // Encode.
        let a = (u64::from(self.vdev) << Dva::VDEV_SHIFT)
            | (0 << Dva::GRID_SHIFT)
            | (u64::from(self.allocated) << Dva::ASIZE_SHIFT);
        let b = (if self.is_gang {
            Dva::GANG_MASK_BIT_FLAG
        } else {
            0
        }) | (self.offset);

        encoder.put_u64(a)?;
        encoder.put_u64(b)?;

        Ok(())
    }

    /** Encodes an empty [`Dva`].
     *
     * # Errors
     *
     * Returns [`DvaEncodeError`] in case of encoding error.
     */
    pub fn empty_to_encoder(encoder: &mut dyn BinaryEncoder<'_>) -> Result<(), DvaEncodeError> {
        Ok(encoder.put_zeros(Dva::SIZE)?)
    }

    /** Encode an `[Option<Dva>`].
     *
     * # Errors
     *
     * Returns [`DvaEncodeError`] in case of encoding error.
     */
    pub fn option_to_encoder(
        dva: &Option<Dva>,
        encoder: &mut dyn BinaryEncoder<'_>,
    ) -> Result<(), DvaEncodeError> {
        match dva {
            Some(v) => v.to_encoder(encoder),
            None => Ok(Dva::empty_to_encoder(encoder)?),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

/// [`Dva`] decode error.
#[derive(Debug)]
pub enum DvaDecodeError {
    /// [`BinaryDecoder`] error.
    Binary {
        /// Error.
        err: BinaryDecodeError,
    },

    /// Invalid offset error.
    InvalidOffset {
        /// Invalid offset value.
        offset: u64,
    },

    /// Non-zero grid.
    NonZeroGrid {
        /// Invalid grid value.
        grid: u8,
    },

    /// Non-zero padding.
    NonZeroPadding {
        /// Invalid padding.
        padding: u8,
    },
}

impl From<BinaryDecodeError> for DvaDecodeError {
    fn from(err: BinaryDecodeError) -> Self {
        DvaDecodeError::Binary { err }
    }
}

impl fmt::Display for DvaDecodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DvaDecodeError::Binary { err } => {
                write!(f, "DVA decode error | {err}")
            }
            DvaDecodeError::InvalidOffset { offset } => {
                write!(f, "DVA decode error, invalid offset {offset}")
            }
            DvaDecodeError::NonZeroGrid { grid } => {
                write!(f, "DVA decode error, non-zero grid {grid}")
            }
            DvaDecodeError::NonZeroPadding { padding } => {
                write!(f, "DVA decode error, non-zero padding {padding}")
            }
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for DvaDecodeError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            DvaDecodeError::Binary { err } => Some(err),
            _ => None,
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

/// [`Dva`] encode error.
#[derive(Debug)]
pub enum DvaEncodeError {
    /// [`BinaryEncoder`] error.
    Binary {
        /// Error.
        err: BinaryEncodeError,
    },

    /// Invalid allocated error.
    InvalidAllocated {
        /// Invalid allocated value.
        allocated: u32,
    },

    /// Invalid offset error.
    InvalidOffset {
        /// Invalid offset value.
        offset: u64,
    },

    /// Invalid vdev error.
    InvalidVdev {
        /// Invalid vdev value.
        vdev: u32,
    },
}

impl From<BinaryEncodeError> for DvaEncodeError {
    fn from(err: BinaryEncodeError) -> Self {
        DvaEncodeError::Binary { err }
    }
}

impl fmt::Display for DvaEncodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DvaEncodeError::Binary { err } => {
                write!(f, "DVA encode error | {err}")
            }
            DvaEncodeError::InvalidAllocated { allocated } => {
                write!(f, "DVA encode error, invalid allocated {allocated}")
            }
            DvaEncodeError::InvalidOffset { offset } => {
                write!(f, "DVA encode error, invalid offset {offset}")
            }
            DvaEncodeError::InvalidVdev { vdev } => {
                write!(f, "DVA encode error, invalid vdev {vdev}")
            }
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for DvaEncodeError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            DvaEncodeError::Binary { err } => Some(err),
            _ => None,
        }
    }
}