Skip to content

ext4: CreateFilesystem fails on multi-GB partitions ("cannot allocate more than 65535 blocks in a single extent") #402

@eriknordmark

Description

@eriknordmark

Summary

Disk.CreateFilesystem with FSType: filesystem.TypeExt4 fails on partitions larger than ~256 MiB with:

could not initialize journal: cannot allocate more than 65535 blocks in a single extent

The error originates at filesystem/ext4/ext4.go:2283:

if extraBlockCount > maxUint16 {
    return nil, fmt.Errorf("cannot allocate more than %d blocks in a single extent", maxUint16)
}

For a 4 KiB-block filesystem this caps a single allocation at 65535 × 4 KiB ≈ 256 MiB. Journal sizing for larger filesystems exceeds that, so creation aborts.

Reproducer

The cleanest repro I've seen is diskfs/partitionresizer's TestRun (https://github.com/diskfs/partitionresizer), which grows existing 500 MiB ext4 partitions to 2 GiB and calls Disk.CreateFilesystem for each grown partition. It fails on the first 2 GiB ext4 create:

... copying data from original partition 2 to new partition 5
Run failed: failed to create ext4 filesystem for new partition parta: could not initialize journal: cannot allocate more than 65535 blocks in a single extent

A minimal standalone repro would be:

d, _ := diskfs.Open("/tmp/2gb.img", ...)
_, err := d.CreateFilesystem(disk.FilesystemSpec{
    Partition: 1,
    FSType:    filesystem.TypeExt4,
})
// err: "could not initialize journal: cannot allocate more than 65535 blocks in a single extent"

against a disk image where partition 1 is 2 GiB.

Observations

The surrounding loop at ext4.go:2270-2305 already iterates over block groups and appends to an extents slice, suggesting multi-extent allocation was intended. The early extraBlockCount > maxUint16 check at line 2282 short-circuits that before the multi-extent path can be exercised. Capping extentLength at maxBlocksPerExtent per iteration (which the code already does at line 2295) and letting the outer loop accumulate multiple extents would seem to be the intended shape.

I don't have a tested fix to attach, but happy to follow up if a maintainer confirms the intended approach.

Impact

Any caller that needs to create an ext4 filesystem larger than ~256 MiB via go-diskfs is blocked. Tools that grow partitions and re-format (such as partitionresizer) inherit the failure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions