-
Notifications
You must be signed in to change notification settings - Fork 205
/
v6.rs
2472 lines (2183 loc) · 80.3 KB
/
v6.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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020-2021 Ant Group. All rights reserved.
// Copyright (C) 2020-2021 Alibaba Cloud. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::ffi::{OsStr, OsString};
use std::fmt::Debug;
use std::io::{Read, Result};
use std::mem::size_of;
use std::os::unix::ffi::OsStrExt;
use std::str::FromStr;
use std::sync::Arc;
use lazy_static::lazy_static;
use nydus_storage::device::{BlobFeatures, BlobInfo};
use nydus_storage::meta::{
BlobChunkInfoV1Ondisk, BlobChunkInfoV2Ondisk, BlobCompressionContextHeader,
};
use nydus_storage::{RAFS_MAX_CHUNKS_PER_BLOB, RAFS_MAX_CHUNK_SIZE};
use nydus_utils::crypt::{self, Cipher, CipherContext};
use nydus_utils::{compress, digest, round_up, ByteSize};
use crate::metadata::inode::InodeWrapper;
use crate::metadata::layout::v5::RafsV5ChunkInfo;
use crate::metadata::layout::{MetaRange, RafsXAttrs};
use crate::metadata::{Inode, RafsBlobExtraInfo, RafsStore, RafsSuperFlags, RafsSuperMeta};
use crate::{impl_bootstrap_converter, impl_pub_getter_setter, RafsIoReader, RafsIoWrite};
/// EROFS metadata slot size.
pub const EROFS_INODE_SLOT_SIZE: usize = 1 << EROFS_INODE_SLOT_BITS;
/// Bits of EROFS logical block size.
pub const EROFS_BLOCK_BITS_12: u8 = 12;
/// EROFS logical block size.
pub const EROFS_BLOCK_SIZE_4096: u64 = 1u64 << EROFS_BLOCK_BITS_12;
pub const EROFS_BLOCK_BITS_9: u8 = 9;
/// EROFS logical block size.
pub const EROFS_BLOCK_SIZE_512: u64 = 1u64 << EROFS_BLOCK_BITS_9;
/// Offset of EROFS super block.
pub const EROFS_SUPER_OFFSET: u16 = 1024;
/// Size of EROFS super block.
pub const EROFS_SUPER_BLOCK_SIZE: u16 = 128;
/// Size of extended super block, used for rafs v6 specific fields
pub const EROFS_EXT_SUPER_BLOCK_SIZE: u16 = 256;
/// EROFS device table offset.
pub const EROFS_DEVTABLE_OFFSET: u16 =
EROFS_SUPER_OFFSET + EROFS_SUPER_BLOCK_SIZE + EROFS_EXT_SUPER_BLOCK_SIZE;
/// Offseet for inode format flags: compact or extended.
pub const EROFS_I_VERSION_BIT: u16 = 0;
/// Number of bits for inode format flags.
pub const EROFS_I_VERSION_BITS: u16 = 1;
/// 32-byte on-disk inode
pub const EROFS_INODE_LAYOUT_COMPACT: u16 = 0;
/// 64-byte on-disk inode
pub const EROFS_INODE_LAYOUT_EXTENDED: u16 = 1;
/// Number of bits for inode data layout.
pub const EROFS_I_DATALAYOUT_BITS: u16 = 3;
/// EROFS plain inode.
pub const EROFS_INODE_FLAT_PLAIN: u16 = 0;
/// EROFS inline inode.
pub const EROFS_INODE_FLAT_INLINE: u16 = 2;
/// EROFS chunked inode.
pub const EROFS_INODE_CHUNK_BASED: u16 = 4;
// Magic number for EROFS super block.
const EROFS_SUPER_MAGIC_V1: u32 = 0xE0F5_E1E2;
// Bits of EROFS metadata slot size.
const EROFS_INODE_SLOT_BITS: u8 = 5;
// Bit flag indicating whether the inode is chunked or not.
const EROFS_CHUNK_FORMAT_INDEXES_FLAG: u16 = 0x0020;
// Encoded chunk size (log2(chunk_size) - EROFS_BLOCK_BITS).
const EROFS_CHUNK_FORMAT_SIZE_MASK: u16 = 0x001F;
/// Checksum of superblock, compatible with EROFS versions prior to Linux kernel 5.5.
#[allow(dead_code)]
const EROFS_FEATURE_COMPAT_SB_CHKSUM: u32 = 0x0000_0001;
/// Rafs v6 specific metadata, compatible with EROFS versions since Linux kernel 5.16.
const EROFS_FEATURE_COMPAT_RAFS_V6: u32 = 0x4000_0000;
/// Chunked inode, incompatible with EROFS versions prior to Linux kernel 5.15.
const EROFS_FEATURE_INCOMPAT_CHUNKED_FILE: u32 = 0x0000_0004;
/// Multi-devices, incompatible with EROFS versions prior to Linux kernel 5.16.
const EROFS_FEATURE_INCOMPAT_DEVICE_TABLE: u32 = 0x0000_0008;
/// Size of SHA256 digest string.
const BLOB_SHA256_LEN: usize = 64;
const BLOB_MAX_SIZE_UNCOMPRESSED: u64 = 1u64 << 44;
const BLOB_MAX_SIZE_COMPRESSED: u64 = 1u64 << 40;
/// RAFS v6 superblock on-disk format, 128 bytes.
///
/// The structure is designed to be compatible with EROFS superblock, so the in kernel EROFS file
/// system driver could be used to mount a RAFS v6 image.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct RafsV6SuperBlock {
/// File system magic number
s_magic: u32,
/// Crc32 checksum of the superblock, ignored by Rafs v6.
s_checksum: u32,
/// Compatible filesystem features.
s_feature_compat: u32,
/// Bits of block size, 4K or 512 bytes.
s_blkszbits: u8,
/// Number of extended superblock slots, ignored by Rafs v6.
/// `superblock size = 128(size of RafsV6SuperBlock) + s_extslots * 16`.
s_extslots: u8,
/// Nid of the root directory.
/// `root inode offset = s_meta_blkaddr * 4096 + s_root_nid * 32`.
s_root_nid: u16,
/// Total valid ino #
s_inos: u64,
/// Timestamp of filesystem creation.
s_build_time: u64,
/// Timestamp of filesystem creation.
s_build_time_nsec: u32,
/// Total size of file system in blocks, used for statfs
s_blocks: u32,
/// Start block address of the metadata area.
s_meta_blkaddr: u32,
/// Start block address of the shared xattr area.
s_xattr_blkaddr: u32,
/// 128-bit uuid for volume
s_uuid: [u8; 16],
/// Volume name.
s_volume_name: [u8; 16],
/// Incompatible filesystem feature flags.
s_feature_incompat: u32,
/// A union of `u16` for miscellaneous usage.
s_u: u16,
/// # of devices besides the primary device.
s_extra_devices: u16,
/// Offset of the device table, `startoff = s_devt_slotoff * 128`.
s_devt_slotoff: u16,
/// Padding.
s_reserved: [u8; 38],
}
impl_bootstrap_converter!(RafsV6SuperBlock);
impl RafsV6SuperBlock {
/// Create a new instance of `RafsV6SuperBlock`.
pub fn new() -> Self {
Self::default()
}
/// Load a `RafsV6SuperBlock` from a reader.
pub fn load(&mut self, r: &mut RafsIoReader) -> Result<()> {
let mut buf1 = [0u8; EROFS_SUPER_OFFSET as usize];
r.read_exact(&mut buf1)?;
r.read_exact(self.as_mut())
}
/// Validate the Rafs v6 super block.
pub fn validate(&self, meta_size: u64) -> Result<()> {
if meta_size < EROFS_BLOCK_SIZE_4096 {
return Err(einval!(format!(
"invalid Rafs v6 metadata size: {}",
meta_size
)));
}
let block_size = if self.s_blkszbits == EROFS_BLOCK_BITS_9 {
EROFS_BLOCK_SIZE_512
} else {
EROFS_BLOCK_SIZE_4096
};
if meta_size & (block_size - 1) != 0 {
return Err(einval!(format!(
"invalid Rafs v6 metadata size: bootstrap size {} is not aligned",
meta_size
)));
}
let meta_addr = u32::from_le(self.s_meta_blkaddr) as u64 * block_size;
if meta_addr > meta_size {
return Err(einval!(format!(
"invalid Rafs v6 meta block address 0x{:x}, meta file size 0x{:x}",
meta_addr, meta_size
)));
}
if u32::from_le(self.s_magic) != EROFS_SUPER_MAGIC_V1 {
return Err(einval!(format!(
"invalid EROFS magic number 0x{:x} in Rafsv6 superblock",
u32::from_le(self.s_magic)
)));
}
if self.s_checksum != 0 {
return Err(einval!(format!(
"invalid checksum {} in Rafsv6 superblock",
u32::from_le(self.s_checksum)
)));
}
if self.s_blkszbits != EROFS_BLOCK_BITS_12 && self.s_blkszbits != EROFS_BLOCK_BITS_9 {
return Err(einval!(format!(
"invalid block size bits {} in Rafsv6 superblock",
self.s_blkszbits
)));
}
if self.s_extslots != 0 {
return Err(einval!("invalid extended slots in Rafsv6 superblock"));
}
if self.s_inos == 0 {
return Err(einval!("invalid inode number in Rafsv6 superblock"));
}
if self.s_u != 0 {
return Err(einval!("invalid union field in Rafsv6 superblock"));
}
if self.s_xattr_blkaddr != 0 {
return Err(einval!(
"unsupported shared extended attribute namespace in Rafsv6 superblock"
));
}
// There's a bug in old RAFS v6 images, which has set s_blocks to a fixed value 4096.
if self.s_extra_devices == 0 && self.s_blocks != 0 && u32::from_le(self.s_blocks) != 4096 {
warn!(
"rafs v6 extra devices {}, blocks {}",
self.s_extra_devices, self.s_blocks
);
return Err(einval!("invalid extra device count in Rafsv6 superblock"));
}
let devtable_off =
u16::from_le(self.s_devt_slotoff) as u64 * size_of::<RafsV6Device>() as u64;
if devtable_off != EROFS_DEVTABLE_OFFSET as u64 {
return Err(einval!(format!(
"invalid device table slot offset {} in Rafsv6 superblock",
u16::from_le(self.s_devt_slotoff)
)));
}
let devtable_end = devtable_off + u16::from_le(self.s_extra_devices) as u64;
if devtable_end > meta_size {
return Err(einval!(format!(
"invalid device table slot count {} in Rafsv6 superblock",
u16::from_le(self.s_extra_devices)
)));
}
// s_build_time may be used as compact_inode's timestamp in the future.
// if u64::from_le(self.s_build_time) != 0 || u32::from_le(self.s_build_time_nsec) != 0 {
// return Err(einval!("invalid build time in Rafsv6 superblock"));
// }
if u32::from_le(self.s_feature_incompat)
!= EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | EROFS_FEATURE_INCOMPAT_DEVICE_TABLE
{
return Err(einval!(
"invalid incompatible feature bits in Rafsv6 superblock"
));
}
if u32::from_le(self.s_feature_compat) & EROFS_FEATURE_COMPAT_RAFS_V6
!= EROFS_FEATURE_COMPAT_RAFS_V6
{
return Err(einval!(
"invalid compatible feature bits in Rafsv6 superblock"
));
}
Ok(())
}
/// Check whether it's super block for Rafs v6.
pub fn is_rafs_v6(&self) -> bool {
self.magic() == EROFS_SUPER_MAGIC_V1
}
/// Set number of inodes.
pub fn set_inos(&mut self, inos: u64) {
self.s_inos = inos.to_le();
}
/// Get total inodes count of this Rafs
pub fn inodes_count(&self) -> u64 {
u64::from_le(self.s_inos)
}
/// Set number of logical blocks.
pub fn set_blocks(&mut self, blocks: u32) {
self.s_blocks = blocks.to_le();
}
/// Get root nid.
pub fn root_nid(&self) -> u16 {
u16::from_le(self.s_root_nid)
}
/// Set EROFS root nid.
pub fn set_root_nid(&mut self, nid: u16) {
self.s_root_nid = nid.to_le();
}
/// Get meta block address.
pub fn meta_addr(&self) -> u32 {
u32::from_le(self.s_meta_blkaddr)
}
/// Set EROFS meta block address.
pub fn set_meta_addr(&mut self, meta_addr: u64) {
if self.s_blkszbits == EROFS_BLOCK_BITS_12 {
assert!((meta_addr / EROFS_BLOCK_SIZE_4096) <= u32::MAX as u64);
self.s_meta_blkaddr = u32::to_le((meta_addr / EROFS_BLOCK_SIZE_4096) as u32);
} else if self.s_blkszbits == EROFS_BLOCK_BITS_9 {
assert!((meta_addr / EROFS_BLOCK_SIZE_512) <= u32::MAX as u64);
self.s_meta_blkaddr = u32::to_le((meta_addr / EROFS_BLOCK_SIZE_512) as u32);
} else {
error!("v6: unsupported block bits {}", self.s_blkszbits);
}
}
/// Get device table offset.
pub fn device_table_offset(&self) -> u64 {
u16::from_le(self.s_devt_slotoff) as u64 * size_of::<RafsV6Device>() as u64
}
/// Set bits of block size.
pub fn set_block_bits(&mut self, block_bits: u8) {
assert!(block_bits == EROFS_BLOCK_BITS_12 || block_bits == EROFS_BLOCK_BITS_9);
self.s_blkszbits = block_bits;
}
impl_pub_getter_setter!(magic, set_magic, s_magic, u32);
impl_pub_getter_setter!(extra_devices, set_extra_devices, s_extra_devices, u16);
}
impl RafsStore for RafsV6SuperBlock {
// This method must be called before RafsV6SuperBlockExt::store(), otherwise data written by
// RafsV6SuperBlockExt::store() will be overwritten.
fn store(&self, w: &mut dyn RafsIoWrite) -> Result<usize> {
debug_assert!(
((EROFS_SUPER_OFFSET + EROFS_SUPER_BLOCK_SIZE) as u64) < EROFS_BLOCK_SIZE_4096
);
w.write_all(&[0u8; EROFS_SUPER_OFFSET as usize])?;
w.write_all(self.as_ref())?;
w.write_all(
&[0u8; (EROFS_BLOCK_SIZE_4096 as usize
- (EROFS_SUPER_OFFSET + EROFS_SUPER_BLOCK_SIZE) as usize)],
)?;
Ok(EROFS_BLOCK_SIZE_4096 as usize)
}
}
impl Default for RafsV6SuperBlock {
fn default() -> Self {
debug_assert!(size_of::<RafsV6Device>() == 128);
Self {
s_magic: u32::to_le(EROFS_SUPER_MAGIC_V1),
s_checksum: 0,
s_feature_compat: u32::to_le(EROFS_FEATURE_COMPAT_RAFS_V6),
s_blkszbits: EROFS_BLOCK_BITS_12,
s_extslots: 0u8,
s_root_nid: 0,
s_inos: 0,
s_build_time: 0,
s_build_time_nsec: 0,
s_blocks: u32::to_le(1),
s_meta_blkaddr: 0,
s_xattr_blkaddr: 0,
s_uuid: [0u8; 16],
s_volume_name: [0u8; 16],
s_feature_incompat: u32::to_le(
EROFS_FEATURE_INCOMPAT_CHUNKED_FILE | EROFS_FEATURE_INCOMPAT_DEVICE_TABLE,
),
s_u: 0,
s_extra_devices: 0,
s_devt_slotoff: u16::to_le(EROFS_DEVTABLE_OFFSET / size_of::<RafsV6Device>() as u16),
s_reserved: [0u8; 38],
}
}
}
/// Extended superblock for RAFS v6, 256 bytes
#[repr(C)]
#[derive(Clone, Copy)]
pub struct RafsV6SuperBlockExt {
/// superblock flags
s_flags: u64,
/// offset of blob table
s_blob_table_offset: u64,
/// size of blob table
s_blob_table_size: u32,
/// chunk size
s_chunk_size: u32,
/// offset of chunk table
s_chunk_table_offset: u64,
/// size of chunk table
s_chunk_table_size: u64,
s_prefetch_table_offset: u64,
s_prefetch_table_size: u32,
s_padding: u32,
/// Reserved
s_reserved: [u8; 200],
}
impl_bootstrap_converter!(RafsV6SuperBlockExt);
impl RafsV6SuperBlockExt {
/// Create a new instance `RafsV6SuperBlockExt`.
pub fn new() -> Self {
debug_assert!(size_of::<Self>() == 256);
Self::default()
}
/// Load an `RafsV6SuperBlockExt` from a reader.
pub fn load(&mut self, r: &mut RafsIoReader) -> Result<()> {
r.seek_to_offset((EROFS_SUPER_OFFSET + EROFS_SUPER_BLOCK_SIZE) as u64)?;
r.read_exact(self.as_mut())?;
r.seek_to_offset(EROFS_BLOCK_SIZE_4096 as u64)?;
Ok(())
}
/// Validate the Rafs v6 super block.
pub fn validate(&self, meta_size: u64, meta: &RafsSuperMeta) -> Result<()> {
let mut flags = self.flags();
flags &= RafsSuperFlags::COMPRESSION_NONE.bits()
| RafsSuperFlags::COMPRESSION_LZ4.bits()
| RafsSuperFlags::COMPRESSION_GZIP.bits()
| RafsSuperFlags::COMPRESSION_ZSTD.bits();
if flags.count_ones() != 1 {
return Err(einval!(format!(
"invalid flags {:#x} related to compression algorithm in Rafs v6 extended superblock",
flags
)));
}
let mut flags = self.flags();
flags &= RafsSuperFlags::HASH_BLAKE3.bits() | RafsSuperFlags::HASH_SHA256.bits();
if flags.count_ones() != 1 {
return Err(einval!(format!(
"invalid flags {:#x} related to digest algorithm in Rafs v6 extended superblock",
flags
)));
}
let chunk_size = u32::from_le(self.s_chunk_size) as u64;
if !chunk_size.is_power_of_two()
|| !(EROFS_BLOCK_SIZE_4096..=RAFS_MAX_CHUNK_SIZE).contains(&chunk_size)
{
return Err(einval!("invalid chunk size in Rafs v6 extended superblock"));
}
let devslot_end = meta.blob_device_table_offset + meta.blob_table_size as u64;
let blob_offset = self.blob_table_offset();
let blob_size = self.blob_table_size() as u64;
if blob_offset & (EROFS_BLOCK_SIZE_4096 - 1) != 0
|| blob_offset < EROFS_BLOCK_SIZE_4096
|| blob_offset < devslot_end
|| blob_size % size_of::<RafsV6Blob>() as u64 != 0
|| blob_offset.checked_add(blob_size).is_none()
|| blob_offset + blob_size > meta_size
{
return Err(einval!(format!(
"invalid blob table offset 0x{:x}/size 0x{:x} in Rafs v6 extended superblock",
blob_offset, blob_size
)));
}
let blob_range = MetaRange::new(blob_offset, blob_size, true)?;
let mut chunk_info_tbl_range = None;
if self.chunk_table_size() > 0 {
let chunk_tbl_offset = self.chunk_table_offset();
let chunk_tbl_size = self.chunk_table_size();
if chunk_tbl_offset < EROFS_BLOCK_SIZE_4096
|| chunk_tbl_offset % EROFS_BLOCK_SIZE_4096 != 0
|| chunk_tbl_offset < devslot_end
|| chunk_tbl_size % size_of::<RafsV5ChunkInfo>() as u64 != 0
|| chunk_tbl_offset.checked_add(chunk_tbl_size).is_none()
|| chunk_tbl_offset + chunk_tbl_size > meta_size
{
return Err(einval!(format!(
"invalid chunk table offset 0x{:x}/size 0x{:x} in Rafs v6 extended superblock",
chunk_tbl_offset, chunk_tbl_size
)));
}
let chunk_range = MetaRange::new(chunk_tbl_offset, chunk_tbl_size, true)?;
if blob_range.intersect_with(&chunk_range) {
return Err(einval!(format!(
"blob table intersects with chunk table in Rafs v6 extended superblock",
)));
}
chunk_info_tbl_range = Some(chunk_range);
}
// Legacy RAFS may have zero prefetch table offset but non-zero prefetch table size for
// empty filesystems.
if self.prefetch_table_size() > 0 && self.prefetch_table_offset() != 0 {
let tbl_offset = self.prefetch_table_offset();
let tbl_size = self.prefetch_table_size() as u64;
if tbl_offset < EROFS_BLOCK_SIZE_4096
|| tbl_size % size_of::<u32>() as u64 != 0
|| tbl_offset < devslot_end
|| tbl_offset.checked_add(tbl_size).is_none()
|| tbl_offset + tbl_size > meta_size
{
return Err(einval!(format!(
"invalid prefetch table offset 0x{:x}/size 0x{:x} in Rafs v6 extended superblock",
tbl_offset, tbl_size
)));
}
let prefetch_range = MetaRange::new(tbl_offset, tbl_size, false)?;
if blob_range.intersect_with(&prefetch_range) {
return Err(einval!(format!(
"blob table intersects with prefetch table in Rafs v6 extended superblock",
)));
}
if let Some(chunk_range) = chunk_info_tbl_range.as_ref() {
if chunk_range.intersect_with(&prefetch_range) {
return Err(einval!(format!(
"chunk information table intersects with prefetch table in Rafs v6 extended superblock",
)));
}
}
}
Ok(())
}
/// Set compression algorithm to handle chunk of the Rafs filesystem.
pub fn set_compressor(&mut self, compressor: compress::Algorithm) {
let c: RafsSuperFlags = compressor.into();
self.s_flags &= !RafsSuperFlags::COMPRESSION_NONE.bits();
self.s_flags &= !RafsSuperFlags::COMPRESSION_LZ4.bits();
self.s_flags &= !RafsSuperFlags::COMPRESSION_GZIP.bits();
self.s_flags &= !RafsSuperFlags::COMPRESSION_ZSTD.bits();
self.s_flags |= c.bits();
}
/// Set the `has_xattr` flag for the RAFS filesystem.
pub fn set_has_xattr(&mut self) {
self.s_flags |= RafsSuperFlags::HAS_XATTR.bits();
}
/// Enable explicit Uid/Gid feature.
pub fn set_explicit_uidgid(&mut self) {
self.s_flags |= RafsSuperFlags::EXPLICIT_UID_GID.bits();
}
/// Set flag indicating that chunk digest is inlined in the data blob.
pub fn set_inlined_chunk_digest(&mut self) {
self.s_flags |= RafsSuperFlags::INLINED_CHUNK_DIGEST.bits();
}
/// Enable `tarfs` mode, which directly use a tar stream/file as RAFS data blob and do not
/// generate any blob meta data.
pub fn set_tarfs_mode(&mut self) {
self.s_flags |= RafsSuperFlags::TARTFS_MODE.bits();
}
/// Set message digest algorithm to handle chunk of the Rafs filesystem.
pub fn set_digester(&mut self, digester: digest::Algorithm) {
let c: RafsSuperFlags = digester.into();
self.s_flags &= !RafsSuperFlags::HASH_BLAKE3.bits();
self.s_flags &= !RafsSuperFlags::HASH_SHA256.bits();
self.s_flags |= c.bits();
}
/// Set offset and size of chunk information table.
pub fn set_chunk_table(&mut self, offset: u64, size: u64) {
self.set_chunk_table_offset(offset);
self.set_chunk_table_size(size);
}
/// Set encryption algorithm to encrypt chunks of the Rafs filesystem.
pub fn set_cipher(&mut self, cipher: crypt::Algorithm) {
let c: RafsSuperFlags = cipher.into();
self.s_flags &= !RafsSuperFlags::ENCRYPTION_NONE.bits();
self.s_flags &= !RafsSuperFlags::ENCRYPTION_ASE_128_XTS.bits();
self.s_flags |= c.bits();
}
impl_pub_getter_setter!(
chunk_table_offset,
set_chunk_table_offset,
s_chunk_table_offset,
u64
);
impl_pub_getter_setter!(
chunk_table_size,
set_chunk_table_size,
s_chunk_table_size,
u64
);
impl_pub_getter_setter!(chunk_size, set_chunk_size, s_chunk_size, u32);
impl_pub_getter_setter!(flags, set_flags, s_flags, u64);
impl_pub_getter_setter!(
blob_table_offset,
set_blob_table_offset,
s_blob_table_offset,
u64
);
impl_pub_getter_setter!(blob_table_size, set_blob_table_size, s_blob_table_size, u32);
impl_pub_getter_setter!(
prefetch_table_size,
set_prefetch_table_size,
s_prefetch_table_size,
u32
);
impl_pub_getter_setter!(
prefetch_table_offset,
set_prefetch_table_offset,
s_prefetch_table_offset,
u64
);
}
impl RafsStore for RafsV6SuperBlockExt {
fn store(&self, w: &mut dyn RafsIoWrite) -> Result<usize> {
w.write_all(self.as_ref())?;
w.seek_offset(EROFS_BLOCK_SIZE_4096 as u64)?;
Ok(EROFS_BLOCK_SIZE_4096 as usize - (EROFS_SUPER_OFFSET + EROFS_SUPER_BLOCK_SIZE) as usize)
}
}
impl Default for RafsV6SuperBlockExt {
fn default() -> Self {
Self {
s_flags: 0,
s_blob_table_offset: 0,
s_blob_table_size: 0,
s_chunk_size: 0,
s_chunk_table_offset: 0,
s_chunk_table_size: 0,
s_prefetch_table_offset: 0,
s_prefetch_table_size: 0,
s_padding: u32::to_le(0),
s_reserved: [0u8; 200],
}
}
}
/// Type of EROFS inodes.
#[repr(u8)]
#[allow(non_camel_case_types, dead_code)]
enum EROFS_FILE_TYPE {
/// Unknown file type.
EROFS_FT_UNKNOWN,
/// Regular file.
EROFS_FT_REG_FILE,
/// Directory.
EROFS_FT_DIR,
/// Character device.
EROFS_FT_CHRDEV,
/// Block device.
EROFS_FT_BLKDEV,
/// FIFO pipe.
EROFS_FT_FIFO,
/// Socket.
EROFS_FT_SOCK,
/// Symlink.
EROFS_FT_SYMLINK,
/// Maximum value of file type.
EROFS_FT_MAX,
}
/// Trait to manipulate data fields of on-disk RAFS v6 inodes.
///
/// There are two types of on disk inode formats defined by EROFS:
/// - compact inode with 32-byte data
/// - extended inode with 64-byte data
pub trait RafsV6OndiskInode: RafsStore {
fn set_size(&mut self, size: u64);
fn set_ino(&mut self, ino: u32);
fn set_nlink(&mut self, nlinks: u32);
fn set_mode(&mut self, mode: u16);
fn set_u(&mut self, u: u32);
fn set_uidgid(&mut self, uid: u32, gid: u32);
fn set_mtime(&mut self, _sec: u64, _nsec: u32);
fn set_rdev(&mut self, rdev: u32);
fn set_xattr_inline_count(&mut self, count: u16);
fn set_data_layout(&mut self, data_layout: u16);
/// Set inode data layout format to be PLAIN.
#[inline]
fn set_inline_plain_layout(&mut self) {
self.set_data_layout(EROFS_INODE_FLAT_PLAIN);
}
/// Set inode data layout format to be INLINE.
#[inline]
fn set_inline_inline_layout(&mut self) {
self.set_data_layout(EROFS_INODE_FLAT_INLINE);
}
/// Set inode data layout format to be CHUNKED.
#[inline]
fn set_chunk_based_layout(&mut self) {
self.set_data_layout(EROFS_INODE_CHUNK_BASED);
}
fn format(&self) -> u16;
fn mode(&self) -> u16;
fn size(&self) -> u64;
fn union(&self) -> u32;
fn ino(&self) -> u32;
fn ugid(&self) -> (u32, u32);
fn mtime_s_ns(&self) -> (u64, u32);
fn nlink(&self) -> u32;
fn rdev(&self) -> u32;
fn xattr_inline_count(&self) -> u16;
fn load(&mut self, r: &mut RafsIoReader) -> Result<()>;
}
impl Debug for &dyn RafsV6OndiskInode {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("RafsV6OndiskInode")
.field("format", &self.format())
.field("ino", &self.ino())
.field("mode", &self.mode())
.field("size", &self.size())
.field("union", &self.union())
.field("nlink", &self.nlink())
.field("xattr count", &self.xattr_inline_count())
.finish()
}
}
/// RAFS v6 inode on-disk format, 32 bytes.
///
/// This structure is designed to be compatible with EROFS compact inode format.
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct RafsV6InodeCompact {
/// inode format hints
pub i_format: u16,
pub i_xattr_icount: u16,
pub i_mode: u16,
pub i_nlink: u16,
pub i_size: u32,
pub i_reserved: u32,
/// raw_blkaddr or rdev or rafs_v6_inode_chunk_info
pub i_u: u32,
pub i_ino: u32,
pub i_uid: u16,
pub i_gid: u16,
pub i_reserved2: [u8; 4],
}
impl RafsV6InodeCompact {
pub fn new() -> Self {
Self {
i_format: u16::to_le(EROFS_INODE_LAYOUT_COMPACT | (EROFS_INODE_FLAT_PLAIN << 1)),
i_xattr_icount: 0,
i_mode: 0,
i_nlink: 0,
i_size: 0,
i_reserved: 0,
i_u: 0,
i_ino: 0,
i_uid: 0,
i_gid: 0,
i_reserved2: [0u8; 4],
}
}
}
impl RafsV6OndiskInode for RafsV6InodeCompact {
/// Set file size for inode.
fn set_size(&mut self, size: u64) {
self.i_size = u32::to_le(size as u32);
}
/// Set ino for inode.
fn set_ino(&mut self, ino: u32) {
self.i_ino = ino.to_le();
}
/// Set number of hardlink.
fn set_nlink(&mut self, nlinks: u32) {
self.i_nlink = u16::to_le(nlinks as u16);
}
/// Set file protection mode.
fn set_mode(&mut self, mode: u16) {
self.i_mode = mode.to_le();
}
/// Set the union field.
fn set_u(&mut self, u: u32) {
self.i_u = u.to_le();
}
/// Set uid and gid for the inode.
fn set_uidgid(&mut self, uid: u32, gid: u32) {
self.i_uid = u16::to_le(uid as u16);
self.i_gid = u16::to_le(gid as u16);
}
/// Set last modification time for the inode.
fn set_mtime(&mut self, _sec: u64, _nsec: u32) {}
/// Set real device id.
fn set_rdev(&mut self, _rdev: u32) {}
/// Set xattr inline count.
fn set_xattr_inline_count(&mut self, count: u16) {
self.i_xattr_icount = count.to_le();
}
/// Set inode data layout format.
fn set_data_layout(&mut self, data_layout: u16) {
self.i_format = u16::to_le(EROFS_INODE_LAYOUT_COMPACT | (data_layout << 1));
}
fn format(&self) -> u16 {
u16::from_le(self.i_format)
}
fn mode(&self) -> u16 {
u16::from_le(self.i_mode)
}
fn size(&self) -> u64 {
u32::from_le(self.i_size) as u64
}
fn union(&self) -> u32 {
u32::from_le(self.i_u)
}
fn ino(&self) -> u32 {
u32::from_le(self.i_ino)
}
fn ugid(&self) -> (u32, u32) {
(
u16::from_le(self.i_uid) as u32,
u16::from_le(self.i_gid) as u32,
)
}
fn mtime_s_ns(&self) -> (u64, u32) {
(0, 0)
}
fn nlink(&self) -> u32 {
u16::from_le(self.i_nlink) as u32
}
fn rdev(&self) -> u32 {
0
}
fn xattr_inline_count(&self) -> u16 {
u16::from_le(self.i_xattr_icount)
}
/// Load a `RafsV6InodeCompact` from a reader.
fn load(&mut self, r: &mut RafsIoReader) -> Result<()> {
r.read_exact(self.as_mut())
}
}
impl_bootstrap_converter!(RafsV6InodeCompact);
impl RafsStore for RafsV6InodeCompact {
fn store(&self, w: &mut dyn RafsIoWrite) -> Result<usize> {
// TODO: need to write xattr as well.
w.write_all(self.as_ref())?;
Ok(self.as_ref().len())
}
}
/// RAFS v6 inode on-disk format, 64 bytes.
///
/// This structure is designed to be compatible with EROFS extended inode format.
#[repr(C)]
#[derive(Clone, Copy, Default, Debug)]
pub struct RafsV6InodeExtended {
/// Layout format for of the inode.
pub i_format: u16,
/// Size of extended attributes, in unit of 4Byte
pub i_xattr_icount: u16,
/// Protection mode.
pub i_mode: u16,
i_reserved: u16,
/// Size of the file content.
pub i_size: u64,
/// A `u32` union: raw_blkaddr or `rdev` or `rafs_v6_inode_chunk_info`
pub i_u: u32,
/// Inode number.
pub i_ino: u32,
/// User ID of owner.
pub i_uid: u32,
/// Group ID of owner
pub i_gid: u32,
/// Time of last modification - second part.
pub i_mtime: u64,
/// Time of last modification - nanoseconds part.
pub i_mtime_nsec: u32,
/// Number of links.
pub i_nlink: u32,
i_reserved2: [u8; 16],
}
impl RafsV6InodeExtended {
/// Create a new instance of `RafsV6InodeExtended`.
pub fn new() -> Self {
Self {
i_format: u16::to_le(EROFS_INODE_LAYOUT_EXTENDED | (EROFS_INODE_FLAT_PLAIN << 1)),
i_xattr_icount: 0,
i_mode: 0,
i_reserved: 0,
i_size: 0,
i_u: 0,
i_ino: 0,
i_uid: 0,
i_gid: 0,
i_mtime: 0,
i_mtime_nsec: 0,
i_nlink: 0,
i_reserved2: [0u8; 16],
}
}
}
impl RafsV6OndiskInode for RafsV6InodeExtended {
/// Set file size for inode.
fn set_size(&mut self, size: u64) {
self.i_size = size.to_le();
}
/// Set ino for inode.
fn set_ino(&mut self, ino: u32) {
self.i_ino = ino.to_le();
}
/// Set number of hardlink.
fn set_nlink(&mut self, nlinks: u32) {
self.i_nlink = nlinks.to_le();
}
/// Set file protection mode.
fn set_mode(&mut self, mode: u16) {
self.i_mode = mode.to_le();
}
/// Set the union field.
fn set_u(&mut self, u: u32) {
self.i_u = u.to_le();
}
/// Set uid and gid for the inode.
fn set_uidgid(&mut self, uid: u32, gid: u32) {
self.i_uid = u32::to_le(uid);
self.i_gid = u32::to_le(gid);
}
/// Set last modification time for the inode.
fn set_mtime(&mut self, sec: u64, nsec: u32) {
self.i_mtime = u64::to_le(sec);
self.i_mtime_nsec = u32::to_le(nsec);
}
fn set_rdev(&mut self, rdev: u32) {
self.i_u = rdev.to_le()
}
/// Set xattr inline count.
fn set_xattr_inline_count(&mut self, count: u16) {
self.i_xattr_icount = count.to_le();
}
/// Set inode data layout format.
fn set_data_layout(&mut self, data_layout: u16) {
self.i_format = u16::to_le(EROFS_INODE_LAYOUT_EXTENDED | (data_layout << 1));
}
fn format(&self) -> u16 {
u16::from_le(self.i_format)
}
fn mode(&self) -> u16 {
u16::from_le(self.i_mode)
}
fn size(&self) -> u64 {
u64::from_le(self.i_size)
}
fn union(&self) -> u32 {
u32::from_le(self.i_u)
}