Re: [REGRESSION] ext4: silent data loss when a split at I/O completion gets ENOSPC
From: Zhang Yi
Date: Tue Nov 24 2026 - 20:25:25 EST
On 11/24/2026 11:53 PM, Jan Kara wrote:
> On Tue 24-11-26 12:49:19, Zhang Yi wrote:
>> On 11/18/2026 10:06 PM, Jose Fernandez (Anthropic) wrote:
>>> On Wed, Nov 18, 2026 Zhang Yi wrote:
>>>> Could you provide a reproducer? My guess is that high concurrency causes
>>>> the blocks freed by my current process to be taken by I/O issued by
>>>> other processes
>>>
>>> That fits our runs. I'll send you the script off-list.
>>>
>>> - Jose
>>
>>
>> Thank you for the reproducer. After debugging, I confirmed the cause. On
>> each failed endio conversion, the retry commits the journal, but the
>> blocks released by the commit are soon claimed again by the
>> background fallocate loop, so after 3 retries the DIO finally returns
>> ENOSPC.
>>
>> Although Ojaswin's v2 does track the exact ranges when submitting I/Os,
>> we cannot convert the whole extent after zeroing until all in-flight I/Os
>> overlapping this extent have finished, which is too expensive to me. So I
>> think we may still need the split to succeed.
>>
>> Note that we do reserve space for exactly this case. At mount time we
>> set aside s_resv_clusters (2% of the clusters, capped at 4096 clusters),
>> and EXT4_GET_BLOCKS_METADATA_NOFAIL is allowed to use it. The problem is
>> that this reservation is not effective today. s_freeclusters_counter is
>> incremented when blocks are freed, but for journaled frees the blocks
>> only return to the buddy allocator after the freeing transaction
>> commits. Most allocation paths already run inside a transaction, so we
>> cannot force a commit from there, and even between retries the released
>> space can be taken again, as the reproducer shows. So ordinary
>> allocations can currently consume the reserved pool, and endio extent
>> splits can be fooled into thinking space is available. This hurts both
>> endio extent conversion and delalloc writeback.
>>
>> My idea is to make this a real reservation of currently allocatable
>> clusters rather than just a counter check. We can subtract
>> sbi->s_mb_free_pending from free_clusters in ext4_has_free_clusters().
>> Then a successful check no longer counts clusters that cannot be
>> allocated yet, ordinary allocations can no longer eat into the reserved
>> pool, and the endio split can fall back on the reserve without having to
>> force a commit first. Delalloc reservations benefit as well, since
>> writeback is less likely to run into ENOSPC.
>>
>> The downside is that ENOSPC may be returned earlier, e.g. by buffered
>> writes under the same heavy concurrency. I think that is acceptable
>> since the retry is best effort anyway, and ext4_should_retry_alloc()
>> still forces a commit when there are pending frees, so it can cover most
>> scenarios. Even if it does fail, it is better to fail early than to fail
>> after the data has already been written. This is also consistent with
>> the behavior of allocating blocks directly when writing in old
>> dioread_lock mode.
>>
>> With the reproducer, this takes the DIO ENOSPC rate from ~11% to 0, and
>> failed conversion attempts from ~1500-2200 per run to ~0.3 per run.
>> fallocate ENOSPC did not increase noticeably (2.0% before vs 2.2% after).
>> However, the probability of returning ENOSPC for pure append writes
>> increases by about 10%.
>>
>> Does this approach sounds OK?
>
> Yes, it sounds good to me. I was also puzzled why the reserved clusters are
> not effective so thanks for explaining that :)
>
> Honza
Thank you for confirming this idea. Let's continue moving forward in
this direction. :)
Best Regards,
Yi.