Skip to content
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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ streaming import design.
its extents.
- **Fixed format choices**, modeled on e2fsprogs: 4 KiB blocks, 256-byte
inodes, 32-bit block numbers, 32-byte group descriptors, sparse superblocks,
extents, **no journal**. Changing any of these is a design decision, so ask
extents, **a jbd2 journal** (v2 superblock, big-endian, no checksum
features; mke2fs-compatible sizes). Changing any of these is a design decision, so ask
before doing it.
- **Cross-platform.** Windows is the primary target but the code also compiles
on Unix (`#[cfg(unix)]` / `#[cfg(windows)]` in `host/tree.rs`,
Expand Down
9 changes: 7 additions & 2 deletions docs/supported-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ The formatter currently creates valid ext4 images with a 4 KiB block size and
256-byte inodes. It writes sparse-superblock layout (including backups and
group descriptors), block and inode bitmaps, inode tables, the root directory,
and `lost+found`. Every image carries a random filesystem UUID (`s_uuid`),
as `mke2fs` writes one, alongside the random directory-hash seed.
as `mke2fs` writes one, alongside the random directory-hash seed, and every
image of at least 8 MiB a jbd2 journal.

It supports regular files across fragmented block runs, building multi-level
extent trees when needed. Directories have `.` and `..` entries, update parent
Expand Down Expand Up @@ -92,7 +93,11 @@ those made by `mke2fs`, are rejected. The image size is fixed. See
- Image creation runs on Windows; Linux tools are only optional verification.
- Layout is modeled on `e2fsprogs` and written directly using ext4 structures.
- The implementation uses 32-bit block addresses and 32-byte group descriptors.
- Images are deliberately created without a journal.
- Every image of at least 8 MiB carries a jbd2 journal (v2 superblock,
big-endian, no checksum features; mke2fs-compatible sizes), so the guest
kernel recovers a crashed VM by replaying it on mount. Smaller images get
none, mirroring mke2fs. The snapshot reader never replays a journal; a
non-empty one is refused.
- Sparse files, inline/shared xattrs, and SELinux validation are not
implemented yet.
- Host import preserves the portable subset of metadata supported by the host
Expand Down
6 changes: 5 additions & 1 deletion src/ext4/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ pub(crate) struct ExtentTree {
pub(crate) tree_blocks: Vec<u32>,
}

/// Builds initialized leaf extents from physical runs, assigning consecutive logical blocks.
/// Builds initialized leaf extents from physical runs, assigning consecutive
/// logical blocks. Run lengths from `plan_data_blocks` are capped at 32,768
/// blocks (the initialized-extent limit), which is asserted here: a longer
/// run would truncate `ee_len` silently.
pub(super) fn extents_for_runs(runs: &[(u32, u32)]) -> Vec<Ext4Extent> {
let mut logical = 0u32;
runs.iter()
.map(|&(start, len)| {
debug_assert!(len <= u32::from(EXT_INIT_MAX_LEN));
let extent = Ext4Extent {
ee_block: logical,
ee_len: len as u16,
Expand Down
42 changes: 40 additions & 2 deletions src/ext4/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ pub const EXT4_INDEXES_PER_BLOCK: usize =
pub const EXT4_BAD_INO: u32 = 1;
pub const EXT4_ROOT_INO: u32 = 2;
pub const EXT4_FIRST_INO: u32 = 11;
/// The jbd2 journal inode mke2fs places its journal in.
pub const EXT4_JOURNAL_INO: u32 = 8;
/// `s_jnl_backup_type` value: `s_jnl_blocks` holds the journal's first
/// block numbers (e2fsprogs EXT3_JNL_BACKUP_BLOCKS).
pub const EXT3_JNL_BACKUP_BLOCKS: u8 = 1;

// File Types
pub const EXT4_FT_UNKNOWN: u8 = 0;
Expand Down Expand Up @@ -94,6 +99,11 @@ pub const EXT2_ERROR_FS: u16 = 0x0002;
/// part of [`Ext4SuperBlock`] that is kept as opaque padding.
pub const EXT4_SUPERBLOCK_FLAGS_OFFSET: usize = 0x160;
pub const EXT2_FLAGS_UNSIGNED_HASH: u32 = 0x0002;
/// What mke2fs writes: dirhash input bytes are sign-extended, matching
/// this crate's `ext4_dirhash_half_md4`. Without it, the kernel records
/// its own (unsigned) choice in `s_flags` on the first mount, and the
/// strict open then rejects the image as using unsigned hashes.
pub const EXT2_FLAGS_SIGNED_HASH: u32 = 0x0001;

#[repr(C, packed)]
#[derive(Clone, Copy, Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
Expand Down Expand Up @@ -147,7 +157,22 @@ pub struct Ext4SuperBlock {
pub s_def_hash_version: u8,
pub _s_jnl_backup_type: u8,
pub _s_desc_size: u16,
pub _padding: [u8; 768],
pub s_default_mount_opts: u32,
pub s_first_meta_bg: u32,
pub s_mkfs_time: u32,
/// Backup of the journal inode's first block numbers, valid when
/// `s_jnl_backup_type` is set (kernel `s_jnl_blocks[17]`, offset 0x10C).
pub s_jnl_blocks: [u32; 17],
/// 64-bit block counts; this writer uses 32-bit numbers, so these stay 0.
pub s_blocks_count_hi: u32,
pub s_r_blocks_count_hi: u32,
pub s_free_blocks_count_hi: u32,
pub s_min_extra_isize: u16,
pub s_want_extra_isize: u16,
/// Miscellaneous flags. SIGNED_HASH records that dirhash input bytes
/// are sign-extended, which both this crate and the kernel honor.
pub s_flags: u32,
pub _padding: [u8; 668],
}

impl Default for Ext4SuperBlock {
Expand Down Expand Up @@ -206,7 +231,20 @@ impl Default for Ext4SuperBlock {
s_def_hash_version: dirhash::DX_HASH_HALF_MD4,
_s_jnl_backup_type: 0,
_s_desc_size: 0,
_padding: [0; 768],
s_default_mount_opts: 0,
s_first_meta_bg: 0,
s_mkfs_time: 0,
s_jnl_blocks: [0; 17],
s_blocks_count_hi: 0,
s_r_blocks_count_hi: 0,
s_free_blocks_count_hi: 0,
s_min_extra_isize: 0,
s_want_extra_isize: 0,
// Like mke2fs: dirhash bytes are sign-extended (see
// format::dirhash). Without this the kernel records its own
// unsigned choice on first mount.
s_flags: EXT2_FLAGS_SIGNED_HASH,
_padding: [0; 668],
}
}
}
Expand Down
Loading