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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
mod block_header;
mod block_map;
mod commit_block;
mod descriptor_block;
mod revocation_block;
mod superblock;
use crate::Ext4;
use crate::block_index::FsBlockIndex;
use crate::error::Ext4Error;
use crate::inode::Inode;
use block_map::{BlockMap, load_block_map};
use superblock::JournalSuperblock;
#[derive(Debug)]
pub(crate) struct Journal {
block_map: BlockMap,
}
impl Journal {
/// Create an empty journal.
pub(crate) fn empty() -> Self {
Self {
block_map: BlockMap::new(),
}
}
/// Load a journal from the filesystem.
///
/// If the filesystem has no journal, an empty journal is returned.
///
/// Note: ext4 is all little-endian, except for the journal, which
/// is all big-endian.
pub(crate) fn load(fs: &Ext4) -> Result<Self, Ext4Error> {
let Some(journal_inode) = fs.0.superblock.journal_inode else {
// Return an empty journal if this filesystem does not have
// a journal.
return Ok(Self::empty());
};
let journal_inode = Inode::read(fs, journal_inode)?;
let superblock = JournalSuperblock::load(fs, &journal_inode)?;
let block_map = load_block_map(fs, &superblock, &journal_inode)?;
Ok(Self { block_map })
}
/// Map from an absolute block index to a block in the journal.
///
/// If the journal does not contain a replacement for the input
/// block, the input block is returned.
pub(crate) fn map_block_index(
&self,
block_index: FsBlockIndex,
) -> FsBlockIndex {
*self.block_map.get(&block_index).unwrap_or(&block_index)
}
}
#[cfg(all(test, feature = "std"))]
mod tests {
use crate::test_util::load_compressed_filesystem;
use alloc::rc::Rc;
#[test]
fn test_journal() {
let mut fs =
load_compressed_filesystem("test_disk_4k_block_journal.bin.zst");
let test_dir = "/dir500";
// With the journal in place, this directory exists.
assert!(fs.exists(test_dir).unwrap());
// Clear the journal, and verify that the directory no longer exists.
Rc::get_mut(&mut fs.0).unwrap().journal.block_map.clear();
assert!(!fs.exists(test_dir).unwrap());
}
}