When the block size is smaller than the folio size, mapping an earlier extent in a folio can succeed before mapping a later extent fails. mpage_map_and_submit_extent() submits the mapped buffers after retryable mapping errors, but discards them along with the rest of the folio after fatal errors. Mapped buffers are also left unsubmitted if allocating an io_end_vec for a later extent fails. Besides losing dirty data that could still be submitted, this can expose stale data with dioread_nolock: the mapped ranges remain in io_end's unwritten conversion list and can be converted without their data having been submitted. A failure to map a later extent does not invalidate the mappings already established for earlier buffers. Submit those buffers after a fatal mapping error when the filesystem is not in an emergency state. Also submit them if allocating an io_end_vec for a later extent fails. Buffers still needing mapping remain dirty for a later writeback attempt. Preserve their extent status entries and delayed allocation reservations, and invalidate only the unused folios after the partially submitted folio on fatal errors. If a later attempt encounters a fatal mapping error before making progress, the existing invalidation path can discard the folio. Suggested-by: Zhang Yi Fixes: c8cc88163f40 ("ext4: Add support for blocksize < pagesize in dioread_nolock") Cc: stable@vger.kernel.org Signed-off-by: Julian Sun --- fs/ext4/inode.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 407a60ac6b85..ee51b12e31af 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -2607,6 +2607,7 @@ int progress = 0; ext4_io_end_t *io_end = mpd->io_submit.io_end; struct ext4_io_end_vec *io_end_vec; + bool fatal = false; io_end_vec = ext4_alloc_io_end_vec(io_end); if (IS_ERR(io_end_vec)) @@ -2617,8 +2618,10 @@ if (err < 0) { struct super_block *sb = inode->i_sb; - if (ext4_emergency_state(sb)) - goto invalidate_dirty_pages; + if (ext4_emergency_state(sb)) { + *give_up_on_write = true; + return err; + } /* * Let the uper layers retry transient errors. * In the case of ENOSPC, if ext4_count_free_blocks() @@ -2626,18 +2629,7 @@ */ if ((err == -ENOMEM) || (err == -EAGAIN) || (err == -ENOSPC && ext4_count_free_clusters(sb))) { - /* - * We may have already allocated extents for - * some bhs inside the folio, issue the - * corresponding data to prevent stale data. - */ - if (progress) { - if (mpage_submit_partial_folio(mpd, - partial_folio_end)) - goto invalidate_dirty_pages; - goto update_disksize; - } - return err; + break; } ext4_msg(sb, KERN_CRIT, "Delayed block allocation failed for " @@ -2651,9 +2643,8 @@ "be lost\n"); if (err == -ENOSPC) ext4_print_free_blocks(inode); - invalidate_dirty_pages: - *give_up_on_write = true; - return err; + fatal = true; + break; } progress = 1; /* @@ -2662,10 +2653,24 @@ */ err = mpage_map_and_submit_buffers(mpd); if (err < 0) - goto update_disksize; + break; } while (map->m_len); -update_disksize: + if (err < 0) { + /* + * We may have already allocated extents for some bhs inside + * the folio, issue the corresponding data to prevent stale + * data. + */ + if (progress && mpage_submit_partial_folio(mpd, partial_folio_end)) + fatal = true; + + if (fatal) + *give_up_on_write = true; + if (!progress) + return err; + } + /* * Update on-disk size after IO is submitted. Races with * truncate are avoided by checking i_size under i_data_sem. -- 2.39.5