Re: [RFC PATCH v13 06/10] ocfs2: rework the direct write context for multi-cluster mappings
From: Joseph Qi
Date: Mon Nov 30 2026 - 07:00:14 EST
On 11/30/26 6:28 PM, sashiko-bot@xxxxxxxxxx wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [High] ocfs2: short direct write marks unzeroed partial cluster
> written in ocfs2_dio_end_io_write()
> --
>
> commit 25ecce6352ba3b041ae3cd99d8a7842789bba66e
> Author: Heming Zhao <heming.zhao@xxxxxxxx>
>
> ocfs2: rework the direct write context for multi-cluster mappings
>
> This commit reworks ocfs2_write_ctxt and unwritten extent tracking to
> handle multi-cluster ranges for direct writes instead of mapping one
> cluster at a time.
>
>> diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
>> index 21cdc5c29331d..e3e28584e7410 100644
>> --- a/fs/ocfs2/aops.c
>> +++ b/fs/ocfs2/aops.c
> [ ... ]
>> @@ -1084,7 +1182,85 @@ static int ocfs2_prepare_folio_for_write(struct inode *inode, u64 *p_blkno,
> [ ... ]
>> +static int ocfs2_grab_folios_direct(struct address_space *mapping,
>> + struct ocfs2_write_ctxt *wc, loff_t user_pos, unsigned user_len)
>> +{
> [ ... ]
>> + for (i = 0; i < ppc; i++) {
>> + unsigned long index = start + i;
>> + struct folio *folio;
>> +
>> + if (index >= last_index)
>> + break;
>> +
>> + /* Track the slots in use, for the unlock/commit loops. */
>> + wc->w_num_folios = max_t(unsigned int, wc->w_num_folios,
>> + win * ppc + i + 1);
>> +
>> + /* The direct io target has no page cache page here. */
>> + if (index >= target_index && index <= end_index)
>> + continue;
> [ ... ]
>> @@ -2151,9 +2747,222 @@ static int ocfs2_write_end(const struct kiocb *iocb,
>> return ret;
>> }
>>
>> +/*
>> + * Convert the unwritten extents of a direct write, which is finished,
>> + * and change the file size if it has been extended.
>> + *
>> + * @head are the clusters the write has zeroed or allocated, recorded by
>> + * ocfs2_dio_wr_map_blocks(). Those which are not covered by the data
>> + * written, [offset, offset + bytes), are left unwritten.
>> + */
>> +static int ocfs2_dio_end_io_write(struct inode *inode,
>> + loff_t offset,
>> + ssize_t bytes,
>> + struct list_head *head)
>> +{
>> + struct ocfs2_cached_dealloc_ctxt dealloc;
>> + struct ocfs2_extent_tree et;
>> + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
>> + struct ocfs2_inode_info *oi = OCFS2_I(inode);
>> + struct ocfs2_unwritten_extent *ue;
>> + struct buffer_head *di_bh = NULL;
>> + struct ocfs2_dinode *di;
>> + struct ocfs2_alloc_context *data_ac = NULL;
>> + struct ocfs2_alloc_context *meta_ac = NULL;
>> + handle_t *handle = NULL;
>> + loff_t end = offset + bytes;
>> + int ret = 0, credits = 0, batch = 0, pass;
>> + u32 first_cpos, last_cpos;
>> + u32 extents_to_split = 0;
>> +
>> + first_cpos = offset >> osb->s_clustersize_bits;
>> + last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, end);
>
> [Severity: High]
> Can a short direct write to a hole or unwritten extent within i_size cause
> ocfs2_dio_end_io_write() to mark an unzeroed partial cluster as written,
> exposing stale disk blocks?
>
> When s_clustersize > PAGE_SIZE, ocfs2_grab_folios_direct() skips zeroing
> all pages in [target_index, end_index] and all middle clusters, assuming
> direct I/O will overwrite the entire mapped range [user_pos, user_pos +
> user_len). If the write stops short (for example, pwritev() faults on a
> later iovec after submitting the first page of a cluster), the remaining
> blocks of that cluster in [end, user_pos + user_len) are neither written
> by direct I/O nor zeroed through the page cache. Rounding end up with
> ocfs2_clusters_for_bytes() into last_cpos then converts the entire cluster
> containing end - 1 from OCFS2_EXT_UNWRITTEN to written.
>
This seems a false postive finding.
In this patch the direct write path is still the buffer_head one:
ocfs2_direct_IO() calls __blockdev_direct_IO(), and that core already
zeroes the outstanding mapped blocks when a write faults partway through
a mapping.
Thanks,
Joseph