btrfs_no_std/core/super_block.rs
1use {
2 crate::{
3 constants::{
4 CSUM_SIZE, FSID_SIZE, LABEL_SIZE, MAX_SYSTEM_CHUNK_ARRAY_SIZE, NUM_BACKUP_ROOTS,
5 },
6 DevItem, RootBackup,
7 },
8 byteorder::LE,
9 num_enum::{IntoPrimitive, TryFromPrimitive},
10 static_assertions::const_assert_eq,
11 strum::EnumIter,
12 zerocopy::{AsBytes, FromBytes, Unaligned, U16, U32, U64},
13};
14
15/// The layout of the superblock. A valid superblock must exist for most btrfs implementations to
16/// mount the filesystem.
17///
18/// The primary superblock is located at [`PRIMARY_SUPERBLOCK_ADDR`].
19///
20/// There are additional copies of the superblock located at [`SUPERBLOCK_ADDRS`], if those addresses
21/// are valid, respectively.
22///
23///
24/// [`PRIMARY_SUPERBLOCK_ADDR`]: crate::constants::PRIMARY_SUPERBLOCK_ADDR
25/// [`SUPERBLOCK_ADDRS`]: crate::constants::SUPERBLOCK_ADDRS
26///
27/// # Resources
28///
29/// * <https://btrfs.wiki.kernel.org/index.php/Data_Structures#btrfs_super_block>
30/// * <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>
31#[derive(Copy, Clone, AsBytes, FromBytes, Unaligned)]
32#[repr(C, packed)]
33pub struct SuperBlock {
34 /// Checksum of everything past this field.
35 pub csum: [u8; CSUM_SIZE],
36
37 /// Filesystem UUID.
38 pub fsid: [u8; FSID_SIZE],
39
40 /// The physical address of this block.
41 pub bytenr: U64<LE>,
42
43 /// Flags
44 pub flags: U64<LE>,
45
46 /// The magic must be equal to `"_BHRfS_M"` in ASCII.
47 pub magic: U64<LE>,
48
49 /// The generation of the superblock. In SSD mode, not all superblocks may be updated, so the
50 /// latest generation superblock should be used.
51 pub generation: U64<LE>,
52
53 /// The logical address of the root tree's root.
54 pub root: U64<LE>,
55
56 /// The logical address of the chunk tree's root.
57 pub chunk_root: U64<LE>,
58
59 /// The logical address of the log tree's root.
60 pub log_root: U64<LE>,
61
62 /// FIXME: find out what this is!
63 pub log_root_transid: U64<LE>,
64
65 /// FIXME: document this!
66 pub total_bytes: U64<LE>,
67
68 pub bytes_used: U64<LE>,
69
70 /// The root directory's object ID, which is typically 6.
71 pub root_dir_objectid: U64<LE>,
72
73 /// The number of devices the current filesystem spans.
74 pub num_devices: U64<LE>,
75
76 /// The size of a sector.
77 pub sectorsize: U32<LE>,
78
79 pub nodesize: U32<LE>,
80
81 /// This is currently unused.
82 pub leafsize: U32<LE>,
83
84 pub stripesize: U32<LE>,
85
86 /// The size of [`sys_chunk_array`] found in the superblock.
87 ///
88 /// [`sys_chunk_array`]: SuperBlock::sys_chunk_array
89 pub sys_chunk_array_size: U32<LE>,
90
91 pub chunk_root_generation: U64<LE>,
92
93 pub compat_flags: U64<LE>,
94
95 /// Only implementations that support these flags can write to the filesystem.
96 pub compat_ro_flags: U64<LE>,
97
98 /// Only implementations that support these flags can use the filesystem.
99 pub incompat_flags: U64<LE>,
100
101 /// The checksum type.
102 ///
103 /// This should correspond with a value from [`ChecksumType`].
104 ///
105 /// [`ChecksumType`]: crate::ChecksumType
106 pub csum_type: U16<LE>,
107
108 pub root_level: u8,
109
110 pub chunk_root_level: u8,
111
112 pub log_root_level: u8,
113
114 pub dev_item: DevItem,
115
116 /// The label represented as a null-terminated UTF-8 string. May not contain `'/'` or `'\\'`.
117 pub label: [u8; LABEL_SIZE],
118
119 pub cache_generation: U64<LE>,
120
121 pub uuid_tree_generation: U64<LE>,
122
123 /// Reserved for extensibility.
124 pub _reserved: [U64<LE>; 30],
125
126 pub sys_chunk_array: [u8; MAX_SYSTEM_CHUNK_ARRAY_SIZE],
127
128 pub super_roots: [RootBackup; NUM_BACKUP_ROOTS],
129
130 pub _unused1: [u8; 565],
131}
132const_assert_eq!(core::mem::size_of::<SuperBlock>(), 4096);
133
134#[derive(Copy, Clone, Debug, Hash, PartialEq, EnumIter, IntoPrimitive, TryFromPrimitive)]
135#[repr(u16)]
136pub enum ChecksumType {
137 CRC32C = 0,
138 XXHASH64 = 1,
139 SHA256 = 2,
140 BLAKE2b = 3,
141}