Skip to content

Allow loading filesystems with the recovery bit set #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Added `Ext4::uuid` to get the filesystem UUID.
* Made the `Corrupt` type opaque. It is no longer possible to `match` on
specific types of corruption.
* Added support for reading filesystems that weren't cleanly unmounted.

## 0.7.0

Expand Down
56 changes: 48 additions & 8 deletions src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,76 @@
// except according to those terms.

mod block_header;
#[expect(unused)] // TODO
mod block_map;
mod commit_block;
mod descriptor_block;
#[expect(unused)] // TODO
mod superblock;

use crate::{Ext4, Ext4Error};
use crate::error::Ext4Error;
use crate::inode::Inode;
use crate::Ext4;
use block_map::{load_block_map, BlockMap};
use superblock::JournalSuperblock;

#[derive(Debug)]
pub(crate) struct Journal {
// TODO: add journal data.
block_map: BlockMap,
}

impl Journal {
/// Create an empty journal.
pub(crate) fn empty() -> Self {
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 {
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());
};

// TODO: actually load the journal.
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 {})
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: u64) -> u64 {
*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());
}
}
22 changes: 22 additions & 0 deletions src/journal/superblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ fn check_incompat_features(
#[cfg(all(test, feature = "std"))]
mod tests {
use super::*;
use crate::test_util::load_compressed_filesystem;

#[test]
fn test_load_journal_superblock() {
let fs =
load_compressed_filesystem("test_disk_4k_block_journal.bin.zst");
let journal_inode =
Inode::read(&fs, fs.0.superblock.journal_inode.unwrap()).unwrap();
let superblock = JournalSuperblock::load(&fs, &journal_inode).unwrap();
assert_eq!(
superblock,
JournalSuperblock {
block_size: 4096,
sequence: 3,
start_block: 289,
uuid: Uuid([
0x6c, 0x48, 0x4f, 0x1b, 0x7f, 0x71, 0x47, 0x4c, 0xa1, 0xf9,
0x3b, 0x50, 0x0c, 0xc1, 0xe2, 0x74
]),
}
);
}

fn write_u32be(bytes: &mut [u8], offset: usize, value: u32) {
bytes[offset..offset + 4].copy_from_slice(&value.to_be_bytes());
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ impl Ext4 {
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps as a follow-up: augment this error to make it clear when the block index has been altered by the journal?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, filed #436

};

let block_index = self.0.journal.map_block_index(block_index);

// The first 1024 bytes are reserved for non-filesystem
// data. This conveniently allows for something like a null
// pointer check.
Expand Down
1 change: 0 additions & 1 deletion src/superblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ fn check_incompat_features(
// relax some of these in the future.
let required_features = IncompatibleFeatures::FILE_TYPE_IN_DIR_ENTRY;
let disallowed_features = IncompatibleFeatures::COMPRESSION
| IncompatibleFeatures::RECOVERY
| IncompatibleFeatures::SEPARATE_JOURNAL_DEVICE
| IncompatibleFeatures::META_BLOCK_GROUPS
| IncompatibleFeatures::MULTIPLE_MOUNT_PROTECTION
Expand Down