|
6 | 6 | // option. This file may not be copied, modified, or distributed
|
7 | 7 | // except according to those terms.
|
8 | 8 |
|
9 |
| -#[expect(unused)] // TODO |
10 | 9 | mod block_header;
|
11 |
| -#[expect(unused)] // TODO |
| 10 | +mod descriptor_block; |
12 | 11 | mod superblock;
|
13 | 12 |
|
14 |
| -use crate::{Ext4, Ext4Error}; |
| 13 | +use crate::checksum::Checksum; |
| 14 | +use crate::inode::Inode; |
| 15 | +use crate::iters::file_blocks::FileBlocks; |
| 16 | +use crate::util::{read_u32be, usize_from_u32}; |
| 17 | +use crate::{CorruptKind, Ext4, Ext4Error}; |
| 18 | +use alloc::collections::BTreeMap; |
| 19 | +use alloc::vec; |
| 20 | +use block_header::{JournalBlockHeader, JournalBlockType}; |
| 21 | +use descriptor_block::{ |
| 22 | + is_descriptor_block_checksum_valid, JournalDescriptorBlockTag, |
| 23 | +}; |
| 24 | +use superblock::JournalSuperblock; |
15 | 25 |
|
16 | 26 | #[derive(Debug)]
|
17 | 27 | pub(crate) struct Journal {
|
18 |
| - // TODO: add journal data. |
| 28 | + block_map: BTreeMap<u64, u64>, |
19 | 29 | }
|
20 | 30 |
|
21 | 31 | impl Journal {
|
22 |
| - /// Create an empty journal. |
23 | 32 | pub(crate) fn empty() -> Self {
|
24 |
| - Self {} |
| 33 | + Self { |
| 34 | + block_map: BTreeMap::new(), |
| 35 | + } |
25 | 36 | }
|
26 | 37 |
|
27 |
| - /// Load a journal from the filesystem. |
| 38 | + /// Load the journal. |
| 39 | + /// |
| 40 | + /// If the filesystem has no journal, an empty journal is returned. |
| 41 | + /// |
| 42 | + /// Note: ext4 is all little-endian, except for the journal, which |
| 43 | + /// is all big-endian. |
28 | 44 | pub(crate) fn load(fs: &Ext4) -> Result<Self, Ext4Error> {
|
29 |
| - let Some(_journal_inode) = fs.0.superblock.journal_inode else { |
| 45 | + let Some(journal_inode) = fs.0.superblock.journal_inode else { |
30 | 46 | // Return an empty journal if this filesystem does not have
|
31 | 47 | // a journal.
|
32 | 48 | return Ok(Self::empty());
|
33 | 49 | };
|
34 | 50 |
|
35 |
| - // TODO: actually load the journal. |
| 51 | + let journal_inode = Inode::read(fs, journal_inode)?; |
| 52 | + let superblock = JournalSuperblock::load(fs, &journal_inode)?; |
36 | 53 |
|
37 |
| - Ok(Self {}) |
| 54 | + // Ensure the journal block size matches the rest of the |
| 55 | + // filesystem. |
| 56 | + let block_size = fs.0.superblock.block_size; |
| 57 | + if superblock.block_size != block_size { |
| 58 | + return Err(CorruptKind::JournalBlockSize.into()); |
| 59 | + } |
| 60 | + |
| 61 | + // Get an iterator over the journal's block indices. |
| 62 | + let journal_block_iter = FileBlocks::new(fs.clone(), &journal_inode)?; |
| 63 | + |
| 64 | + // Skip forward to the start block. |
| 65 | + let mut journal_block_iter = |
| 66 | + journal_block_iter.skip(usize_from_u32(superblock.start_block)); |
| 67 | + |
| 68 | + // TODO: the loop below currently returns an error if something |
| 69 | + // bad is encountered (e.g. a wrong checksum). We should |
| 70 | + // actually still apply valid commits, and just stop reading the |
| 71 | + // journal when bad data is encountered. |
| 72 | + |
| 73 | + let mut block = vec![0; block_size.to_usize()]; |
| 74 | + let mut block_map = BTreeMap::new(); |
| 75 | + let mut uncommitted_block_map = BTreeMap::new(); |
| 76 | + let mut sequence = superblock.sequence; |
| 77 | + while let Some(block_index) = journal_block_iter.next() { |
| 78 | + let block_index = block_index?; |
| 79 | + |
| 80 | + fs.read_from_block(block_index, 0, &mut block)?; |
| 81 | + |
| 82 | + let Some(header) = JournalBlockHeader::read_bytes(&block)? else { |
| 83 | + // Journal block magic is not present, so we've reached |
| 84 | + // the end of the journal. |
| 85 | + break; |
| 86 | + }; |
| 87 | + |
| 88 | + if header.sequence != sequence { |
| 89 | + return Err(CorruptKind::JournalSequence.into()); |
| 90 | + } |
| 91 | + |
| 92 | + if header.block_type == JournalBlockType::DESCRIPTOR { |
| 93 | + if !is_descriptor_block_checksum_valid(&superblock, &block) { |
| 94 | + return Err( |
| 95 | + CorruptKind::JournalDescriptorBlockChecksum.into() |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + let tags = |
| 100 | + JournalDescriptorBlockTag::read_bytes_to_vec(&block[12..]) |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + for tag in &tags { |
| 104 | + let block_index = journal_block_iter |
| 105 | + .next() |
| 106 | + .ok_or(CorruptKind::JournalTruncated)??; |
| 107 | + |
| 108 | + // TODO: is it a good idea to do this here, vs when |
| 109 | + // the data is actually needed? |
| 110 | + // TODO: either way, we definitely shouldn't fail if |
| 111 | + // not committed yet, right? |
| 112 | + let mut checksum = Checksum::new(); |
| 113 | + checksum.update(superblock.uuid.as_bytes()); |
| 114 | + checksum.update_u32_be(sequence); |
| 115 | + fs.read_from_block(block_index, 0, &mut block)?; |
| 116 | + checksum.update(&block); |
| 117 | + if checksum.finalize() != tag.checksum { |
| 118 | + // TODO |
| 119 | + panic!(); |
| 120 | + } |
| 121 | + |
| 122 | + uncommitted_block_map.insert(tag.block_number, block_index); |
| 123 | + } |
| 124 | + } else if header.block_type == JournalBlockType::COMMIT { |
| 125 | + if !is_commit_block_checksum_valid(&superblock, &block) { |
| 126 | + return Err(CorruptKind::JournalCommitBlockChecksum.into()); |
| 127 | + } |
| 128 | + |
| 129 | + // Move the entries from `uncommitted_block_map` to `block_map`. |
| 130 | + block_map.extend(uncommitted_block_map.iter()); |
| 131 | + uncommitted_block_map.clear(); |
| 132 | + |
| 133 | + // TODO: unwrap |
| 134 | + sequence = sequence.checked_add(1).unwrap(); |
| 135 | + } else { |
| 136 | + todo!() |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + Ok(Self { block_map }) |
| 141 | + } |
| 142 | + |
| 143 | + /// Map from an absolute block index to a block in the journal. |
| 144 | + /// |
| 145 | + /// If the journal does not contain a replacement for the input |
| 146 | + /// block, the input block is returned. |
| 147 | + pub(crate) fn map_block_index(&self, block_index: u64) -> u64 { |
| 148 | + *self.block_map.get(&block_index).unwrap_or(&block_index) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +fn is_commit_block_checksum_valid( |
| 153 | + superblock: &JournalSuperblock, |
| 154 | + block: &[u8], |
| 155 | +) -> bool { |
| 156 | + // The kernel documentation says that fields 0xc and 0xd contain the |
| 157 | + // checksum type and size, but this is not correct. If the |
| 158 | + // superblock features include `CHECKSUM_V3`, the type/size fields |
| 159 | + // are both zero. |
| 160 | + |
| 161 | + const CHECKSUM_OFFSET: usize = 16; |
| 162 | + const CHECKSUM_SIZE: usize = 4; |
| 163 | + |
| 164 | + let expected_checksum = read_u32be(block, CHECKSUM_OFFSET); |
| 165 | + |
| 166 | + let mut checksum = Checksum::new(); |
| 167 | + checksum.update(superblock.uuid.as_bytes()); |
| 168 | + checksum.update(&block[..CHECKSUM_OFFSET]); |
| 169 | + checksum.update(&[0; CHECKSUM_SIZE]); |
| 170 | + checksum.update(&block[CHECKSUM_OFFSET + CHECKSUM_SIZE..]); |
| 171 | + |
| 172 | + checksum.finalize() == expected_checksum |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(all(test, feature = "std"))] |
| 176 | +mod tests { |
| 177 | + use crate::test_util::load_compressed_filesystem; |
| 178 | + use alloc::rc::Rc; |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn test_journal() { |
| 182 | + let mut fs = |
| 183 | + load_compressed_filesystem("test_disk_4k_block_journal.bin.zst"); |
| 184 | + |
| 185 | + let test_dir = "/dir500"; |
| 186 | + |
| 187 | + // With the journal in place, this directory exists. |
| 188 | + assert!(fs.exists(test_dir).unwrap()); |
| 189 | + |
| 190 | + // Clear the journal, and verify that the directory no longer exists. |
| 191 | + Rc::get_mut(&mut fs.0).unwrap().journal.block_map.clear(); |
| 192 | + assert!(!fs.exists(test_dir).unwrap()); |
38 | 193 | }
|
39 | 194 | }
|
0 commit comments