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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// SPDX-License-Identifier: MIT
// Adapted from rzfs @ea41cf0b5b29 (dual GPL-2.0 OR MIT; MIT elected); see NOTICE.

use core::fmt;
use core::fmt::Display;

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

use crate::phys::{
    Acl, AclDecodeError, AclEncodeError, BinaryDecodeError, BinaryDecoder, BinaryEncodeError,
    BinaryEncoder,
};

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

/** ZFS Posix Layer (ZPL) Version.
 */
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ZplVersion {
    /// ZPL version 1.
    V1 = 1,

    /// ZPL version 2.
    V2 = 2,

    /// ZPL version 3.
    V3 = 3,

    /// ZPL version 4.
    V4 = 4,

    /// ZPL version 5.
    V5 = 5,
}

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

impl From<ZplVersion> for u64 {
    fn from(val: ZplVersion) -> u64 {
        val as u64
    }
}

impl Display for ZplVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", u64::from(*self))
    }
}

impl TryFrom<u64> for ZplVersion {
    type Error = ZplVersionError;

    /** Try converting from a [`u64`] to a [`ZplVersion`].
     *
     * # Errors
     *
     * Returns [`ZplVersionError`] in case of an unknown [`ZplVersion`].
     */
    fn try_from(version: u64) -> Result<Self, Self::Error> {
        match version {
            1 => Ok(ZplVersion::V1),
            2 => Ok(ZplVersion::V2),
            3 => Ok(ZplVersion::V3),
            4 => Ok(ZplVersion::V4),
            5 => Ok(ZplVersion::V5),
            _ => Err(ZplVersionError::Unknown { version }),
        }
    }
}

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

/// [`ZplVersion`] conversion error.
#[derive(Debug)]
pub enum ZplVersionError {
    /// Unknown [`ZplVersion`].
    Unknown {
        /// Version.
        version: u64,
    },
}

impl fmt::Display for ZplVersionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZplVersionError::Unknown { version } => {
                write!(f, "Unknown ZplVersion {version}")
            }
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for ZplVersionError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

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

/// [`Znode`] file type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum ZnodeFileType {
    /// Fifo (first-in-first-out) pipe.
    Fifo = 1,

    /// Character device.
    Character = 2,

    /// Directory.
    Directory = 4,

    /// Block device.
    Block = 6,

    /// Regular file.
    Regular = 8,

    /// Symbolic link.
    Symlink = 10,

    /// Unix socket.
    Socket = 12,

    /// Solaris IPC door.
    Door = 13,

    /// Solaris event port.
    EventPort = 14,
}

impl Display for ZnodeFileType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZnodeFileType::Fifo => write!(f, "Fifo"),
            ZnodeFileType::Character => write!(f, "Character"),
            ZnodeFileType::Directory => write!(f, "Directory"),
            ZnodeFileType::Block => write!(f, "Block"),
            ZnodeFileType::Regular => write!(f, "Regular"),
            ZnodeFileType::Symlink => write!(f, "Symlink"),
            ZnodeFileType::Socket => write!(f, "Socket"),
            ZnodeFileType::Door => write!(f, "Door"),
            ZnodeFileType::EventPort => write!(f, "EventPort"),
        }
    }
}

impl From<ZnodeFileType> for u8 {
    fn from(val: ZnodeFileType) -> u8 {
        val as u8
    }
}

impl TryFrom<u8> for ZnodeFileType {
    type Error = ZnodeFileTypeError;

    /** Try converting from a [`u8`] to a [`ZnodeFileType`].
     *
     * # Errors
     *
     * Returns [`ZnodeFileTypeError`] in case of an unknown [`ZnodeFileType`].
     */
    fn try_from(file_type: u8) -> Result<Self, Self::Error> {
        match file_type {
            1 => Ok(ZnodeFileType::Fifo),
            2 => Ok(ZnodeFileType::Character),
            4 => Ok(ZnodeFileType::Directory),
            6 => Ok(ZnodeFileType::Block),
            8 => Ok(ZnodeFileType::Regular),
            10 => Ok(ZnodeFileType::Symlink),
            12 => Ok(ZnodeFileType::Socket),
            13 => Ok(ZnodeFileType::Door),
            14 => Ok(ZnodeFileType::EventPort),
            _ => Err(ZnodeFileTypeError::Unknown { file_type }),
        }
    }
}

/// [`ZnodeFileType`] conversion error.
#[derive(Debug)]
pub enum ZnodeFileTypeError {
    /// Unknown [`ZnodeFileType`].
    Unknown {
        /// File type.
        file_type: u8,
    },
}

impl fmt::Display for ZnodeFileTypeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZnodeFileTypeError::Unknown { file_type } => {
                write!(f, "Unknown ZnodeFileType {file_type}")
            }
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for ZnodeFileTypeError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

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

/// [`Znode`] time since January 1st, 1970 (GMT).
#[derive(Debug)]
pub struct ZnodeTime {
    /// Seconds.
    pub seconds: u64,

    /// Nanoseconds, fractional part of a second.
    pub nanoseconds: u64,
}

impl ZnodeTime {
    /** Decodes a [`ZnodeTime`].
     *
     * # Errors
     *
     * Returns [`BinaryDecodeError`] on error.
     */
    pub fn from_decoder(
        decoder: &mut dyn BinaryDecoder<'_>,
    ) -> Result<ZnodeTime, BinaryDecodeError> {
        Ok(ZnodeTime {
            seconds: decoder.get_u64()?,
            nanoseconds: decoder.get_u64()?,
        })
    }

    /** Encodes a [`ZnodeTime`].
     *
     * # Errors
     *
     * Returns [`BinaryEncodeError`] on error.
     */
    pub fn to_encoder(&self, encoder: &mut dyn BinaryEncoder<'_>) -> Result<(), BinaryEncodeError> {
        encoder.put_u64(self.seconds)?;
        encoder.put_u64(self.nanoseconds)?;
        Ok(())
    }
}

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

/// [`Znode`] permission wrapper struct.
pub struct ZnodePermission;

impl ZnodePermission {
    /// [`Znode`] permission bits mask for S_ISUID (setuid).
    pub const SUID: u16 = 0o4000;

    /// [`Znode`] permission bits mask for S_ISGID (setgid).
    pub const SGID: u16 = 0o2000;

    /// [`Znode`] permission bits mask for S_ISVTX (sticky).
    pub const STCK: u16 = 0o1000;

    /// [`Znode`] permission bits mask for S_IRUSR (user read).
    pub const USR_R: u16 = 0o0400;

    /// [`Znode`] permission bits mask for S_IWUSR (user write).
    pub const USR_W: u16 = 0o0200;

    /// [`Znode`] permission bits mask for S_IXUSR (user execute).
    pub const USR_X: u16 = 0o0100;

    /// [`Znode`] permission bits mask for S_IRGRP (group read).
    pub const GRP_R: u16 = 0o0040;

    /// [`Znode`] permission bits mask for S_IWGRP (group write).
    pub const GRP_W: u16 = 0o0020;

    /// [`Znode`] permission bits mask for S_IXGRP (group execute).
    pub const GRP_X: u16 = 0o0010;

    /// [`Znode`] permission bits mask for S_IROTH (other read).
    pub const OTH_R: u16 = 0o0004;

    /// [`Znode`] permission bits mask for S_IWOTH (other write).
    pub const OTH_W: u16 = 0o0002;

    /// [`Znode`] permission bits mask for S_IXOTH (other execute).
    pub const OTH_X: u16 = 0o0001;

    /// [`Znode`] permission bits mask.
    pub const MASK: u16 = 0o7777;
}

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

/** Znode.
 *
 * ### Byte layout.
 *
 * - Bytes: 264
 *
 * ```text
 * +-------------------------------+------+-------------+-------------+
 * | Field                         | Size | ZPL Version | SPA Version |
 * +-------------------------------+------+-------------+-------------+
 * | access time seconds           |    8 |           1 |           1 |
 * | access time nanoseconds       |    8 |           1 |           1 |
 * | modified time seconds         |    8 |           1 |           1 |
 * | modified time nanoseconds     |    8 |           1 |           1 |
 * | change time seconds           |    8 |           1 |           1 |
 * | change time nanoseconds       |    8 |           1 |           1 |
 * | creation time seconds         |    8 |           1 |           1 |
 * | creation time nanoseconds     |    8 |           1 |           1 |
 * | creation transaction group    |    8 |           1 |           1 |
 * | mode                          |    8 |           1 |           1 |
 * | size                          |    8 |           1 |           1 |
 * | parent object id              |    8 |           1 |           1 |
 * | number of links               |    8 |           1 |           1 |
 * | extended attributes object id |    8 |           1 |           1 |
 * | device number                 |    8 |           1 |           1 |
 * | flags                         |    8 |           1 |           1 |
 * | user id                       |    8 |           1 |           1 |
 * | group id                      |    8 |           1 |           1 |
 * | extra_attributes              |    8 |           3 |           9 |
 * | padding                       |   24 |             |             |
 * | acl                           |   88 |           1 |           1 |
 * +-------------------------------+------+-------------+-------------+
 *
 * bit layout of mode:
 *
 *        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
 * +-------------------------------------------------------------------------------------------------------------------------------+
 * |                                          unused (48)                                          |typ (4)|u|g|k|r|w|x|r|w|x|r|w|x|
 * +-------------------------------------------------------------------------------------------------------------------------------+
 * ```
 */
#[derive(Debug)]
pub struct Znode {
    /// Access time.
    pub access_time: ZnodeTime,

    /// Modified time of contents.
    pub modified_time: ZnodeTime,

    /// Change time of metadata.
    pub change_time: ZnodeTime,

    /// Creation time.
    pub creation_time: ZnodeTime,

    /// Creation transaction group.
    pub creation_txg: u64,

    /// Unix-style permission bits, refer to [`ZnodePermission`] constants.
    pub permission_bits: u16,

    /// File type.
    pub file_type: ZnodeFileType,

    /// Size in bytes.
    pub size: u64,

    /// Parent object id.
    pub parent_object_id: u64,

    /// Number of hard links to this file.
    pub num_links: u64,

    /// Object id containing ZAP encoded extended attributes for this object.
    pub xattr_object_id: Option<u64>,

    /// Major minor numbers for block and character devices.
    pub device_number: u64,

    /// ???
    pub flags: u64,

    /// File user owner.
    pub user_id: u64,

    /// File group owner.
    pub group_id: u64,

    /// ???
    pub extra_attributes: u64,

    /// [`Acl`].
    pub acl: Acl,
}

impl Znode {
    /// Byte size of an encoded [`Znode`].
    pub const SIZE: usize = 512;

    /// Padding byte size.
    const PADDING_SIZE: usize = 24;

    /// Mode mask for unknown bits.
    const MODE_UNKNOWN_MASK: u64 = u64::MAX ^ ((1 << 16) - 1);

    /// Mode mask for down shifted [`ZnodeFileType`].
    const MODE_FILE_TYPE_MASK_DOWN_SHIFTED: u64 = 0xf;

    /// Mode shift for [`ZnodeFileType`].
    const MODE_FILE_TYPE_SHIFT: usize = 12;

    /** Decodes a [`Znode`].
     *
     * # Errors
     *
     * Returns [`ZnodeDecodeError`] on error.
     */
    pub fn from_decoder(decoder: &mut dyn BinaryDecoder<'_>) -> Result<Znode, ZnodeDecodeError> {
        ////////////////////////////////
        // Decode values.
        let access_time = ZnodeTime::from_decoder(decoder)?;
        let modified_time = ZnodeTime::from_decoder(decoder)?;
        let change_time = ZnodeTime::from_decoder(decoder)?;
        let creation_time = ZnodeTime::from_decoder(decoder)?;

        let creation_txg = decoder.get_u64()?;

        ////////////////////////////////
        // Decode mode.
        let mode = decoder.get_u64()?;
        if (mode & Znode::MODE_UNKNOWN_MASK) != 0 {
            return Err(ZnodeDecodeError::Mode { mode });
        }
        let permission_bits = (mode & u64::from(ZnodePermission::MASK)) as u16;
        let file_type =
            (mode >> Znode::MODE_FILE_TYPE_SHIFT) & Znode::MODE_FILE_TYPE_MASK_DOWN_SHIFTED;
        let file_type = ZnodeFileType::try_from(file_type as u8)?;

        ////////////////////////////////
        // Decode values.
        let size = decoder.get_u64()?;
        let parent_object_id = decoder.get_u64()?;
        if parent_object_id == 0 {
            return Err(ZnodeDecodeError::MissingParentObjectId {});
        }
        let num_links = decoder.get_u64()?;
        let xattr_object_id = match decoder.get_u64()? {
            0 => None,
            v => Some(v),
        };
        let device_number = decoder.get_u64()?;
        let flags = decoder.get_u64()?;
        let user_id = decoder.get_u64()?;
        let group_id = decoder.get_u64()?;
        let extra_attributes = decoder.get_u64()?;

        decoder.skip_zeros(Znode::PADDING_SIZE)?;

        Ok(Znode {
            access_time,
            modified_time,
            change_time,
            creation_time,
            creation_txg,
            permission_bits,
            file_type,
            size,
            parent_object_id,
            num_links,
            xattr_object_id,
            device_number,
            flags,
            user_id,
            group_id,
            extra_attributes,
            acl: Acl::from_decoder(decoder)?,
        })
    }

    /** Encodes a [`Znode`].
     *
     * # Errors
     *
     * Returns [`ZnodeEncodeError`] on error.
     */
    pub fn to_encoder(&self, encoder: &mut dyn BinaryEncoder<'_>) -> Result<(), ZnodeEncodeError> {
        ////////////////////////////////
        // Encode values.
        self.access_time.to_encoder(encoder)?;
        self.modified_time.to_encoder(encoder)?;
        self.change_time.to_encoder(encoder)?;
        self.creation_time.to_encoder(encoder)?;

        encoder.put_u64(self.creation_txg)?;

        ////////////////////////////////
        // Encode mode.
        if (self.permission_bits & ZnodePermission::MASK) != self.permission_bits {
            return Err(ZnodeEncodeError::Permissions {
                permissions: self.permission_bits,
            });
        }
        let mode = u64::from(u8::from(self.file_type)) << Znode::MODE_FILE_TYPE_SHIFT;
        let mode = mode | u64::from(self.permission_bits);
        encoder.put_u64(mode)?;

        ////////////////////////////////
        // Encode values.
        encoder.put_u64(self.size)?;

        if self.parent_object_id == 0 {
            return Err(ZnodeEncodeError::MissingParentObjectId {});
        }
        encoder.put_u64(self.parent_object_id)?;

        encoder.put_u64(self.num_links)?;
        encoder.put_u64(self.xattr_object_id.unwrap_or(0))?;
        encoder.put_u64(self.device_number)?;
        encoder.put_u64(self.flags)?;
        encoder.put_u64(self.user_id)?;
        encoder.put_u64(self.group_id)?;
        encoder.put_u64(self.extra_attributes)?;

        encoder.put_zeros(Znode::PADDING_SIZE)?;

        self.acl.to_encoder(encoder)?;

        Ok(())
    }
}

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

/// [`Znode`] decode error.
#[derive(Debug)]
pub enum ZnodeDecodeError {
    /// [`Acl`] decode error.
    Acl {
        /// Error.
        err: AclDecodeError,
    },

    /// [`BinaryDecoder`] error.
    Binary {
        /// Error.
        err: BinaryDecodeError,
    },

    /// [`ZnodeFileType`] error.
    FileType {
        /// Error.
        err: ZnodeFileTypeError,
    },

    /// Missing parent object id.
    MissingParentObjectId {},

    /// Unknown mode.
    Mode {
        /// Mode.
        mode: u64,
    },
}

impl From<AclDecodeError> for ZnodeDecodeError {
    fn from(err: AclDecodeError) -> Self {
        ZnodeDecodeError::Acl { err }
    }
}

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

impl From<ZnodeFileTypeError> for ZnodeDecodeError {
    fn from(err: ZnodeFileTypeError) -> Self {
        ZnodeDecodeError::FileType { err }
    }
}

impl fmt::Display for ZnodeDecodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZnodeDecodeError::Acl { err } => {
                write!(f, "Znode decode error | {err}")
            }
            ZnodeDecodeError::Binary { err } => {
                write!(f, "Znode decode error | {err}")
            }
            ZnodeDecodeError::FileType { err } => {
                write!(f, "Znode decode error | {err}")
            }
            ZnodeDecodeError::MissingParentObjectId {} => {
                write!(f, "Znode decode error, missing parent object id")
            }
            ZnodeDecodeError::Mode { mode } => {
                write!(f, "Znode decode error, unknown mode {mode}")
            }
        }
    }
}

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

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

/// [`Znode`] encode error.
#[derive(Debug)]
pub enum ZnodeEncodeError {
    /// [`Acl`] encode error.
    Acl {
        /// Error.
        err: AclEncodeError,
    },

    /// Binary encode error.
    Binary {
        /// Error.
        err: BinaryEncodeError,
    },

    /// Missing parent object id.
    MissingParentObjectId {},

    /// Bad permission bits.
    Permissions {
        /// Permissions.
        permissions: u16,
    },
}

impl From<AclEncodeError> for ZnodeEncodeError {
    fn from(err: AclEncodeError) -> Self {
        ZnodeEncodeError::Acl { err }
    }
}

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

impl fmt::Display for ZnodeEncodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ZnodeEncodeError::Acl { err } => {
                write!(f, "Znode encode error | {err}")
            }
            ZnodeEncodeError::Binary { err } => {
                write!(f, "Znode encode error | {err}")
            }
            ZnodeEncodeError::MissingParentObjectId {} => {
                write!(f, "Znode encode error, missing parent object id")
            }
            ZnodeEncodeError::Permissions { permissions } => {
                write!(f, "Znode encode error, unknown permission {permissions}")
            }
        }
    }
}

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