AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/10/09 13:28 flow patch-triage
7m Args:
null

Results:
{
  "EnableConfigs": [
    "EXT4_FS"
  ],
  "FocusSymbols": [
    "ext4_enable_buffered_iomap",
    "ext4_iomap_buffered_write",
    "ext4_iomap_writepages",
    "ext4_iomap_page_mkwrite",
    "ext4_iomap_end_io",
    "ext4_iomap_end_bio",
    "ext4_iomap_sync_zeroed_eof",
    "ext4_convert_unwritten_extents",
    "ext4_truncate_up",
    "ext4_truncate_down"
  ],
  "KMSANReasoning": "The patch series implements support for the iomap buffered I/O path in ext4 (enabled via the 'buffered_iomap' mount option), introducing iomap address space operations (ext4_iomap_aops), extent mapping helpers, writeback completion and disksize pending synchronization routines, and extent conversion handling.\n\nAnalysis regarding KMSAN vs. KASAN/LOCKDEP:\n1. No uninitialized memory or info-leak risk:\n   - The changes do not expose any kernel data structures, buffers, or padded structs to user space (no copy_to_user, put_user, netlink skb generation, or ioctl output structs).\n   - All stack variables (e.g., `struct ext4_map_blocks map`, `struct iomap_writepage_ctx wpc`) are properly initialized: `wpc` uses designated initialization zeroing all unspecified fields; `ext4_map_blocks` and `ext4_da_map_blocks` explicitly zero `map-\u003em_flags` and assign `map-\u003em_seq` / `map-\u003em_pblk` before use; helper functions like `ext4_iomap_get_disksize_pending_range()` initialize the out-parameter whenever a non-zero length is returned, and callers strictly check the return value before accessing the out-parameter.\n2. KASAN and LOCKDEP coverage:\n   - The primary risks of this patch are complex lock ordering changes (between folio locks, journal transaction starts, i_data_sem, and i_rwsem) which are scrutinized by LOCKDEP, and potential bio/ioend lifetime or concurrency bugs (use-after-free or out-of-bounds access) which are covered by standard KASAN.\n\nBecause the changes do not introduce or expose uninitialized memory access or user-space information leaks, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false,
  "Reasoning": "The patch implements buffered I/O via the iomap framework in ext4 (enabled via the 'buffered_iomap' mount option). It adds iomap read/write/writeback/page_mkwrite operations, alters unwritten extent conversion, restructures truncate-up and truncate-down handling, adds disksize-grow-pending synchronization logic, and adds new assertions (WARN_ON_ONCE). This is core filesystem code fully reachable in virtualized environments.",
  "WorthFuzzing": true
}

1/1 2026/10/09 13:28 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 747cfe98d30bc59325d25d500da86aa585c6ddae\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Fri Oct 9 13:28:25 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h\nindex 724a27e8be613..8cfe77b45e21b 100644\n--- a/fs/ext4/ext4.h\n+++ b/fs/ext4/ext4.h\n@@ -1208,8 +1208,10 @@ struct ext4_inode_info {\n \t/* Lock protecting lists below */\n \tspinlock_t i_completed_io_lock;\n \t/*\n-\t * Completed IOs that need unwritten extents handling and have\n-\t * transaction reserved\n+\t * Completed IOs that need unwritten extents handling and have a\n+\t * transaction reserved for the buffer_head writeback path, and\n+\t * also used by the iomap writeback path to queue ioends needing\n+\t * unwritten extents conversion, i_disksize update, etc.\n \t */\n \tstruct list_head i_rsv_conversion_list;\n \tstruct work_struct i_rsv_conversion_work;\n@@ -1319,6 +1321,7 @@ struct ext4_inode_info {\n \t\t\t\t\t\t    * scanning in mballoc\n \t\t\t\t\t\t    */\n #define EXT4_MOUNT2_ABORT\t\t0x00000100 /* Abort filesystem */\n+#define EXT4_MOUNT2_BUFFERED_IOMAP\t0x00000200 /* Use iomap for buffered I/O */\n \n #define clear_opt(sb, opt)\t\tEXT4_SB(sb)-\u003es_mount_opt \u0026= \\\n \t\t\t\t\t\t~EXT4_MOUNT_##opt\n@@ -2049,6 +2052,10 @@ enum {\n \tEXT4_STATE_FC_FLUSHING_DATA,\t/* Fast commit flushing data */\n \tEXT4_STATE_ORPHAN_FILE,\t\t/* Inode orphaned in orphan file */\n \tEXT4_STATE_FC_REQUEUE,\t\t/* Inode modified during fast commit */\n+\tEXT4_STATE_BUFFERED_IOMAP,\t/* Inode use iomap for buffered IO */\n+\tEXT4_STATE_DISKSIZE_GROW_PENDING,\n+\t\t\t\t\t/* Has zeroed EOF block straddles\n+\t\t\t\t\t * i_disksize awaiting writeback */\n };\n \n #define EXT4_INODE_BIT_FNS(name, field, offset)\t\t\t\t\\\n@@ -2148,6 +2155,12 @@ static inline struct mapping_metadata_bhs *ext4_i_metadata_bhs(\n \treturn READ_ONCE(EXT4_I(inode)-\u003ei_metadata_bhs);\n }\n \n+/* Whether the inode pass through the iomap infrastructure for buffered I/O */\n+static inline bool ext4_inode_buffered_iomap(struct inode *inode)\n+{\n+\treturn ext4_test_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\n+}\n+\n /*\n  * Codes for operating systems\n  */\n@@ -3158,6 +3171,8 @@ int ext4_walk_page_buffers(handle_t *handle,\n int do_journal_get_write_access(handle_t *handle, struct inode *inode,\n \t\t\t\tstruct buffer_head *bh);\n void ext4_set_inode_mapping_order(struct inode *inode);\n+void ext4_enable_buffered_iomap(struct inode *inode);\n+int ext4_nonda_switch(struct super_block *sb);\n #define FALL_BACK_TO_NONDELALLOC 1\n #define EXT4_WRITE_DATA_INLINE\t 2\n \n@@ -3209,6 +3224,12 @@ extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);\n extern int ext4_chunk_trans_extent(struct inode *inode, int nrblocks);\n extern int ext4_meta_trans_blocks(struct inode *inode, int lblocks,\n \t\t\t\t  int pextents, int alloc_extents);\n+void ext4_iomap_clear_disksize_pending(struct inode *inode);\n+void ext4_iomap_wait_disksize_pending(struct inode *inode);\n+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,\n+\t\t\t\t\t\t   loff_t *start);\n+extern int ext4_iomap_sync_zeroed_eof(struct inode *inode,\n+\t\t\t\t      loff_t offset, loff_t end);\n extern int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end);\n \n #define EXT4_PARTIAL_ZERO_START\t0x1\n@@ -3588,30 +3609,67 @@ do {\t\t\t\t\t\t\t\t\\\n #define EXT4_FREECLUSTERS_WATERMARK 0\n #endif\n \n-/* Update i_disksize. Requires i_rwsem to avoid races with truncate */\n+/*\n+ * Update i_disksize. Requires i_rwsem to avoid races with truncate.\n+ *\n+ * In the iomap buffered I/O path, the EXT4_STATE_DISKSIZE_GROW_PENDING\n+ * inode state bit indicates that the zeroed EOF partial block which\n+ * straddles i_disksize is still waiting writeback.  In that case,\n+ * i_disksize will be updated after the pending zeroed data has been\n+ * written out.\n+ */\n static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)\n {\n \tWARN_ON_ONCE(S_ISREG(inode-\u003ei_mode) \u0026\u0026\n \t\t     !inode_is_locked(inode));\n \tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n-\tif (newsize \u003e EXT4_I(inode)-\u003ei_disksize)\n+\tif (newsize \u003e EXT4_I(inode)-\u003ei_disksize \u0026\u0026\n+\t    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))\n \t\tWRITE_ONCE(EXT4_I(inode)-\u003ei_disksize, newsize);\n \tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n }\n \n-/* Update i_size, i_disksize. Requires i_rwsem to avoid races with truncate */\n+static inline void __ext4_set_i_disksize(struct inode *inode, loff_t newsize)\n+{\n+\tWARN_ON_ONCE(!rwsem_is_locked(\u0026EXT4_I(inode)-\u003ei_data_sem));\n+\n+\tif (newsize \u003c EXT4_I(inode)-\u003ei_disksize ||\n+\t    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))\n+\t\tWRITE_ONCE(EXT4_I(inode)-\u003ei_disksize, newsize);\n+}\n+\n+/*\n+ * Update i_size and i_disksize to @newsize.  Requires i_rwsem to avoid\n+ * races with truncate.\n+ *\n+ * In the iomap buffered I/O path, i_disksize is updated only if no zeroed\n+ * pending block straddles i_disksize (EXT4_STATE_DISKSIZE_GROW_PENDING\n+ * clear), otherwise the ioend worker for the pending block will advance\n+ * i_disksize once the pending block is written back.  Both updates happen\n+ * under i_data_sem so that the writeback ioend worker can always see the\n+ * latest i_size under the same semaphore.\n+ *\n+ * Returns 0 if nothing changed, 1 if i_size was raised, 2 if i_disksize\n+ * was raised, or 3 if both were.\n+ */\n static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)\n {\n \tint changed = 0;\n \n+\tif (newsize \u003c= inode-\u003ei_size \u0026\u0026 newsize \u003c= EXT4_I(inode)-\u003ei_disksize)\n+\t\treturn 0;\n+\n+\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n \tif (newsize \u003e inode-\u003ei_size) {\n \t\ti_size_write(inode, newsize);\n \t\tchanged = 1;\n \t}\n-\tif (newsize \u003e EXT4_I(inode)-\u003ei_disksize) {\n-\t\text4_update_i_disksize(inode, newsize);\n+\tif (newsize \u003e EXT4_I(inode)-\u003ei_disksize \u0026\u0026\n+\t    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING)) {\n+\t\tWRITE_ONCE(EXT4_I(inode)-\u003ei_disksize, newsize);\n \t\tchanged |= 2;\n \t}\n+\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n \treturn changed;\n }\n \n@@ -3969,6 +4027,12 @@ extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,\n \t\t\t     __u64 len, __u64 *moved_len);\n \n /* page-io.c */\n+/*\n+ * The I/O range covers the zeroed EOF block that straddles i_disksize\n+ * and will advance it upon completion.\n+ */\n+#define EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO\t1UL\n+\n extern int __init ext4_init_pageio(void);\n extern void ext4_exit_pageio(void);\n extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);\n@@ -3983,6 +4047,8 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *page,\n \t\tsize_t len);\n extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);\n extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);\n+extern void ext4_iomap_end_io(struct work_struct *work);\n+extern void ext4_iomap_end_bio(struct bio *bio);\n \n /* mmp.c */\n extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);\n@@ -4040,6 +4106,9 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)\n \n extern const struct iomap_ops ext4_iomap_ops;\n extern const struct iomap_ops ext4_iomap_report_ops;\n+extern const struct iomap_ops ext4_iomap_buffered_write_ops;\n+extern const struct iomap_ops ext4_iomap_buffered_da_write_ops;\n+extern const struct iomap_write_ops ext4_iomap_write_ops;\n \n int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,\n \t\tunsigned flags, struct iomap *iomap, struct iomap *srcmap);\ndiff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c\nindex 53ddedb52a6f7..a4664ddecdcd6 100644\n--- a/fs/ext4/ext4_jbd2.c\n+++ b/fs/ext4/ext4_jbd2.c\n@@ -17,8 +17,12 @@ int ext4_inode_journal_mode(struct inode *inode)\n \t    test_opt(inode-\u003ei_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||\n \t    (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) \u0026\u0026\n \t    !test_opt(inode-\u003ei_sb, DELALLOC))) {\n-\t\t/* We do not support data journalling for encrypted data */\n-\t\tif (S_ISREG(inode-\u003ei_mode) \u0026\u0026 IS_ENCRYPTED(inode))\n+\t\t/*\n+\t\t * We do not support data journalling for encrypted data\n+\t\t * and buffered IOMAP path.\n+\t\t */\n+\t\tif (S_ISREG(inode-\u003ei_mode) \u0026\u0026\n+\t\t    (IS_ENCRYPTED(inode) || ext4_inode_buffered_iomap(inode)))\n \t\t\treturn EXT4_INODE_ORDERED_DATA_MODE;  /* ordered */\n \t\treturn EXT4_INODE_JOURNAL_DATA_MODE;\t/* journal data */\n \t}\ndiff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h\nindex 2fbf48b3dfe21..be54e93bde0b1 100644\n--- a/fs/ext4/ext4_jbd2.h\n+++ b/fs/ext4/ext4_jbd2.h\n@@ -379,7 +379,12 @@ static inline int ext4_should_journal_data(struct inode *inode)\n \n static inline int ext4_should_order_data(struct inode *inode)\n {\n-\treturn ext4_inode_journal_mode(inode) \u0026 EXT4_INODE_ORDERED_DATA_MODE;\n+\t/*\n+\t * inodes using the iomap buffered I/O path do not use the\n+\t * data=ordered mode.\n+\t */\n+\treturn !ext4_inode_buffered_iomap(inode) \u0026\u0026\n+\t\t(ext4_inode_journal_mode(inode) \u0026 EXT4_INODE_ORDERED_DATA_MODE);\n }\n \n static inline int ext4_should_writeback_data(struct inode *inode)\ndiff --git a/fs/ext4/extents.c b/fs/ext4/extents.c\nindex 76038b6c36552..a10d8c6e3cf1f 100644\n--- a/fs/ext4/extents.c\n+++ b/fs/ext4/extents.c\n@@ -4561,7 +4561,7 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)\n \t */\n \n \t/* we have to know where to truncate from in crash case */\n-\tEXT4_I(inode)-\u003ei_disksize = inode-\u003ei_size;\n+\t__ext4_set_i_disksize(inode, inode-\u003ei_size);\n \terr = ext4_mark_inode_dirty(handle, inode);\n \tif (err)\n \t\treturn err;\n@@ -4857,6 +4857,17 @@ static long ext4_zero_range(struct file *file, loff_t offset,\n \t\tif (ret)\n \t\t\treturn ret;\n \t}\n+\n+\t/*\n+\t * In SYNC mode, sync the pending zeroed EOF block to ensure the\n+\t * i_disksize update is persisted.\n+\t */\n+\tif (((file-\u003ef_flags \u0026 O_SYNC) || IS_SYNC(inode)) \u0026\u0026 new_size) {\n+\t\tret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n \t/* Finish zeroing out if it doesn't contain partial block */\n \tif (IS_ALIGNED(offset | end, blocksize))\n \t\treturn ret;\n@@ -4928,10 +4939,20 @@ static long ext4_do_fallocate(struct file *file, loff_t offset,\n \tif (ret)\n \t\tgoto out;\n \n-\tif (((file-\u003ef_flags \u0026 O_SYNC) || IS_SYNC(inode)) \u0026\u0026\n-\t    EXT4_SB(inode-\u003ei_sb)-\u003es_journal) {\n-\t\tret = ext4_fc_commit(EXT4_SB(inode-\u003ei_sb)-\u003es_journal,\n-\t\t\t\t\tEXT4_I(inode)-\u003ei_sync_tid);\n+\tif ((file-\u003ef_flags \u0026 O_SYNC) || IS_SYNC(inode)) {\n+\t\t/*\n+\t\t * Sync the pending zeroed EOF block to ensure the\n+\t\t * i_disksize update is persisted.\n+\t\t */\n+\t\tif (new_size) {\n+\t\t\tret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);\n+\t\t\tif (ret)\n+\t\t\t\tgoto out;\n+\t\t}\n+\t\tif (EXT4_SB(inode-\u003ei_sb)-\u003es_journal) {\n+\t\t\tret = ext4_fc_commit(EXT4_SB(inode-\u003ei_sb)-\u003es_journal,\n+\t\t\t\t\t\tEXT4_I(inode)-\u003ei_sync_tid);\n+\t\t}\n \t}\n out:\n \ttrace_ext4_fallocate_exit(inode, offset,\n@@ -5137,27 +5158,36 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,\n \tint ret = 0, ret2 = 0, ret3 = 0;\n \tstruct ext4_map_blocks map;\n \tunsigned int blkbits = inode-\u003ei_blkbits;\n-\tunsigned int credits = 0;\n+\tunsigned int credits;\n+\tbool internal_handle = !handle;\n \n \tmap.m_lblk = offset \u003e\u003e blkbits;\n \tmap.m_len = max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);\n \n-\tif (!handle) {\n-\t\t/*\n-\t\t * credits to insert 1 extent into extent tree\n-\t\t */\n-\t\tcredits = ext4_chunk_trans_blocks(inode, max_blocks);\n+\t/* Credits to convert one extent in this range to written state. */\n+\tcredits = ext4_meta_trans_blocks(inode, max_blocks, 1, 0);\n+\n+\tif (internal_handle) {\n+\t\thandle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);\n+\t\tif (IS_ERR(handle)) {\n+\t\t\tret = PTR_ERR(handle);\n+\t\t\tgoto out;\n+\t\t}\n \t}\n \n \twhile (max_blocks) {\n-\t\tif (credits) {\n-\t\t\thandle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,\n-\t\t\t\t\t\t    credits);\n-\t\t\tif (IS_ERR(handle)) {\n-\t\t\t\tret = PTR_ERR(handle);\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n+\t\t/*\n+\t\t * The caller cannot know how many extents the range covers,\n+\t\t * so make sure the transaction can take one more extent,\n+\t\t * extending or restarting it once it is full.  The reserved\n+\t\t * handle of the buffer_head writeback path is safe as it has\n+\t\t * enough credits for the conversion, so it is not restarted\n+\t\t * here.\n+\t\t */\n+\t\tret = ext4_journal_ensure_credits(handle, credits, 0);\n+\t\tif (ret \u003c 0)\n+\t\t\tbreak;\n+\n \t\t/*\n \t\t * Do not cache any unrelated extents, as it does not hold the\n \t\t * i_rwsem or invalidate_lock, which could corrupt the extent\n@@ -5177,11 +5207,6 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,\n \t\t}\n \n \t\tret2 = ext4_mark_inode_dirty(handle, inode);\n-\t\tif (credits) {\n-\t\t\tret3 = ext4_journal_stop(handle);\n-\t\t\tif (unlikely(ret3))\n-\t\t\t\tret2 = ret3;\n-\t\t}\n \t\tret = ret \u003c 0 ? ret : ret2;\n \t\tif (ret)\n \t\t\tbreak;\n@@ -5189,6 +5214,13 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,\n \t\tmap.m_lblk += map.m_len;\n \t\tmap.m_len = (max_blocks -= map.m_len);\n \t}\n+\n+\tif (internal_handle) {\n+\t\tret3 = ext4_journal_stop(handle);\n+\t\tif (!ret)\n+\t\t\tret = ret3;\n+\t}\n+out:\n \t/* Converted some or all blocks successfully? */\n \tif (converted)\n \t\t*converted = conv_blocks;\n@@ -5203,8 +5235,8 @@ int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)\n \n \t/*\n \t * This is somewhat ugly but the idea is clear: When transaction is\n-\t * reserved, everything goes into it. Otherwise we rather start several\n-\t * smaller transactions for conversion of each extent separately.\n+\t * reserved, everything goes into it. Otherwise the conversion runs\n+\t * its own transaction, extending or restarting it as it goes.\n \t */\n \tif (handle) {\n \t\thandle = ext4_journal_start_reserved(handle,\n@@ -5662,6 +5694,14 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)\n \tif (end \u003e= inode-\u003ei_size)\n \t\treturn -EINVAL;\n \n+\t/*\n+\t * Persist the pending zeroed EOF block to ensure i_disksize\n+\t * can be safely updated thereafter.\n+\t */\n+\tret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);\n+\tif (ret)\n+\t\treturn ret;\n+\n \t/*\n \t * Write tail of the last page before removed range and data that\n \t * will be shifted since they will get removed from the page cache\n@@ -5711,7 +5751,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)\n \n \tnew_size = inode-\u003ei_size - len;\n \ti_size_write(inode, new_size);\n-\tEXT4_I(inode)-\u003ei_disksize = new_size;\n+\t__ext4_set_i_disksize(inode, new_size);\n \n \tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n \tret = ext4_mark_inode_dirty(handle, inode);\n@@ -5764,6 +5804,14 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)\n \tif (len \u003e inode-\u003ei_sb-\u003es_maxbytes - inode-\u003ei_size)\n \t\treturn -EFBIG;\n \n+\t/*\n+\t * Persist the pending zeroed EOF block to ensure i_disksize\n+\t * can be safely updated thereafter.\n+\t */\n+\tret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);\n+\tif (ret)\n+\t\treturn ret;\n+\n \t/*\n \t * Write out all dirty pages. Need to round down to align start offset\n \t * to page size boundary for page size \u003e block size.\n@@ -5783,8 +5831,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)\n \text4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);\n \n \t/* Expand file to avoid data loss if there is error while shifting */\n-\tinode-\u003ei_size += len;\n-\tEXT4_I(inode)-\u003ei_disksize += len;\n+\text4_update_inode_size(inode, inode-\u003ei_size + len);\n \tret = ext4_mark_inode_dirty(handle, inode);\n \tif (ret)\n \t\tgoto out_handle;\ndiff --git a/fs/ext4/file.c b/fs/ext4/file.c\nindex 374b4bc25bd5d..50d3c92709c8b 100644\n--- a/fs/ext4/file.c\n+++ b/fs/ext4/file.c\n@@ -330,6 +330,21 @@ static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)\n \treturn count;\n }\n \n+static ssize_t ext4_iomap_buffered_write(struct kiocb *iocb,\n+\t\t\t\t\t struct iov_iter *from)\n+{\n+\tstruct inode *inode = file_inode(iocb-\u003eki_filp);\n+\tconst struct iomap_ops *iomap_ops;\n+\n+\tif (test_opt(inode-\u003ei_sb, DELALLOC) \u0026\u0026 !ext4_nonda_switch(inode-\u003ei_sb))\n+\t\tiomap_ops = \u0026ext4_iomap_buffered_da_write_ops;\n+\telse\n+\t\tiomap_ops = \u0026ext4_iomap_buffered_write_ops;\n+\n+\treturn iomap_file_buffered_write(iocb, from, iomap_ops,\n+\t\t\t\t\t \u0026ext4_iomap_write_ops, NULL);\n+}\n+\n static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,\n \t\t\t\t\tstruct iov_iter *from)\n {\n@@ -351,7 +366,10 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,\n \tif (ret \u003c= 0)\n \t\tgoto out;\n \n-\tret = generic_perform_write(iocb, from);\n+\tif (ext4_inode_buffered_iomap(inode))\n+\t\tret = ext4_iomap_buffered_write(iocb, from);\n+\telse\n+\t\tret = generic_perform_write(iocb, from);\n \n out:\n \tinode_unlock(inode);\ndiff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c\nindex a5831fc536db9..f97a2f4904ebb 100644\n--- a/fs/ext4/ialloc.c\n+++ b/fs/ext4/ialloc.c\n@@ -1346,6 +1346,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,\n \t\t}\n \t}\n \n+\text4_enable_buffered_iomap(inode);\n \text4_set_inode_mapping_order(inode);\n \n \text4_update_inode_fsync_trans(handle, inode, 1);\ndiff --git a/fs/ext4/inode.c b/fs/ext4/inode.c\nindex 26f0f9714f03c..1853fbdc2a5af 100644\n--- a/fs/ext4/inode.c\n+++ b/fs/ext4/inode.c\n@@ -44,6 +44,7 @@\n #include \u003clinux/iversion.h\u003e\n \n #include \"ext4_jbd2.h\"\n+#include \"ext4_extents.h\"\n #include \"xattr.h\"\n #include \"acl.h\"\n #include \"truncate.h\"\n@@ -125,6 +126,70 @@ void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,\n \t\traw-\u003ei_checksum_hi = cpu_to_le16(csum \u003e\u003e 16);\n }\n \n+/*\n+ * Clear the disksize-grow-pending state and wake up all waiters.\n+ * Called when the pending zeroed EOF block which straddles i_disksize\n+ * has completed writeback or its folio is discarded.\n+ */\n+void ext4_iomap_clear_disksize_pending(struct inode *inode)\n+{\n+\tif (!test_and_clear_wake_up_bit(\n+\t\text4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),\n+\t\text4_inode_state_wait_word(inode)))\n+\t\treturn;\n+\n+\ttrace_ext4_iomap_clear_disksize_pending(inode);\n+}\n+\n+/*\n+ * Wait for the disksize-grow-pending zeroed EOF block which straddles\n+ * i_disksize to be written back or cleared.\n+ */\n+void ext4_iomap_wait_disksize_pending(struct inode *inode)\n+{\n+\t/* Only emit the trace when the bit is actually set */\n+\tif (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))\n+\t\ttrace_ext4_iomap_wait_disksize_pending(inode);\n+\n+\twait_on_bit(ext4_inode_state_wait_word(inode),\n+\t\t    ext4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),\n+\t\t    TASK_UNINTERRUPTIBLE);\n+}\n+\n+/*\n+ * Get the range of the disksize-grow-pending zeroed EOF block range\n+ * which straddles i_disksize if the EXT4_STATE_DISKSIZE_GROW_PENDING\n+ * bit is set.\n+ *\n+ * Return the pending range, or zero if the BIT has already been cleared.\n+ */\n+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,\n+\t\t\t\t\t\t   loff_t *start)\n+{\n+\tunsigned int blocksize = i_blocksize(inode);\n+\tloff_t disksize;\n+\n+\tif (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))\n+\t\treturn 0;\n+\n+\t/*\n+\t * The pending bit should be set only when i_disksize is not\n+\t * block-size aligned. While set, i_disksize must not be advanced,\n+\t * and the bit must be cleared when i_disksize is shrunk.\n+\t */\n+\tdown_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\tdisksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\tif (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) ||\n+\t    WARN_ON_ONCE(IS_ALIGNED(disksize, blocksize))) {\n+\t\tup_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\t\treturn 0;\n+\t}\n+\n+\tup_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\t*start = disksize;\n+\treturn blocksize - (disksize \u0026 (blocksize - 1));\n+}\n+\n static inline int ext4_begin_ordered_truncate(struct inode *inode,\n \t\t\t\t\t      loff_t new_size)\n {\n@@ -208,6 +273,8 @@ void ext4_evict_inode(struct inode *inode)\n \n \tif (ext4_should_order_data(inode))\n \t\text4_begin_ordered_truncate(inode, 0);\n+\tif (ext4_inode_buffered_iomap(inode))\n+\t\text4_iomap_clear_disksize_pending(inode);\n \ttruncate_inode_pages_final(\u0026inode-\u003ei_data);\n \n \t/*\n@@ -686,6 +753,11 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,\n  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping\n  * based files\n  *\n+ * ext4_map_blocks() may also be called with a NULL @handle. In that case\n+ * it starts its own transaction handle only when block allocation is\n+ * actually needed, i.e. after the lookup confirms the blocks are not yet\n+ * mapped, and stops it before returning.\n+ *\n  * On success, it returns the number of blocks being mapped or allocated.\n  * If flags doesn't contain EXT4_GET_BLOCKS_CREATE the blocks are\n  * pre-allocated and unwritten, the resulting @map is marked as unwritten.\n@@ -703,7 +775,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \tstruct extent_status es;\n \tint retval;\n \tint ret = 0;\n-\tunsigned int orig_mlen = map-\u003em_len;\n+\tbool internal_handle = false;\n+\tunsigned int orig_mlen;\n #ifdef ES_AGGRESSIVE_TEST\n \tstruct ext4_map_blocks orig_map;\n \n@@ -719,6 +792,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \t */\n \tif (unlikely(map-\u003em_len \u003e INT_MAX))\n \t\tmap-\u003em_len = INT_MAX;\n+\torig_mlen = map-\u003em_len;\n \n \t/* We can handle the block number less than EXT_MAX_BLOCKS */\n \tif (unlikely(map-\u003em_lblk \u003e= EXT_MAX_BLOCKS))\n@@ -734,6 +808,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \telse\n \t\text4_check_map_extents_env(inode);\n \n+create_retry:\n \t/* Lookup extent status tree firstly */\n \tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n \t\tif (ext4_es_is_written(\u0026es) || ext4_es_is_unwritten(\u0026es)) {\n@@ -784,12 +859,14 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \tdown_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n \tretval = ext4_map_query_blocks(handle, inode, map, flags);\n \tup_read((\u0026EXT4_I(inode)-\u003ei_data_sem));\n+\tif (retval \u003c 0)\n+\t\tgoto out;\n \n found:\n \tif (retval \u003e 0 \u0026\u0026 map-\u003em_flags \u0026 EXT4_MAP_MAPPED) {\n \t\tret = check_block_validity(inode, map);\n \t\tif (ret != 0)\n-\t\t\treturn ret;\n+\t\t\tgoto out;\n \t}\n \n \t/* If it is only a block(s) look up */\n@@ -802,15 +879,42 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \t * Note that if blocks have been preallocated\n \t * ext4_ext_map_blocks() returns with buffer head unmapped\n \t */\n-\tif (retval \u003e 0 \u0026\u0026 map-\u003em_flags \u0026 EXT4_MAP_MAPPED)\n+\tif (retval \u003e 0) {\n \t\t/*\n-\t\t * If we need to convert extent to unwritten\n-\t\t * we continue and do the actual work in\n-\t\t * ext4_ext_map_blocks()\n+\t\t * If we need to convert written extent to unwritten or\n+\t\t * convert unwritten extent to written, continue and do\n+\t\t * the actual work in ext4_ext_map_blocks().\n \t\t */\n-\t\tif (!(flags \u0026 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))\n-\t\t\treturn retval;\n+\t\tif (map-\u003em_flags \u0026 EXT4_MAP_MAPPED \u0026\u0026\n+\t\t    !(flags \u0026 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))\n+\t\t\tgoto out;\n+\t\tif (map-\u003em_flags \u0026 EXT4_MAP_UNWRITTEN \u0026\u0026\n+\t\t    (flags \u0026 EXT4_GET_BLOCKS_UNWRIT_EXT) \u0026\u0026\n+\t\t    !(flags \u0026 EXT4_GET_BLOCKS_CONVERT)) {\n+\t\t\t/* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */\n+\t\t\tret = check_block_validity(inode, map);\n+\t\t\tif (!ret)\n+\t\t\t\tmap-\u003em_flags |= EXT4_MAP_MAPPED;\n+\t\t\tgoto out;\n+\t\t}\n+\t} else if (retval == 0) {\n+\t\t/*\n+\t\t * Do not allocate blocks for holes in the context of\n+\t\t * data submission path.\n+\t\t */\n+\t\tif (!map-\u003em_flags \u0026\u0026 (flags \u0026 EXT4_GET_BLOCKS_IO_SUBMIT))\n+\t\t\tgoto out;\n+\t}\n \n+\tif (!handle) {\n+\t\thandle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,\n+\t\t\t\text4_chunk_trans_blocks(inode, orig_mlen));\n+\t\tif (IS_ERR(handle)) {\n+\t\t\tret = PTR_ERR(handle);\n+\t\t\tgoto out;\n+\t\t}\n+\t\tinternal_handle = true;\n+\t}\n \n \text4_fc_track_inode(handle, inode);\n \t/*\n@@ -820,18 +924,33 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \t * with create == 1 flag.\n \t */\n \tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\n+\t/*\n+\t * Check the validity of the mapping found via the extent status\n+\t * tree or the disk query. A racing truncate may have changed the\n+\t * extent, since writeback may not hold i_rwsem or the folio locks\n+\t * covering the full extent(e.g., the iomap writeback path may\n+\t * allocate an extent that extends beyond the length of the locked\n+\t * folio in advance).\n+\t */\n+\tif (map-\u003em_seq != READ_ONCE(EXT4_I(inode)-\u003ei_es_seq)) {\n+\t\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\t\tmap-\u003em_flags = 0;\n+\t\tmap-\u003em_len = orig_mlen;\n+\t\tgoto create_retry;\n+\t}\n \tretval = ext4_map_create_blocks(handle, inode, map, flags);\n \tup_write((\u0026EXT4_I(inode)-\u003ei_data_sem));\n \n \tif (retval \u003c 0)\n \t\text_debug(inode, \"failed with err %d\\n\", retval);\n \tif (retval \u003c= 0)\n-\t\treturn retval;\n+\t\tgoto out;\n \n \tif (map-\u003em_flags \u0026 EXT4_MAP_MAPPED) {\n \t\tret = check_block_validity(inode, map);\n \t\tif (ret != 0)\n-\t\t\treturn ret;\n+\t\t\tgoto out;\n \n \t\t/*\n \t\t * Inodes with freshly allocated blocks where contents will be\n@@ -853,12 +972,16 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,\n \t\t\t\tret = ext4_jbd2_inode_add_write(handle, inode,\n \t\t\t\t\t\tstart_byte, length);\n \t\t\tif (ret)\n-\t\t\t\treturn ret;\n+\t\t\t\tgoto out;\n \t\t}\n \t}\n \text4_fc_track_range(handle, inode, map-\u003em_lblk, map-\u003em_lblk +\n \t\t\t    map-\u003em_len - 1);\n-\treturn retval;\n+out:\n+\tif (internal_handle)\n+\t\text4_journal_stop(handle);\n+\n+\treturn ret ? ret : retval;\n }\n \n /*\n@@ -916,6 +1039,9 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock,\n \n \tif (ext4_has_inline_data(inode))\n \t\treturn -ERANGE;\n+\t/* inode using the iomap buffered I/O path should not go here. */\n+\tif (WARN_ON_ONCE(ext4_inode_buffered_iomap(inode)))\n+\t\treturn -EINVAL;\n \n \tmap.m_lblk = iblock;\n \tmap.m_len = bh-\u003eb_size \u003e\u003e inode-\u003ei_blkbits;\n@@ -1921,7 +2047,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)\n \text4_check_map_extents_env(inode);\n \n \t/* Lookup extent status tree firstly */\n-\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, NULL)) {\n+\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n \t\tmap-\u003em_len = min_t(unsigned int, map-\u003em_len,\n \t\t\t\t   es.es_len - (map-\u003em_lblk - es.es_lblk));\n \n@@ -1974,7 +2100,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)\n \t * is held in write mode, before inserting a new da entry in\n \t * the extent status tree.\n \t */\n-\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, NULL)) {\n+\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n \t\tmap-\u003em_len = min_t(unsigned int, map-\u003em_len,\n \t\t\t\t   es.es_len - (map-\u003em_lblk - es.es_lblk));\n \n@@ -1990,7 +2116,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)\n \t\t}\n \t}\n \n-\tmap-\u003em_flags |= EXT4_MAP_DELAYED;\n+\tmap-\u003em_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;\n \tretval = ext4_insert_delayed_blocks(inode, map-\u003em_lblk, map-\u003em_len);\n \tif (!retval)\n \t\tmap-\u003em_seq = READ_ONCE(EXT4_I(inode)-\u003ei_es_seq);\n@@ -2379,6 +2505,12 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)\n \terr = ext4_map_blocks(handle, inode, map, get_blocks_flags);\n \tif (err \u003c 0)\n \t\treturn err;\n+\t/*\n+\t * A hole? This should never happen since mpage_add_bh_to_extent()\n+\t * has filtered it out.\n+\t */\n+\tif (WARN_ON_ONCE(!err))\n+\t\treturn -EINVAL;\n \tif (dioread_nolock \u0026\u0026 (map-\u003em_flags \u0026 EXT4_MAP_UNWRITTEN)) {\n \t\tif (!mpd-\u003eio_submit.io_end-\u003ehandle \u0026\u0026\n \t\t    ext4_handle_valid(handle)) {\n@@ -2786,6 +2918,13 @@ static int ext4_do_writepages(struct mpage_da_data *mpd)\n \tif (!mapping-\u003enrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))\n \t\tgoto out_writepages;\n \n+\t/*\n+\t * Does ext4_change_inode_journal_flag() change the inode's\n+\t * buffered I/O path?\n+\t */\n+\tif (ext4_inode_buffered_iomap(inode))\n+\t\tgoto out_writepages;\n+\n \t/*\n \t * If the filesystem has aborted, it is read-only, so return\n \t * right away instead of dumping stack traces later on that\n@@ -3087,7 +3226,7 @@ static int ext4_dax_writepages(struct address_space *mapping,\n \treturn ret;\n }\n \n-static int ext4_nonda_switch(struct super_block *sb)\n+int ext4_nonda_switch(struct super_block *sb)\n {\n \ts64 free_clusters, dirty_clusters;\n \tstruct ext4_sb_info *sbi = EXT4_SB(sb);\n@@ -3459,6 +3598,15 @@ static bool ext4_inode_datasync_dirty(struct inode *inode)\n \treturn inode_state_read_once(inode) \u0026 I_DIRTY_DATASYNC;\n }\n \n+static bool ext4_iomap_valid(struct inode *inode, const struct iomap *iomap)\n+{\n+\treturn iomap-\u003evalidity_cookie == READ_ONCE(EXT4_I(inode)-\u003ei_es_seq);\n+}\n+\n+const struct iomap_write_ops ext4_iomap_write_ops = {\n+\t.iomap_valid = ext4_iomap_valid,\n+};\n+\n static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,\n \t\t\t   struct ext4_map_blocks *map, loff_t offset,\n \t\t\t   loff_t length, unsigned int flags)\n@@ -3493,6 +3641,8 @@ static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,\n \t    !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))\n \t\tiomap-\u003eflags |= IOMAP_F_MERGED;\n \n+\tiomap-\u003evalidity_cookie = map-\u003em_seq;\n+\n \t/*\n \t * Flags passed to ext4_map_blocks() for direct I/O writes can result\n \t * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits\n@@ -3907,151 +4057,636 @@ const struct iomap_ops ext4_iomap_report_ops = {\n \t.iomap_next = ext4_iomap_next_report,\n };\n \n-/*\n- * For data=journal mode, folio should be marked dirty only when it was\n- * writeably mapped. When that happens, it was already attached to the\n- * transaction and marked as jbddirty (we take care of this in\n- * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings\n- * so we should have nothing to do here, except for the case when someone\n- * had the page pinned and dirtied the page through this pin (e.g. by doing\n- * direct IO to it). In that case we'd need to attach buffers here to the\n- * transaction but we cannot due to lock ordering.  We cannot just dirty the\n- * folio and leave attached buffers clean, because the buffers' dirty state is\n- * \"definitive\".  We cannot just set the buffers dirty or jbddirty because all\n- * the journalling code will explode.  So what we do is to mark the folio\n- * \"pending dirty\" and next time ext4_writepages() is called, attach buffers\n- * to the transaction appropriately.\n- */\n-static bool ext4_journalled_dirty_folio(struct address_space *mapping,\n-\t\tstruct folio *folio)\n+static int ext4_iomap_map_blocks(struct inode *inode, loff_t offset,\n+\t\t\t\t loff_t length, struct ext4_map_blocks *map,\n+\t\t\t\t int flags)\n {\n-\tWARN_ON_ONCE(!folio_buffers(folio));\n-\tif (folio_maybe_dma_pinned(folio))\n-\t\tfolio_set_checked(folio);\n-\treturn filemap_dirty_folio(mapping, folio);\n+\tu8 blkbits = inode-\u003ei_blkbits;\n+\n+\t/* inode using the buffer_head buffered I/O path should not go here. */\n+\tif (WARN_ON_ONCE(!ext4_inode_buffered_iomap(inode)))\n+\t\treturn -EINVAL;\n+\tif ((offset \u003e\u003e blkbits) \u003e EXT4_MAX_LOGICAL_BLOCK)\n+\t\treturn -EINVAL;\n+\n+\t/* Calculate the first and last logical blocks respectively. */\n+\tmap-\u003em_lblk = offset \u003e\u003e blkbits;\n+\tmap-\u003em_len = min_t(loff_t, (offset + length - 1) \u003e\u003e blkbits,\n+\t\t\t   EXT4_MAX_LOGICAL_BLOCK) - map-\u003em_lblk + 1;\n+\n+\tif (flags \u0026 EXT4_GET_BLOCKS_DELALLOC_RESERVE)\n+\t\treturn ext4_da_map_blocks(inode, map);\n+\n+\treturn ext4_map_blocks(NULL, inode, map, flags);\n }\n \n-static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)\n+static int ext4_iomap_buffered_read_begin(struct inode *inode, loff_t offset,\n+\t\tloff_t length, unsigned int flags, struct iomap *iomap,\n+\t\tstruct iomap *srcmap)\n {\n-\tWARN_ON_ONCE(!folio_test_locked(folio) \u0026\u0026 !folio_test_dirty(folio));\n-\tWARN_ON_ONCE(!folio_buffers(folio));\n-\treturn block_dirty_folio(mapping, folio);\n+\tstruct ext4_map_blocks map;\n+\tint ret;\n+\n+\tif (unlikely(ext4_forced_shutdown(inode-\u003ei_sb)))\n+\t\treturn -EIO;\n+\n+\t/* Inline data support is not yet available. */\n+\tif (WARN_ON_ONCE(ext4_has_inline_data(inode)))\n+\t\treturn -ERANGE;\n+\n+\tret = ext4_iomap_map_blocks(inode, offset, length, \u0026map, 0);\n+\tif (ret \u003c 0)\n+\t\treturn ret;\n+\n+\ttrace_ext4_iomap_buffered_read_begin(inode, \u0026map, offset, length,\n+\t\t\t\t\t     flags);\n+\text4_set_iomap(inode, iomap, \u0026map, offset, length, flags);\n+\treturn 0;\n }\n \n-static int ext4_iomap_swap_activate(struct swap_info_struct *sis,\n-\t\t\t\t    struct file *file, sector_t *span)\n+static int ext4_iomap_buffered_do_write_begin(struct inode *inode,\n+\t\tloff_t offset, loff_t length, unsigned int flags,\n+\t\tstruct iomap *iomap, struct iomap *srcmap, bool delalloc)\n {\n-\treturn iomap_swapfile_activate(sis, file, span,\n-\t\t\t\t       \u0026ext4_iomap_report_ops);\n-}\n+\tint ret, retries = 0;\n+\tstruct ext4_map_blocks map;\n+\tint map_flags;\n \n-static const struct address_space_operations ext4_aops = {\n-\t.read_folio\t\t= ext4_read_folio,\n-\t.readahead\t\t= ext4_readahead,\n-\t.writepages\t\t= ext4_writepages,\n-\t.write_begin\t\t= ext4_write_begin,\n-\t.write_end\t\t= ext4_write_end,\n-\t.dirty_folio\t\t= ext4_dirty_folio,\n-\t.bmap\t\t\t= ext4_bmap,\n-\t.invalidate_folio\t= ext4_invalidate_folio,\n-\t.release_folio\t\t= ext4_release_folio,\n-\t.migrate_folio\t\t= buffer_migrate_folio,\n-\t.is_partially_uptodate  = block_is_partially_uptodate,\n-\t.error_remove_folio\t= generic_error_remove_folio,\n-\t.swap_activate\t\t= ext4_iomap_swap_activate,\n-};\n+\tret = ext4_emergency_state(inode-\u003ei_sb);\n+\tif (unlikely(ret))\n+\t\treturn ret;\n \n-static const struct address_space_operations ext4_journalled_aops = {\n-\t.read_folio\t\t= ext4_read_folio,\n-\t.readahead\t\t= ext4_readahead,\n-\t.writepages\t\t= ext4_writepages,\n-\t.write_begin\t\t= ext4_write_begin,\n-\t.write_end\t\t= ext4_journalled_write_end,\n-\t.dirty_folio\t\t= ext4_journalled_dirty_folio,\n-\t.bmap\t\t\t= ext4_bmap,\n-\t.invalidate_folio\t= ext4_journalled_invalidate_folio,\n-\t.release_folio\t\t= ext4_release_folio,\n-\t.migrate_folio\t\t= buffer_migrate_folio_norefs,\n-\t.is_partially_uptodate  = block_is_partially_uptodate,\n-\t.error_remove_folio\t= generic_error_remove_folio,\n-\t.swap_activate\t\t= ext4_iomap_swap_activate,\n-};\n+\t/* Inline data and non-extent are not supported. */\n+\tif (WARN_ON_ONCE(ext4_has_inline_data(inode)))\n+\t\treturn -ERANGE;\n+\tif (WARN_ON_ONCE(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))\n+\t\treturn -EINVAL;\n+\tif (WARN_ON_ONCE(!(flags \u0026 (IOMAP_WRITE | IOMAP_FAULT))))\n+\t\treturn -EINVAL;\n \n-static const struct address_space_operations ext4_da_aops = {\n-\t.read_folio\t\t= ext4_read_folio,\n-\t.readahead\t\t= ext4_readahead,\n-\t.writepages\t\t= ext4_writepages,\n-\t.write_begin\t\t= ext4_da_write_begin,\n-\t.write_end\t\t= ext4_da_write_end,\n-\t.dirty_folio\t\t= ext4_dirty_folio,\n-\t.bmap\t\t\t= ext4_bmap,\n-\t.invalidate_folio\t= ext4_invalidate_folio,\n-\t.release_folio\t\t= ext4_release_folio,\n-\t.migrate_folio\t\t= buffer_migrate_folio,\n-\t.is_partially_uptodate  = block_is_partially_uptodate,\n-\t.error_remove_folio\t= generic_error_remove_folio,\n-\t.swap_activate\t\t= ext4_iomap_swap_activate,\n-};\n+\tmap_flags = delalloc ? EXT4_GET_BLOCKS_DELALLOC_RESERVE :\n+\t\t\t       EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;\n+retry:\n+\tret = ext4_iomap_map_blocks(inode, offset, length, \u0026map, map_flags);\n+\tif (ret == -ENOSPC \u0026\u0026 ext4_should_retry_alloc(inode-\u003ei_sb, \u0026retries))\n+\t\tgoto retry;\n+\tif (ret \u003c 0)\n+\t\treturn ret;\n \n-static const struct address_space_operations ext4_dax_aops = {\n-\t.writepages\t\t= ext4_dax_writepages,\n-\t.dirty_folio\t\t= noop_dirty_folio,\n-\t.bmap\t\t\t= ext4_bmap,\n-\t.swap_activate\t\t= ext4_iomap_swap_activate,\n-};\n+\ttrace_ext4_iomap_buffered_write_begin(inode, \u0026map, offset, length,\n+\t\t\t\t\t      flags);\n+\text4_set_iomap(inode, iomap, \u0026map, offset, length, flags);\n+\treturn 0;\n+}\n \n-void ext4_set_aops(struct inode *inode)\n+static int ext4_iomap_buffered_write_begin(struct inode *inode,\n+\t\tloff_t offset, loff_t length, unsigned int flags,\n+\t\tstruct iomap *iomap, struct iomap *srcmap)\n {\n-\tswitch (ext4_inode_journal_mode(inode)) {\n-\tcase EXT4_INODE_ORDERED_DATA_MODE:\n-\tcase EXT4_INODE_WRITEBACK_DATA_MODE:\n-\t\tbreak;\n-\tcase EXT4_INODE_JOURNAL_DATA_MODE:\n-\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_journalled_aops;\n-\t\treturn;\n-\tdefault:\n-\t\tBUG();\n-\t}\n-\tif (IS_DAX(inode))\n-\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_dax_aops;\n-\telse if (test_opt(inode-\u003ei_sb, DELALLOC))\n-\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_da_aops;\n-\telse\n-\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_aops;\n+\treturn ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,\n+\t\t\t\t\t\t  iomap, srcmap, false);\n+}\n+\n+static int ext4_iomap_buffered_da_write_begin(struct inode *inode,\n+\t\tloff_t offset, loff_t length, unsigned int flags,\n+\t\tstruct iomap *iomap, struct iomap *srcmap)\n+{\n+\treturn ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,\n+\t\t\t\t\t\t  iomap, srcmap, true);\n }\n \n /*\n- * Here we can't skip an unwritten buffer even though it usually reads zero\n- * because it might have data in pagecache (eg, if called from ext4_zero_range,\n- * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a\n- * racing writeback can come later and flush the stale pagecache to disk.\n- *\n- * Return the loaded bh if it actually needs zeroing - in written, dirty\n- * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or\n- * a clean unwritten block).\n+ * On write failure, drop the stale delayed allocation range and release\n+ * its reserved space for both start and end blocks. Otherwise, we may\n+ * leave a range of delayed extents covered by a clean folio, which can\n+ * result in inaccurate space reservation accounting.\n  */\n-static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)\n+static void ext4_iomap_punch_delalloc(struct inode *inode, loff_t offset,\n+\t\t\t\t     loff_t length, struct iomap *iomap)\n {\n-\tunsigned int offset, blocksize, pos;\n-\text4_lblk_t iblock;\n-\tstruct address_space *mapping = inode-\u003ei_mapping;\n-\tstruct buffer_head *bh;\n-\tstruct folio *folio;\n-\tint err = 0;\n+\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\text4_es_remove_extent(inode, offset \u003e\u003e inode-\u003ei_blkbits,\n+\t\t\tDIV_ROUND_UP_ULL(length, EXT4_BLOCK_SIZE(inode-\u003ei_sb)));\n+\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+}\n \n-\tfolio = __filemap_get_folio(mapping, from \u003e\u003e PAGE_SHIFT,\n-\t\t\t\t    FGP_WRITEBEGIN | FGP_ACCESSED,\n-\t\t\t\t    mapping_gfp_constraint(mapping, ~__GFP_FS));\n-\tif (IS_ERR(folio))\n-\t\treturn ERR_CAST(folio);\n+static int ext4_iomap_buffered_da_write_end(struct inode *inode, loff_t offset,\n+\t\t\t\t\t    loff_t length, ssize_t written,\n+\t\t\t\t\t    unsigned int flags,\n+\t\t\t\t\t    struct iomap *iomap)\n+{\n+\tloff_t start_byte, end_byte;\n \n-\tblocksize = inode-\u003ei_sb-\u003es_blocksize;\n+\t/* If we didn't reserve the blocks, we're not allowed to punch them. */\n+\tif (iomap-\u003etype != IOMAP_DELALLOC || !(iomap-\u003eflags \u0026 IOMAP_F_NEW))\n+\t\treturn 0;\n \n-\tiblock = EXT4_PG_TO_LBLK(inode, folio-\u003eindex);\n+\t/*\n+\t * iomap_page_mkwrite() will never fail in a way that requires delalloc\n+\t * extents that it allocated to be revoked.  Hence never try to release\n+\t * them here.\n+\t */\n+\tif (flags \u0026 IOMAP_FAULT)\n+\t\treturn 0;\n \n-\tbh = folio_buffers(folio);\n-\tif (!bh)\n-\t\tbh = create_empty_buffers(folio, blocksize, 0);\n+\t/* Nothing to do if we've written the entire delalloc extent */\n+\tstart_byte = iomap_last_written_block(inode, offset, written);\n+\tend_byte = round_up(offset + length, i_blocksize(inode));\n+\tif (start_byte \u003e= end_byte)\n+\t\treturn 0;\n+\n+\tfilemap_invalidate_lock(inode-\u003ei_mapping);\n+\tiomap_write_delalloc_release(inode, start_byte, end_byte, flags,\n+\t\t\t\t     iomap, ext4_iomap_punch_delalloc);\n+\tfilemap_invalidate_unlock(inode-\u003ei_mapping);\n+\treturn 0;\n+}\n+\n+static int ext4_iomap_zero_begin(struct inode *inode,\n+\t\tloff_t offset, loff_t length, unsigned int flags,\n+\t\tstruct iomap *iomap, struct iomap *srcmap)\n+{\n+\tstruct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);\n+\tstruct ext4_map_blocks map;\n+\tu8 blkbits = inode-\u003ei_blkbits;\n+\tunsigned int iomap_flags;\n+\tint ret;\n+\n+\tret = ext4_emergency_state(inode-\u003ei_sb);\n+\tif (unlikely(ret))\n+\t\treturn ret;\n+\n+\tif (WARN_ON_ONCE(!(flags \u0026 IOMAP_ZERO)))\n+\t\treturn -EINVAL;\n+\n+again:\n+\tret = ext4_iomap_map_blocks(inode, offset, length, \u0026map, 0);\n+\tif (ret \u003c 0)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Look up dirty folios for unwritten mappings within EOF. Providing\n+\t * this bypasses the flush iomap uses to trigger extent conversion\n+\t * when unwritten mappings have dirty pagecache in need of zeroing.\n+\t */\n+\tiomap_flags = 0;\n+\tif (map.m_flags \u0026 EXT4_MAP_UNWRITTEN) {\n+\t\tloff_t start = ((loff_t)map.m_lblk) \u003c\u003c blkbits;\n+\t\tloff_t end = ((loff_t)map.m_lblk + map.m_len) \u003c\u003c blkbits;\n+\t\tunsigned int count;\n+\n+\t\tcount = iomap_fill_dirty_folios(iter, \u0026start, end,\n+\t\t\t\t\t\t\u0026iomap_flags);\n+\t\tif ((start \u003e\u003e blkbits) \u003c map.m_lblk + map.m_len)\n+\t\t\tmap.m_len = (start \u003e\u003e blkbits) - map.m_lblk;\n+\n+\t\t/*\n+\t\t * This can be raced by a concurrent writeback that cleans\n+\t\t * the folio and converts the unwritten extent to written.\n+\t\t * Recheck the mapping after a folio lock round in\n+\t\t * iomap_fill_dirty_folios().\n+\t\t */\n+\t\tif (count == 0 \u0026\u0026\n+\t\t    map.m_seq != READ_ONCE(EXT4_I(inode)-\u003ei_es_seq))\n+\t\t\tgoto again;\n+\t}\n+\n+\ttrace_ext4_iomap_zero_begin(inode, \u0026map, offset, length, flags);\n+\text4_set_iomap(inode, iomap, \u0026map, offset, length, flags);\n+\tiomap-\u003eflags |= iomap_flags;\n+\n+\treturn 0;\n+}\n+\n+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_zero_next, ext4_iomap_zero_begin);\n+\n+static const struct iomap_ops ext4_iomap_zero_ops = {\n+\t.iomap_next = ext4_iomap_zero_next,\n+};\n+\n+/*\n+ * Since we always allocate unwritten extents, there is no need for\n+ * iomap_end to clean up allocated blocks on a short write.\n+ */\n+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next,\n+\t\t\t      ext4_iomap_buffered_write_begin);\n+\n+const struct iomap_ops ext4_iomap_buffered_write_ops = {\n+\t.iomap_next = ext4_iomap_buffered_write_next,\n+};\n+\n+static DEFINE_IOMAP_ITER_NEXT_END(ext4_iomap_buffered_da_write_next,\n+\t\t\t\t  ext4_iomap_buffered_da_write_begin,\n+\t\t\t\t  ext4_iomap_buffered_da_write_end);\n+\n+const struct iomap_ops ext4_iomap_buffered_da_write_ops = {\n+\t.iomap_next = ext4_iomap_buffered_da_write_next,\n+};\n+\n+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_read_next,\n+\t\t\t      ext4_iomap_buffered_read_begin);\n+\n+static const struct iomap_ops ext4_iomap_buffered_read_ops = {\n+\t.iomap_next = ext4_iomap_buffered_read_next,\n+};\n+\n+static int ext4_iomap_read_folio(struct file *file, struct folio *folio)\n+{\n+\tiomap_bio_read_folio(folio, \u0026ext4_iomap_buffered_read_ops);\n+\treturn 0;\n+}\n+\n+static void ext4_iomap_readahead(struct readahead_control *rac)\n+{\n+\tiomap_bio_readahead(rac, \u0026ext4_iomap_buffered_read_ops);\n+}\n+\n+\n+static int ext4_iomap_map_writeback_range(struct iomap_writepage_ctx *wpc,\n+\t\t\t\t\t  loff_t offset, unsigned int dirty_len)\n+{\n+\tstruct inode *inode = wpc-\u003einode;\n+\tstruct super_block *sb = inode-\u003ei_sb;\n+\tstruct journal_s *journal = EXT4_SB(sb)-\u003es_journal;\n+\tstruct ext4_map_blocks map;\n+\text4_lblk_t index = offset \u003e\u003e inode-\u003ei_blkbits;\n+\tunsigned int blk_len, blk_end;\n+\tint retries = 0;\n+\tint ret;\n+\n+\tret = ext4_emergency_state(sb);\n+\tif (unlikely(ret))\n+\t\treturn ret;\n+\n+\t/* Check validity of the cached writeback mapping. */\n+\tif (offset \u003e= wpc-\u003eiomap.offset \u0026\u0026\n+\t    offset \u003c wpc-\u003eiomap.offset + wpc-\u003eiomap.length \u0026\u0026\n+\t    ext4_iomap_valid(inode, \u0026wpc-\u003eiomap))\n+\t\treturn 0;\n+\n+\tblk_len = dirty_len \u003e\u003e inode-\u003ei_blkbits;\n+\tblk_end = umin(wpc-\u003ewbc-\u003erange_end \u003e\u003e inode-\u003ei_blkbits, UINT_MAX - 1);\n+\tif (blk_end \u003e index + blk_len)\n+\t\tblk_len = blk_end - index + 1;\n+\n+retry:\n+\tmap.m_lblk = index;\n+\tmap.m_len = min_t(unsigned int, MAX_WRITEPAGES_EXTENT_LEN, blk_len);\n+\tret = ext4_map_blocks(NULL, inode, \u0026map,\n+\t\t\t      EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT |\n+\t\t\t      EXT4_GET_BLOCKS_METADATA_NOFAIL |\n+\t\t\t      EXT4_GET_BLOCKS_IO_SUBMIT |\n+\t\t\t      EXT4_EX_NOCACHE);\n+\tif (ret \u003c 0) {\n+\t\tif (ext4_emergency_state(sb))\n+\t\t\treturn ret;\n+\n+\t\t/*\n+\t\t * Retry transient ENOSPC errors, if\n+\t\t * ext4_count_free_blocks() is non-zero, a commit\n+\t\t * should free up blocks.\n+\t\t */\n+\t\tif (ret == -ENOSPC \u0026\u0026 journal \u0026\u0026 ext4_count_free_clusters(sb) \u0026\u0026\n+\t\t    ++retries \u003c= 3) {\n+\t\t\tjbd2_journal_force_commit_nested(journal);\n+\t\t\tgoto retry;\n+\t\t}\n+\n+\t\text4_msg(sb, KERN_CRIT,\n+\t\t\t \"Delayed block allocation failed for inode %llu at logical offset %llu with max blocks %u with error %d\",\n+\t\t\t inode-\u003ei_ino, (unsigned long long)map.m_lblk,\n+\t\t\t (unsigned int)map.m_len, -ret);\n+\t\text4_msg(sb, KERN_CRIT,\n+\t\t\t \"This should not happen!! Data will be lost\\n\");\n+\t\tif (ret == -ENOSPC)\n+\t\t\text4_print_free_blocks(inode);\n+\t\treturn ret;\n+\t}\n+\n+\ttrace_ext4_iomap_map_writeback_range(inode, \u0026map, offset, dirty_len, 0);\n+\text4_set_iomap(inode, \u0026wpc-\u003eiomap, \u0026map, offset, dirty_len, 0);\n+\treturn 0;\n+}\n+\n+static void ext4_iomap_discard_folio(struct folio *folio, loff_t pos)\n+{\n+\tstruct inode *inode = folio-\u003emapping-\u003ehost;\n+\tloff_t length = folio_pos(folio) + folio_size(folio) - pos;\n+\tloff_t pstart, plen;\n+\n+\text4_iomap_punch_delalloc(inode, pos, length, NULL);\n+\n+\t/*\n+\t * Clear the disksize-grow-pending state if the zeroed EOF block\n+\t * fails to write back and is discarded.\n+\t */\n+\tplen = ext4_iomap_get_disksize_pending_range(inode, \u0026pstart);\n+\tif (plen \u0026\u0026 pos \u003c= pstart \u0026\u0026 folio_next_pos(folio) \u003e= pstart + plen)\n+\t\text4_iomap_clear_disksize_pending(inode);\n+}\n+\n+static ssize_t ext4_iomap_writeback_range(struct iomap_writepage_ctx *wpc,\n+\t\t\t\t\t  struct folio *folio, u64 offset,\n+\t\t\t\t\t  unsigned int len, u64 end_pos)\n+{\n+\tssize_t ret;\n+\n+\tret = ext4_iomap_map_writeback_range(wpc, offset, len);\n+\tif (!ret)\n+\t\tret = iomap_add_to_ioend(wpc, folio, offset, end_pos, len);\n+\tif (ret \u003c 0)\n+\t\text4_iomap_discard_folio(folio, offset);\n+\treturn ret;\n+}\n+\n+static int ext4_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,\n+\t\t\t\t       int error)\n+{\n+\tstruct iomap_ioend *ioend = wpc-\u003ewb_ctx;\n+\tstruct inode *inode = ioend-\u003eio_inode;\n+\tstruct ext4_inode_info *ei = EXT4_I(inode);\n+\tunsigned int blocksize = i_blocksize(inode);\n+\tloff_t pstart, plen;\n+\n+\t/*\n+\t * After I/O completion, a worker needs to be scheduled when:\n+\t * 1) Unwritten extents require conversion.\n+\t * 2) The file size needs to be extended.\n+\t * 3) The journal needs to be aborted due to an I/O error.\n+\t */\n+\tif ((ioend-\u003eio_flags \u0026 IOMAP_IOEND_UNWRITTEN) ||\n+\t    (ioend-\u003eio_offset + ioend-\u003eio_size \u003e READ_ONCE(ei-\u003ei_disksize)) ||\n+\t    test_opt(ioend-\u003eio_inode-\u003ei_sb, DATA_ERR_ABORT))\n+\t\tioend-\u003eio_bio.bi_end_io = ext4_iomap_end_bio;\n+\n+\t/*\n+\t * Mark the I/O as DISKSIZE_GROW_IO by setting io_private to\n+\t * EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO if it covers the pending range.\n+\t * Such I/O will allow or trigger i_disksize advancement in the\n+\t * ioend worker.\n+\t */\n+\tplen = ext4_iomap_get_disksize_pending_range(inode, \u0026pstart);\n+\tif (plen \u0026\u0026\n+\t    round_down(ioend-\u003eio_offset, blocksize) \u003c= pstart \u0026\u0026\n+\t    round_up(ioend-\u003eio_offset + ioend-\u003eio_size, blocksize) \u003e=\n+\t\t\tpstart + plen) {\n+\t\ttrace_ext4_iomap_wb_disksize_pending_submit(inode,\n+\t\t\t\tioend-\u003eio_offset, ioend-\u003eio_size);\n+\t\tioend-\u003eio_bio.bi_end_io = ext4_iomap_end_bio;\n+\t\tioend-\u003eio_private = (void *)EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO;\n+\t}\n+\n+\t/*\n+\t * ext4_iomap_end_bio() always defers endio processing, disable\n+\t * generic BIO in task to avoid double deferral since we will use\n+\t * a private defer endio handler in process context.\n+\t *\n+\t * TODO: Switch all defer handlers to the generic bio complete\n+\t * in task framework.\n+\t */\n+\tif (ioend-\u003eio_bio.bi_end_io)\n+\t\tbio_clear_flag(\u0026ioend-\u003eio_bio, BIO_COMPLETE_IN_TASK);\n+\n+\treturn iomap_ioend_writeback_submit(wpc, error);\n+}\n+\n+static const struct iomap_writeback_ops ext4_writeback_ops = {\n+\t.writeback_range = ext4_iomap_writeback_range,\n+\t.writeback_submit = ext4_iomap_writeback_submit,\n+};\n+\n+/*\n+ * If the current writeback range begins after the pending zeroed EOF\n+ * block range which straddles i_disksize, issue a separate writeback to\n+ * flush it first, so as to avoid prolonged waiting.\n+ */\n+static void ext4_iomap_wb_submit_zeroed_eof(struct inode *inode,\n+\t\t\t\t\t    struct writeback_control *wbc)\n+{\n+\tstruct address_space *mapping = inode-\u003ei_mapping;\n+\tloff_t pstart, plen, range_start;\n+\n+\tif (wbc-\u003erange_cyclic)\n+\t\trange_start = (loff_t)mapping-\u003ewriteback_index \u003c\u003c PAGE_SHIFT;\n+\telse\n+\t\trange_start = wbc-\u003erange_start;\n+\n+\tplen = ext4_iomap_get_disksize_pending_range(inode, \u0026pstart);\n+\tif (!plen)\n+\t\treturn;\n+\tif (round_down(range_start, mapping_min_folio_nrbytes(mapping)) \u003c\n+\t    pstart + plen)\n+\t\treturn;\n+\n+\t/* Keep the caller's sync mode to avoid stalling the background flusher. */\n+\tif (wbc-\u003esync_mode == WB_SYNC_ALL)\n+\t\tfilemap_fdatawrite_range(mapping, pstart, pstart + plen - 1);\n+\telse\n+\t\tfilemap_flush_range(mapping, pstart, pstart + plen - 1);\n+}\n+\n+static int ext4_iomap_writepages(struct address_space *mapping,\n+\t\t\t\t struct writeback_control *wbc)\n+{\n+\tstruct inode *inode = mapping-\u003ehost;\n+\tstruct super_block *sb = inode-\u003ei_sb;\n+\tlong nr = wbc-\u003enr_to_write;\n+\tint alloc_ctx, ret;\n+\tstruct iomap_writepage_ctx wpc = {\n+\t\t.inode = inode,\n+\t\t.wbc = wbc,\n+\t\t.ops = \u0026ext4_writeback_ops,\n+\t};\n+\n+\tret = ext4_emergency_state(sb);\n+\tif (unlikely(ret))\n+\t\treturn ret;\n+\n+\t/*\n+\t * Submit the pending zeroed EOF block range if the entire\n+\t * writeback range lies beyond it.\n+\t */\n+\text4_iomap_wb_submit_zeroed_eof(inode, wbc);\n+\n+\talloc_ctx = ext4_writepages_down_read(sb);\n+\t/*\n+\t * Does ext4_change_inode_journal_flag() change the inode's\n+\t * buffered I/O path?\n+\t */\n+\tif (!ext4_inode_buffered_iomap(inode)) {\n+\t\text4_writepages_up_read(sb, alloc_ctx);\n+\t\treturn ret;\n+\t}\n+\n+\ttrace_ext4_writepages(inode, wbc);\n+\tret = iomap_writepages(\u0026wpc);\n+\ttrace_ext4_writepages_result(inode, wbc, ret, nr - wbc-\u003enr_to_write);\n+\text4_writepages_up_read(sb, alloc_ctx);\n+\n+\treturn ret;\n+}\n+\n+/*\n+ * For data=journal mode, folio should be marked dirty only when it was\n+ * writeably mapped. When that happens, it was already attached to the\n+ * transaction and marked as jbddirty (we take care of this in\n+ * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings\n+ * so we should have nothing to do here, except for the case when someone\n+ * had the page pinned and dirtied the page through this pin (e.g. by doing\n+ * direct IO to it). In that case we'd need to attach buffers here to the\n+ * transaction but we cannot due to lock ordering.  We cannot just dirty the\n+ * folio and leave attached buffers clean, because the buffers' dirty state is\n+ * \"definitive\".  We cannot just set the buffers dirty or jbddirty because all\n+ * the journalling code will explode.  So what we do is to mark the folio\n+ * \"pending dirty\" and next time ext4_writepages() is called, attach buffers\n+ * to the transaction appropriately.\n+ */\n+static bool ext4_journalled_dirty_folio(struct address_space *mapping,\n+\t\tstruct folio *folio)\n+{\n+\tWARN_ON_ONCE(!folio_buffers(folio));\n+\tif (folio_maybe_dma_pinned(folio))\n+\t\tfolio_set_checked(folio);\n+\treturn filemap_dirty_folio(mapping, folio);\n+}\n+\n+static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)\n+{\n+\tWARN_ON_ONCE(!folio_test_locked(folio) \u0026\u0026 !folio_test_dirty(folio));\n+\tWARN_ON_ONCE(!folio_buffers(folio));\n+\treturn block_dirty_folio(mapping, folio);\n+}\n+\n+static int ext4_iomap_swap_activate(struct swap_info_struct *sis,\n+\t\t\t\t    struct file *file, sector_t *span)\n+{\n+\treturn iomap_swapfile_activate(sis, file, span,\n+\t\t\t\t       \u0026ext4_iomap_report_ops);\n+}\n+\n+static const struct address_space_operations ext4_aops = {\n+\t.read_folio\t\t= ext4_read_folio,\n+\t.readahead\t\t= ext4_readahead,\n+\t.writepages\t\t= ext4_writepages,\n+\t.write_begin\t\t= ext4_write_begin,\n+\t.write_end\t\t= ext4_write_end,\n+\t.dirty_folio\t\t= ext4_dirty_folio,\n+\t.bmap\t\t\t= ext4_bmap,\n+\t.invalidate_folio\t= ext4_invalidate_folio,\n+\t.release_folio\t\t= ext4_release_folio,\n+\t.migrate_folio\t\t= buffer_migrate_folio,\n+\t.is_partially_uptodate  = block_is_partially_uptodate,\n+\t.error_remove_folio\t= generic_error_remove_folio,\n+\t.swap_activate\t\t= ext4_iomap_swap_activate,\n+};\n+\n+static const struct address_space_operations ext4_journalled_aops = {\n+\t.read_folio\t\t= ext4_read_folio,\n+\t.readahead\t\t= ext4_readahead,\n+\t.writepages\t\t= ext4_writepages,\n+\t.write_begin\t\t= ext4_write_begin,\n+\t.write_end\t\t= ext4_journalled_write_end,\n+\t.dirty_folio\t\t= ext4_journalled_dirty_folio,\n+\t.bmap\t\t\t= ext4_bmap,\n+\t.invalidate_folio\t= ext4_journalled_invalidate_folio,\n+\t.release_folio\t\t= ext4_release_folio,\n+\t.migrate_folio\t\t= buffer_migrate_folio_norefs,\n+\t.is_partially_uptodate  = block_is_partially_uptodate,\n+\t.error_remove_folio\t= generic_error_remove_folio,\n+\t.swap_activate\t\t= ext4_iomap_swap_activate,\n+};\n+\n+static const struct address_space_operations ext4_da_aops = {\n+\t.read_folio\t\t= ext4_read_folio,\n+\t.readahead\t\t= ext4_readahead,\n+\t.writepages\t\t= ext4_writepages,\n+\t.write_begin\t\t= ext4_da_write_begin,\n+\t.write_end\t\t= ext4_da_write_end,\n+\t.dirty_folio\t\t= ext4_dirty_folio,\n+\t.bmap\t\t\t= ext4_bmap,\n+\t.invalidate_folio\t= ext4_invalidate_folio,\n+\t.release_folio\t\t= ext4_release_folio,\n+\t.migrate_folio\t\t= buffer_migrate_folio,\n+\t.is_partially_uptodate  = block_is_partially_uptodate,\n+\t.error_remove_folio\t= generic_error_remove_folio,\n+\t.swap_activate\t\t= ext4_iomap_swap_activate,\n+};\n+\n+static const struct address_space_operations ext4_iomap_aops = {\n+\t.read_folio\t\t= ext4_iomap_read_folio,\n+\t.readahead\t\t= ext4_iomap_readahead,\n+\t.writepages\t\t= ext4_iomap_writepages,\n+\t.dirty_folio\t\t= iomap_dirty_folio,\n+\t.bmap\t\t\t= ext4_bmap,\n+\t.invalidate_folio\t= iomap_invalidate_folio,\n+\t.release_folio\t\t= iomap_release_folio,\n+\t.migrate_folio\t\t= filemap_migrate_folio,\n+\t.is_partially_uptodate  = iomap_is_partially_uptodate,\n+\t.error_remove_folio\t= generic_error_remove_folio,\n+\t.swap_activate\t\t= ext4_iomap_swap_activate,\n+};\n+\n+static const struct address_space_operations ext4_dax_aops = {\n+\t.writepages\t\t= ext4_dax_writepages,\n+\t.dirty_folio\t\t= noop_dirty_folio,\n+\t.bmap\t\t\t= ext4_bmap,\n+\t.swap_activate\t\t= ext4_iomap_swap_activate,\n+};\n+\n+void ext4_set_aops(struct inode *inode)\n+{\n+\tswitch (ext4_inode_journal_mode(inode)) {\n+\tcase EXT4_INODE_ORDERED_DATA_MODE:\n+\tcase EXT4_INODE_WRITEBACK_DATA_MODE:\n+\t\tbreak;\n+\tcase EXT4_INODE_JOURNAL_DATA_MODE:\n+\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_journalled_aops;\n+\t\treturn;\n+\tdefault:\n+\t\tBUG();\n+\t}\n+\tif (IS_DAX(inode))\n+\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_dax_aops;\n+\telse if (ext4_inode_buffered_iomap(inode))\n+\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_iomap_aops;\n+\telse if (test_opt(inode-\u003ei_sb, DELALLOC))\n+\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_da_aops;\n+\telse\n+\t\tinode-\u003ei_mapping-\u003ea_ops = \u0026ext4_aops;\n+}\n+\n+/*\n+ * Here we can't skip an unwritten buffer even though it usually reads zero\n+ * because it might have data in pagecache (eg, if called from ext4_zero_range,\n+ * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a\n+ * racing writeback can come later and flush the stale pagecache to disk.\n+ *\n+ * Return the loaded bh if it actually needs zeroing - in written, dirty\n+ * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or\n+ * a clean unwritten block).\n+ */\n+static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)\n+{\n+\tunsigned int offset, blocksize, pos;\n+\text4_lblk_t iblock;\n+\tstruct address_space *mapping = inode-\u003ei_mapping;\n+\tstruct buffer_head *bh;\n+\tstruct folio *folio;\n+\tint err = 0;\n+\n+\tfolio = __filemap_get_folio(mapping, from \u003e\u003e PAGE_SHIFT,\n+\t\t\t\t    FGP_WRITEBEGIN | FGP_ACCESSED,\n+\t\t\t\t    mapping_gfp_constraint(mapping, ~__GFP_FS));\n+\tif (IS_ERR(folio))\n+\t\treturn ERR_CAST(folio);\n+\n+\tblocksize = inode-\u003ei_sb-\u003es_blocksize;\n+\n+\tiblock = EXT4_PG_TO_LBLK(inode, folio-\u003eindex);\n+\n+\tbh = folio_buffers(folio);\n+\tif (!bh)\n+\t\tbh = create_empty_buffers(folio, blocksize, 0);\n \n \t/* Find the buffer that contains \"offset\" */\n \toffset = offset_in_folio(folio, from);\n@@ -4167,6 +4802,48 @@ static int ext4_block_journalled_zero_range(struct inode *inode, loff_t from,\n \treturn err;\n }\n \n+static int ext4_block_iomap_zero_range(struct inode *inode, loff_t from,\n+\t\t\t\t       loff_t length, bool *did_zero,\n+\t\t\t\t       bool *zero_written)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Zeroing out under an active handle can cause deadlock since\n+\t * the order of acquiring the folio lock and starting a handle is\n+\t * inconsistent with the iomap writeback procedure.\n+\t */\n+\tif (WARN_ON_ONCE(ext4_handle_valid(journal_current_handle())))\n+\t\treturn -EINVAL;\n+\n+\t/* The zeroing scope should not extend across a block. */\n+\tif (WARN_ON_ONCE((from \u003e\u003e inode-\u003ei_blkbits) !=\n+\t\t\t ((from + length - 1) \u003e\u003e inode-\u003ei_blkbits)))\n+\t\treturn -EINVAL;\n+\n+\tif (!(EXT4_SB(inode-\u003ei_sb)-\u003es_mount_state \u0026 EXT4_ORPHAN_FS) \u0026\u0026\n+\t    !(inode_state_read_once(inode) \u0026 (I_NEW | I_FREEING)))\n+\t\tWARN_ON_ONCE(!inode_is_locked(inode) \u0026\u0026\n+\t\t\t!rwsem_is_locked(\u0026inode-\u003ei_mapping-\u003einvalidate_lock));\n+\n+\tret = iomap_zero_range(inode, from, length, did_zero,\n+\t\t\t       \u0026ext4_iomap_zero_ops, \u0026ext4_iomap_write_ops,\n+\t\t\t       NULL);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * TODO: The iomap does not distinguish between different types\n+\t * of zeroing operations. So we always set zero_written whenever\n+\t * zeroing is performed, which may cause unnecessary folio\n+\t * flushing when zeroing occurs on delayed-allocated blocks.\n+\t */\n+\tif (did_zero \u0026\u0026 zero_written)\n+\t\t*zero_written = *did_zero;\n+\n+\treturn 0;\n+}\n+\n /*\n  * Zeros out a mapping of length 'length' starting from file offset\n  * 'from'.  The range to be zero'd must be contained with in one block.\n@@ -4193,11 +4870,132 @@ static int ext4_block_zero_range(struct inode *inode,\n \t} else if (ext4_should_journal_data(inode)) {\n \t\treturn ext4_block_journalled_zero_range(inode, from, length,\n \t\t\t\t\t\t\tdid_zero);\n+\t} else if (ext4_inode_buffered_iomap(inode)) {\n+\t\treturn ext4_block_iomap_zero_range(inode, from, length,\n+\t\t\t\t\t\t   did_zero, zero_written);\n \t}\n \treturn ext4_block_do_zero_range(inode, from, length, did_zero,\n \t\t\t\t\tzero_written);\n }\n \n+/*\n+ * Inodes using the iomap buffered I/O path do not use data=ordered mode.\n+ * Therefore, we mark the inode as disksize-grow-pending after zeroing the\n+ * EOF block. The zeroed block will be submitted before any subsequent\n+ * data.\n+ *\n+ * In the I/O completion path, ext4_iomap_wb_disksize_pending_wait() will\n+ * wait for I/O completion before advancing i_disksize if the write\n+ * extends beyond the zeroed boundary.\n+ *\n+ * When zeroed I/O is in progress, operations that extend i_disksize are\n+ * handled as follows:\n+ *\n+ *  - Truncate up, append fallocate and zero_range:\n+ *    Defer the update. The file size will be updated to i_size by the\n+ *    end_io handler once the ongoing pending I/O completes.\n+ *\n+ *  - Insert range and collapse range operations:\n+ *    Wait synchronously for the relevant I/O to complete before updating\n+ *    i_disksize.\n+ */\n+static int ext4_iomap_mark_disksize_pending(struct inode *inode, loff_t from)\n+{\n+\tstruct folio *folio;\n+\n+\tfolio = filemap_lock_folio(inode-\u003ei_mapping, from \u003e\u003e PAGE_SHIFT);\n+\tif (IS_ERR(folio))\n+\t\t/* Already in writeback and cleared? */\n+\t\treturn PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);\n+\n+\t/*\n+\t * Ensure that in-flight writeback, possibly started after\n+\t * iomap_zero_range() unlocked the folio, has completed. Without\n+\t * this wait folio_test_dirty() below may miss the zeroed data\n+\t * (writeback clears PG_dirty), causing us to skip the\n+\t * disksize-grow-pending tracking and potentially expose stale\n+\t * on-disk data.\n+\t */\n+\tfolio_wait_writeback(folio);\n+\tWARN_ON_ONCE(folio_test_writeback(folio));\n+\n+\t/*\n+\t * If the zeroed range does not overlap the existing on-disk tail\n+\t * block, the zeroed data lies beyond the currently on-disk data\n+\t * and will be written back before i_disksize is advanced past it,\n+\t * so no stale data can be exposed.\n+\t *\n+\t * Checking i_disksize here (after folio_wait_writeback()) is\n+\t * necessary and safe. If a delalloc writeback of this folio was\n+\t * in-flight, it could be raced by a concurrent mmap write which\n+\t * corrupts the tail block beyond i_size but the i_disksize is not\n+\t * advanced. folio_wait_writeback() has waited for its completion\n+\t * and the ioend has advanced i_disksize accordingly. If writeback\n+\t * had not started, we don't need to mark any pending state because\n+\t * any future writeback will carry the pagecache content that now\n+\t * includes the zeroed data, so no stale data can appear on disk\n+\t * even without the pending tracking.\n+\t */\n+\tif (from \u003e= round_up(READ_ONCE(EXT4_I(inode)-\u003ei_disksize),\n+\t\t\t     i_blocksize(inode)))\n+\t\tgoto out;\n+\n+\t/*\n+\t * Mark the inode as disksize-grow-pending. The zeroed block will\n+\t * be written out by the generic writepages cycle or any other\n+\t * syncing operation.\n+\t *\n+\t * Multiple overlapping unaligned EOF writes should not happen,\n+\t * because we only mark the pending state after zeroing the on-disk\n+\t * EOF block, and i_disksize can only be updated after the previous\n+\t * zeroed pending block has been written back or the dirty folio\n+\t * has been discarded.\n+\t */\n+\tif (likely(folio_test_dirty(folio) \u0026\u0026\n+\t\t   !ext4_test_inode_state(inode,\n+\t\t\t\t\t  EXT4_STATE_DISKSIZE_GROW_PENDING))) {\n+\t\ttrace_ext4_iomap_mark_disksize_pending(inode);\n+\t\text4_set_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING);\n+\t}\n+out:\n+\tfolio_unlock(folio);\n+\tfolio_put(folio);\n+\treturn 0;\n+}\n+\n+/*\n+ * Submit and wait for the pending zeroed EOF block range to complete\n+ * if the given range [@offset, @end) fully covers it.  Must be called\n+ * outside the context of an active journal handle and hold the i_rwsem.\n+ */\n+int ext4_iomap_sync_zeroed_eof(struct inode *inode, loff_t offset, loff_t end)\n+{\n+\tloff_t pstart, plen;\n+\tint ret;\n+\n+\tif (!ext4_inode_buffered_iomap(inode))\n+\t\treturn 0;\n+\n+\tplen = ext4_iomap_get_disksize_pending_range(inode, \u0026pstart);\n+\tif (!plen || offset \u003e pstart || end \u003c pstart + plen)\n+\t\treturn 0;\n+\n+\tret = filemap_write_and_wait_range(inode-\u003ei_mapping, pstart,\n+\t\t\t\t\t   pstart + plen - 1);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * The pending bit should have been cleared by the ioend worker,\n+\t * which runs before the folio writeback flag is cleared.  The\n+\t * caller holds i_rwsem, so no concurrent ext4_block_zero_eof()\n+\t * can re-set it.\n+\t */\n+\tWARN_ON_ONCE(ext4_test_inode_state(inode,\n+\t\t\t\tEXT4_STATE_DISKSIZE_GROW_PENDING));\n+\treturn 0;\n+}\n+\n /*\n  * Zero out a mapping from file offset 'from' up to the end of the block\n  * which corresponds to 'from' or to the given 'end' inside this block.\n@@ -4242,20 +5040,24 @@ int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)\n \t * exposing stale on-disk data which may caused by concurrent post-EOF\n \t * mmap write during folio writeback.\n \t */\n-\tif (ext4_should_order_data(inode) \u0026\u0026\n-\t    did_zero \u0026\u0026 zero_written \u0026\u0026 !IS_DAX(inode)) {\n-\t\thandle_t *handle;\n+\tif (did_zero \u0026\u0026 zero_written \u0026\u0026 !IS_DAX(inode)) {\n+\t\tif (ext4_should_order_data(inode)) {\n+\t\t\thandle_t *handle;\n \n-\t\thandle = ext4_journal_start(inode, EXT4_HT_MISC, 1);\n-\t\tif (IS_ERR(handle))\n-\t\t\treturn PTR_ERR(handle);\n+\t\t\thandle = ext4_journal_start(inode, EXT4_HT_MISC, 1);\n+\t\t\tif (IS_ERR(handle))\n+\t\t\t\treturn PTR_ERR(handle);\n \n-\t\terr = ext4_jbd2_inode_add_write(handle, inode, from, length);\n-\t\text4_journal_stop(handle);\n+\t\t\terr = ext4_jbd2_inode_add_write(handle, inode, from,\n+\t\t\t\t\t\t\tlength);\n+\t\t\text4_journal_stop(handle);\n+\t\t} else if (ext4_inode_buffered_iomap(inode))\n+\t\t\terr = ext4_iomap_mark_disksize_pending(inode, from);\n \t\tif (err)\n \t\t\treturn err;\n \t}\n \n+\ttrace_ext4_block_zero_eof(inode, from, length, did_zero, zero_written);\n \treturn 0;\n }\n \n@@ -4352,6 +5154,14 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,\n \tif (offset \u003e size)\n \t\treturn 0;\n \n+\t/*\n+\t * We are going to punch the pending zeroed EOF block, persist\n+\t * it to ensure i_disksize can be safely updated thereafter.\n+\t */\n+\tret = ext4_iomap_sync_zeroed_eof(inode, offset, offset + len);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tif (offset + len \u003c size)\n \t\tsize = offset + len;\n \tif (EXT4_I(inode)-\u003ei_disksize \u003e= size)\n@@ -4503,7 +5313,14 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)\n \tret = ext4_zero_partial_blocks(inode, offset, length, \u0026partial_zeroed);\n \tif (ret)\n \t\treturn ret;\n-\tif (((file-\u003ef_flags \u0026 O_SYNC) || IS_SYNC(inode)) \u0026\u0026 partial_zeroed) {\n+\t/*\n+\t * On the iomap path, partial invalidate of a folio without an ifs\n+\t * leaves sub-block dirty bits uncleared.  Drain writeback before\n+\t * removing extents so that any bio in flight on a partial folio\n+\t * completes against blocks still owned by this inode.\n+\t */\n+\tif (ext4_inode_buffered_iomap(inode) ||\n+\t    (((file-\u003ef_flags \u0026 O_SYNC) || IS_SYNC(inode)) \u0026\u0026 partial_zeroed)) {\n \t\tret = filemap_write_and_wait_range(inode-\u003ei_mapping, offset,\n \t\t\t\t\t\t   end - 1);\n \t\tif (ret)\n@@ -5281,6 +6098,87 @@ static int check_igot_inode(struct inode *inode, ext4_iget_flags flags,\n \treturn -EFSCORRUPTED;\n }\n \n+/*\n+ * Determine whether an inode should use the iomap buffered I/O path.\n+ * EXT4_STATE_BUFFERED_IOMAP is generally set at inode initialization\n+ * time. Online switching of the buffered I/O path on an active inode is\n+ * NOT supported, with the exception of changing a per-inode journal\n+ * flag.\n+ *\n+ * For features like inline data, fsverity, and encryption that can be\n+ * dynamically enabled or disabled, we check the superblock-level\n+ * feature flags. If any of these is globally enabled, no inode is\n+ * allowed into the iomap buffered I/O path. This avoids the complexity\n+ * of dynamic toggling.\n+ *\n+ * For the global data journal mode (EXT4_MOUNT_JOURNAL_DATA), dynamic\n+ * change through remount is deferred. It will only become available\n+ * after the inode is re-initialized (i.e., after the last reference\n+ * drops and the inode is re-read from disk with the journal flag\n+ * cleared).\n+ *\n+ * For the per-inode data journal mode (EXT4_INODE_JOURNAL_DATA),\n+ * dynamic changes take effect immediately. This is safe because\n+ * address_space operations can be switched and all page cache can be\n+ * dropped under i_rwsem and filemap_invalidate_lock.\n+ *\n+ * For extent-to-indirect block migration (via EXT4_IOC_SETFLAGS\n+ * clearing EXT4_EXTENTS_FL), this operation is directly rejected for\n+ * inodes using the iomap path.\n+ *\n+ * When remounting to toggle the buffered_iomap mount option, the change\n+ * of I/O path is deferred as well, it will be available after the inode\n+ * is re-initialized.\n+ */\n+void ext4_enable_buffered_iomap(struct inode *inode)\n+{\n+\tstruct super_block *sb = inode-\u003ei_sb;\n+\n+\tif (!test_opt2(sb, BUFFERED_IOMAP))\n+\t\treturn;\n+\tif (!S_ISREG(inode-\u003ei_mode))\n+\t\treturn;\n+\tif (ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))\n+\t\treturn;\n+\n+\t/* Unsupported Features */\n+\tif (ext4_has_feature_inline_data(sb))\n+\t\treturn;\n+\tif (ext4_has_feature_verity(sb))\n+\t\treturn;\n+\tif (ext4_has_feature_encrypt(sb))\n+\t\treturn;\n+\tif (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||\n+\t    ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA))\n+\t\treturn;\n+\tif (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))\n+\t\treturn;\n+\n+\text4_set_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\n+\n+\t/*\n+\t * Install the iomap end_io handler on the shared conversion\n+\t * work.  This is safe at inode initialization and during the\n+\t * buffered I/O path changes where we flush all pending\n+\t * writebacks and drop page cache under i_rwsem and\n+\t * filemap_invalidate_lock.\n+\t */\n+\tINIT_WORK(\u0026EXT4_I(inode)-\u003ei_rsv_conversion_work, ext4_iomap_end_io);\n+}\n+\n+static void ext4_disable_buffered_iomap(struct inode *inode)\n+{\n+\text4_clear_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\n+\n+\t/*\n+\t * Reinstall the buffer_head end_io handler on the shared\n+\t * conversion work.  This is safe during the buffered I/O path\n+\t * changes where we flush all pending writebacks and drop page\n+\t * cache under i_rwsem and filemap_invalidate_lock.\n+\t */\n+\tINIT_WORK(\u0026EXT4_I(inode)-\u003ei_rsv_conversion_work, ext4_end_io_rsv_work);\n+}\n+\n void ext4_set_inode_mapping_order(struct inode *inode)\n {\n \tstruct super_block *sb = inode-\u003ei_sb;\n@@ -5595,6 +6493,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,\n \tif (ret)\n \t\tgoto bad_inode;\n \n+\text4_enable_buffered_iomap(inode);\n+\n \tif (S_ISREG(inode-\u003ei_mode)) {\n \t\tinode-\u003ei_op = \u0026ext4_file_inode_operations;\n \t\tinode-\u003ei_fop = \u0026ext4_file_operations;\n@@ -5982,6 +6882,125 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)\n \t}\n }\n \n+/*\n+ * Set i_size and i_disksize to 'newsize'.\n+ *\n+ * Both i_rwsem and i_data_sem are required here to avoid races between\n+ * generic append writeback (or zeroed pending I/O writeback) and\n+ * concurrent operations (e.g., fallocate, truncate) that also modify\n+ * i_size and i_disksize. This also ensures that the writeback ioend worker\n+ * observes the latest i_size under the same lock protection.\n+ */\n+static inline void ext4_set_inode_size(struct inode *inode, loff_t newsize)\n+{\n+\tWARN_ON_ONCE(S_ISREG(inode-\u003ei_mode) \u0026\u0026 !inode_is_locked(inode));\n+\n+\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\ti_size_write(inode, newsize);\n+\t__ext4_set_i_disksize(inode, newsize);\n+\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+}\n+\n+static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)\n+{\n+\text4_lblk_t old_lblk, new_lblk;\n+\thandle_t *handle;\n+\tint ret;\n+\n+\tif (!IS_ALIGNED(oldsize | newsize, i_blocksize(inode))) {\n+\t\tret = ext4_inode_attach_jinode(inode);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\tinode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));\n+\tif (!IS_ALIGNED(oldsize, i_blocksize(inode))) {\n+\t\tret = ext4_block_zero_eof(inode, oldsize, LLONG_MAX);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\thandle = ext4_journal_start(inode, EXT4_HT_INODE, 3);\n+\tif (IS_ERR(handle))\n+\t\treturn PTR_ERR(handle);\n+\n+\told_lblk = oldsize \u003e 0 ? (oldsize - 1) \u003e\u003e inode-\u003ei_blkbits : 0;\n+\tnew_lblk = newsize \u003e 0 ? (newsize - 1) \u003e\u003e inode-\u003ei_blkbits : 0;\n+\text4_fc_track_range(handle, inode, old_lblk, new_lblk);\n+\n+\text4_set_inode_size(inode, newsize);\n+\n+\tret = ext4_mark_inode_dirty(handle, inode);\n+\text4_journal_stop(handle);\n+\tif (ret)\n+\t\treturn ret;\n+\t/*\n+\t * isize extend must be called outside an active handle due to\n+\t * the lock ordering of transaction start and folio lock in the\n+\t * iomap buffered I/O path (folio lock -\u003e transaction start).\n+\t */\n+\tpagecache_isize_extended(inode, oldsize, newsize);\n+\treturn 0;\n+}\n+\n+static int ext4_truncate_down(struct inode *inode, loff_t oldsize,\n+\t\t\t      loff_t newsize, int *orphan)\n+{\n+\text4_lblk_t start_lblk;\n+\thandle_t *handle;\n+\tint ret;\n+\n+\t/* Do not change i_size. */\n+\tif (newsize == oldsize)\n+\t\tgoto truncate;\n+\n+\t/* Shrink. */\n+\thandle = ext4_journal_start(inode, EXT4_HT_INODE, 3);\n+\tif (IS_ERR(handle))\n+\t\treturn PTR_ERR(handle);\n+\n+\tif (ext4_handle_valid(handle)) {\n+\t\tret = ext4_orphan_add(handle, inode);\n+\t\t*orphan = 1;\n+\t\tif (ret) {\n+\t\t\text4_journal_stop(handle);\n+\t\t\treturn ret;\n+\t\t}\n+\t}\n+\n+\tstart_lblk = newsize \u003e 0 ? (newsize - 1) \u003e\u003e inode-\u003ei_blkbits : 0;\n+\text4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1);\n+\n+\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\t/*\n+\t * Truncate the zeroed EOF block invalidates the pending disksize\n+\t * update, so clear the disksize-grow-pending state.\n+\t */\n+\tif (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) \u0026\u0026\n+\t    (newsize \u003c= EXT4_I(inode)-\u003ei_disksize))\n+\t\text4_iomap_clear_disksize_pending(inode);\n+\n+\ti_size_write(inode, newsize);\n+\t__ext4_set_i_disksize(inode, newsize);\n+\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n+\n+\tret = ext4_mark_inode_dirty(handle, inode);\n+\text4_journal_stop(handle);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tif (ext4_should_journal_data(inode))\n+\t\text4_wait_for_tail_page_commit(inode);\n+truncate:\n+\t/*\n+\t * Truncate pagecache after we've waited for commit in data=journal\n+\t * mode to make pages freeable.  Call ext4_truncate() even if\n+\t * i_size didn't change to truncate possible preallocated blocks.\n+\t */\n+\ttruncate_pagecache(inode, newsize);\n+\treturn ext4_truncate(inode);\n+}\n+\n /*\n  * ext4_setattr()\n  *\n@@ -6078,9 +7097,7 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,\n \t}\n \n \tif (attr-\u003eia_valid \u0026 ATTR_SIZE) {\n-\t\thandle_t *handle;\n \t\tloff_t oldsize = inode-\u003ei_size;\n-\t\tloff_t old_disksize;\n \t\tint shrink = (attr-\u003eia_size \u003c inode-\u003ei_size);\n \n \t\tif (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {\n@@ -6131,97 +7148,14 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,\n \t\t\tgoto err_out;\n \t\t}\n \n-\t\tif (attr-\u003eia_size != inode-\u003ei_size) {\n-\t\t\t/* attach jbd2 jinode for EOF folio tail zeroing */\n-\t\t\tif (attr-\u003eia_size \u0026 (inode-\u003ei_sb-\u003es_blocksize - 1) ||\n-\t\t\t    oldsize \u0026 (inode-\u003ei_sb-\u003es_blocksize - 1)) {\n-\t\t\t\terror = ext4_inode_attach_jinode(inode);\n-\t\t\t\tif (error)\n-\t\t\t\t\tgoto out_mmap_sem;\n-\t\t\t}\n-\n-\t\t\t/*\n-\t\t\t * Update c/mtime and tail zero the EOF folio on\n-\t\t\t * truncate up. ext4_truncate() handles the shrink case\n-\t\t\t * below.\n-\t\t\t */\n-\t\t\tif (!shrink) {\n-\t\t\t\tinode_set_mtime_to_ts(inode,\n-\t\t\t\t\t\t      inode_set_ctime_current(inode));\n-\t\t\t\tif (oldsize \u0026 (inode-\u003ei_sb-\u003es_blocksize - 1)) {\n-\t\t\t\t\terror = ext4_block_zero_eof(inode,\n-\t\t\t\t\t\t\toldsize, LLONG_MAX);\n-\t\t\t\t\tif (error)\n-\t\t\t\t\t\tgoto out_mmap_sem;\n-\t\t\t\t}\n-\t\t\t}\n-\n-\t\t\thandle = ext4_journal_start(inode, EXT4_HT_INODE, 3);\n-\t\t\tif (IS_ERR(handle)) {\n-\t\t\t\terror = PTR_ERR(handle);\n-\t\t\t\tgoto out_mmap_sem;\n-\t\t\t}\n-\t\t\tif (ext4_handle_valid(handle) \u0026\u0026 shrink) {\n-\t\t\t\terror = ext4_orphan_add(handle, inode);\n-\t\t\t\torphan = 1;\n-\t\t\t}\n-\n-\t\t\tif (shrink)\n-\t\t\t\text4_fc_track_range(handle, inode,\n-\t\t\t\t\t(attr-\u003eia_size \u003e 0 ? attr-\u003eia_size - 1 : 0) \u003e\u003e\n-\t\t\t\t\tinode-\u003ei_sb-\u003es_blocksize_bits,\n-\t\t\t\t\tEXT_MAX_BLOCKS - 1);\n-\t\t\telse\n-\t\t\t\text4_fc_track_range(\n-\t\t\t\t\thandle, inode,\n-\t\t\t\t\t(oldsize \u003e 0 ? oldsize - 1 : oldsize) \u003e\u003e\n-\t\t\t\t\tinode-\u003ei_sb-\u003es_blocksize_bits,\n-\t\t\t\t\t(attr-\u003eia_size \u003e 0 ? attr-\u003eia_size - 1 : 0) \u003e\u003e\n-\t\t\t\t\tinode-\u003ei_sb-\u003es_blocksize_bits);\n-\n-\t\t\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n-\t\t\told_disksize = EXT4_I(inode)-\u003ei_disksize;\n-\t\t\tEXT4_I(inode)-\u003ei_disksize = attr-\u003eia_size;\n-\n-\t\t\t/*\n-\t\t\t * We have to update i_size under i_data_sem together\n-\t\t\t * with i_disksize to avoid races with writeback code\n-\t\t\t * running ext4_wb_update_i_disksize().\n-\t\t\t */\n-\t\t\tif (!error)\n-\t\t\t\ti_size_write(inode, attr-\u003eia_size);\n-\t\t\telse\n-\t\t\t\tEXT4_I(inode)-\u003ei_disksize = old_disksize;\n-\t\t\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n-\t\t\trc = ext4_mark_inode_dirty(handle, inode);\n-\t\t\tif (!error)\n-\t\t\t\terror = rc;\n-\t\t\text4_journal_stop(handle);\n-\t\t\tif (error)\n-\t\t\t\tgoto out_mmap_sem;\n-\t\t\tif (!shrink) {\n-\t\t\t\tpagecache_isize_extended(inode, oldsize,\n-\t\t\t\t\t\t\t inode-\u003ei_size);\n-\t\t\t} else if (ext4_should_journal_data(inode)) {\n-\t\t\t\text4_wait_for_tail_page_commit(inode);\n-\t\t\t}\n+\t\tif (attr-\u003eia_size \u003e oldsize)\n+\t\t\terror = ext4_truncate_up(inode, oldsize, attr-\u003eia_size);\n+\t\telse {\n+\t\t\t/* Shrink or do not change i_size. */\n+\t\t\terror = ext4_truncate_down(inode, oldsize,\n+\t\t\t\t\t\t   attr-\u003eia_size, \u0026orphan);\n \t\t}\n \n-\t\t/*\n-\t\t * Truncate pagecache after we've waited for commit\n-\t\t * in data=journal mode to make pages freeable.\n-\t\t */\n-\t\ttruncate_pagecache(inode, inode-\u003ei_size);\n-\t\t/*\n-\t\t * Call ext4_truncate() even if i_size didn't change to\n-\t\t * truncate possible preallocated blocks.\n-\t\t */\n-\t\tif (attr-\u003eia_size \u003c= oldsize) {\n-\t\t\trc = ext4_truncate(inode);\n-\t\t\tif (rc)\n-\t\t\t\terror = rc;\n-\t\t}\n-out_mmap_sem:\n \t\tfilemap_invalidate_unlock(inode-\u003ei_mapping);\n \t}\n \n@@ -6791,9 +7725,10 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)\n \t * the inode's in-core data-journaling state flag now.\n \t */\n \n-\tif (val)\n+\tif (val) {\n \t\text4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);\n-\telse {\n+\t\text4_disable_buffered_iomap(inode);\n+\t} else {\n \t\terr = jbd2_journal_flush(journal, 0);\n \t\tif (err \u003c 0) {\n \t\t\tjbd2_journal_unlock_updates(journal);\n@@ -6802,6 +7737,7 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)\n \t\t\treturn err;\n \t\t}\n \t\text4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);\n+\t\text4_enable_buffered_iomap(inode);\n \t}\n \text4_set_aops(inode);\n \text4_set_inode_mapping_order(inode);\n@@ -6881,6 +7817,23 @@ static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio,\n \treturn ret;\n }\n \n+static vm_fault_t ext4_iomap_page_mkwrite(struct vm_fault *vmf)\n+{\n+\tstruct inode *inode = file_inode(vmf-\u003evma-\u003evm_file);\n+\tconst struct iomap_ops *iomap_ops;\n+\n+\t/*\n+\t * ext4_nonda_switch() could writeback this folio, so have to\n+\t * call it before lock folio.\n+\t */\n+\tif (test_opt(inode-\u003ei_sb, DELALLOC) \u0026\u0026 !ext4_nonda_switch(inode-\u003ei_sb))\n+\t\tiomap_ops = \u0026ext4_iomap_buffered_da_write_ops;\n+\telse\n+\t\tiomap_ops = \u0026ext4_iomap_buffered_write_ops;\n+\n+\treturn iomap_page_mkwrite(vmf, iomap_ops, NULL);\n+}\n+\n vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)\n {\n \tstruct vm_area_struct *vma = vmf-\u003evma;\n@@ -6907,6 +7860,11 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)\n \tif (err)\n \t\tgoto out_ret;\n \n+\tif (ext4_inode_buffered_iomap(inode)) {\n+\t\tret = ext4_iomap_page_mkwrite(vmf);\n+\t\tgoto out;\n+\t}\n+\n \t/*\n \t * On data journalling we skip straight to the transaction handle:\n \t * there's no delalloc; page truncated will be checked later; the\ndiff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c\nindex 5d60ef10fe110..09931d3ba2c6a 100644\n--- a/fs/ext4/migrate.c\n+++ b/fs/ext4/migrate.c\n@@ -621,6 +621,8 @@ int ext4_ind_migrate(struct inode *inode)\n \n \tif (ext4_has_feature_bigalloc(inode-\u003ei_sb))\n \t\treturn -EOPNOTSUPP;\n+\tif (ext4_inode_buffered_iomap(inode))\n+\t\treturn -EOPNOTSUPP;\n \n \t/*\n \t * In order to get correct extent info, force all delayed allocation\ndiff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c\nindex 3329b7ad5dbd8..948ef3f44df5e 100644\n--- a/fs/ext4/move_extent.c\n+++ b/fs/ext4/move_extent.c\n@@ -476,6 +476,17 @@ static int mext_check_validity(struct inode *orig_inode,\n \t\treturn -EOPNOTSUPP;\n \t}\n \n+\t/*\n+\t * TODO: support online defrag for inodes that use the buffered\n+\t * I/O iomap path.\n+\t */\n+\tif (ext4_inode_buffered_iomap(orig_inode) ||\n+\t    ext4_inode_buffered_iomap(donor_inode)) {\n+\t\text4_msg(sb, KERN_ERR,\n+\t\t\t \"Online defrag not supported for inode with iomap buffered IO path\");\n+\t\treturn -EOPNOTSUPP;\n+\t}\n+\n \tif (donor_inode-\u003ei_mode \u0026 (S_ISUID|S_ISGID)) {\n \t\text4_debug(\"ext4 move extent: suid or sgid is set to donor file [ino:orig %llu, donor %llu]\\n\",\n \t\t\t   orig_inode-\u003ei_ino, donor_inode-\u003ei_ino);\ndiff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c\nindex 0236b6b9785a2..734d856a6d478 100644\n--- a/fs/ext4/page-io.c\n+++ b/fs/ext4/page-io.c\n@@ -22,6 +22,7 @@\n #include \u003clinux/bio.h\u003e\n #include \u003clinux/workqueue.h\u003e\n #include \u003clinux/kernel.h\u003e\n+#include \u003clinux/iomap.h\u003e\n #include \u003clinux/slab.h\u003e\n #include \u003clinux/mm.h\u003e\n #include \u003clinux/sched/mm.h\u003e\n@@ -30,6 +31,8 @@\n #include \"xattr.h\"\n #include \"acl.h\"\n \n+#include \u003ctrace/events/ext4.h\u003e\n+\n static struct kmem_cache *io_end_cachep;\n static struct kmem_cache *io_end_vec_cachep;\n \n@@ -547,3 +550,201 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *folio,\n \t\tio_submit_add_bh(io, inode, folio, bh);\n \t} while ((bh = bh-\u003eb_this_page) != head);\n }\n+\n+/*\n+ * If the current writeback range starts beyond the zeroed EOF pending\n+ * range that straddles i_disksize, wait for the zeroed data from\n+ * ext4_block_zero_eof() to be written out first. Otherwise, extending\n+ * i_disksize may expose stale data in the old EOF block.\n+ */\n+static void ext4_iomap_wb_disksize_pending_wait(struct inode *inode,\n+\t\t\t\t\t\tloff_t pos, size_t size)\n+{\n+\tloff_t disksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\tloff_t pstart, plen;\n+\n+\t/*\n+\t * Overwrite I/Os and I/Os covering the EOF block do not need to\n+\t * wait: the former do not advance i_disksize past the pending\n+\t * boundary, and the latter are the pending I/O itself (cleared in\n+\t * the bio completion path).\n+\t */\n+\tif (pos \u003c round_up(disksize, i_blocksize(inode)))\n+\t\treturn;\n+\n+\tplen = ext4_iomap_get_disksize_pending_range(inode, \u0026pstart);\n+\tif (!plen || pos \u003c pstart + plen)\n+\t\treturn;\n+\n+\ttrace_ext4_iomap_wb_disksize_pending_wait(inode, pos, size);\n+\text4_iomap_wait_disksize_pending(inode);\n+}\n+\n+static int ext4_iomap_wb_update_disksize(handle_t *handle, struct inode *inode,\n+\t\t\t\t\t loff_t end, bool is_disksize_grow)\n+{\n+\tloff_t new_disksize, i_size;\n+\tstruct ext4_inode_info *ei = EXT4_I(inode);\n+\tint ret;\n+\n+\t/*\n+\t * Races with truncate are avoided by checking i_size under\n+\t * i_data_sem.\n+\t */\n+\tdown_write(\u0026ei-\u003ei_data_sem);\n+\ti_size = i_size_read(inode);\n+\n+\t/*\n+\t * EXT4_STATE_DISKSIZE_GROW_PENDING is cleared when the pending\n+\t * I/O completes. However, another thread may have re-set the bit\n+\t * between that point and here, meaning i_disksize has already\n+\t * been advanced and a new EOF zeroing has been initiated. In that\n+\t * case, do not advance i_disksize to i_size; leave it to the\n+\t * next pending grow ioend.\n+\t */\n+\tif (is_disksize_grow \u0026\u0026\n+\t    ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))\n+\t\tis_disksize_grow = false;\n+\n+\t/*\n+\t * Update i_disksize to i_size when EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO\n+\t * completes. This is safe because we never directly allocate written\n+\t * blocks during buffered writes.\n+\t *\n+\t * This ensures that i_disksize is correctly advanced during\n+\t * truncate-up or append fallocate on a block-unaligned file,\n+\t * preventing it from remaining stale. The tradeoff is that zeroed\n+\t * data may be exposed after crash recovery if dirty data in this\n+\t * range is not yet on disk, but stale data will never be exposed.\n+\t * This is because the extent is only converted to written state\n+\t * after the data has been persisted.\n+\t */\n+\tnew_disksize = is_disksize_grow ? i_size : min(end, i_size);\n+\tif (new_disksize \u003e ei-\u003ei_disksize) {\n+\t\ttrace_ext4_iomap_wb_update_disksize(inode, end, i_size,\n+\t\t\t\tei-\u003ei_disksize, new_disksize, is_disksize_grow);\n+\t\tWRITE_ONCE(ei-\u003ei_disksize, new_disksize);\n+\t}\n+\tup_write(\u0026ei-\u003ei_data_sem);\n+\tret = ext4_mark_inode_dirty(handle, inode);\n+\tif (ret)\n+\t\tEXT4_ERROR_INODE_ERR(inode, -ret, \"Failed to mark inode dirty\");\n+\n+\treturn ret;\n+}\n+\n+static void ext4_iomap_finish_ioend(struct iomap_ioend *ioend)\n+{\n+\tstruct inode *inode = ioend-\u003eio_inode;\n+\tstruct super_block *sb = inode-\u003ei_sb;\n+\tloff_t pos = ioend-\u003eio_offset;\n+\tsize_t size = ioend-\u003eio_size;\n+\tloff_t end = pos + size;\n+\tunsigned long io_mode = (unsigned long)ioend-\u003eio_private;\n+\tbool is_disksize_grow = (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO);\n+\thandle_t *handle;\n+\tint credits;\n+\tint ret, err;\n+\n+\tret = blk_status_to_errno(ioend-\u003eio_bio.bi_status);\n+\tif (is_disksize_grow)\n+\t\ttrace_ext4_iomap_wb_disksize_pending_complete(ioend-\u003eio_inode,\n+\t\t\t\tioend-\u003eio_offset, ioend-\u003eio_size, ret);\n+\tif (unlikely(ret)) {\n+\t\tif (test_opt(sb, DATA_ERR_ABORT) \u0026\u0026 !ext4_emergency_state(sb))\n+\t\t\tjbd2_journal_abort(EXT4_SB(sb)-\u003es_journal, ret);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!(ioend-\u003eio_flags \u0026 IOMAP_IOEND_UNWRITTEN) \u0026\u0026\n+\t    end \u003c= READ_ONCE(EXT4_I(inode)-\u003ei_disksize) \u0026\u0026 !is_disksize_grow)\n+\t\tgoto out;\n+\n+\t/* Wait for disksize-pending zeroed data to be written out. */\n+\text4_iomap_wb_disksize_pending_wait(inode, pos, size);\n+\n+\t/*\n+\t * We may need to convert one extent, update the i_disksize and\n+\t * dirty the inode.\n+\t */\n+\tcredits = ext4_chunk_trans_blocks(inode,\n+\t\t\tEXT4_MAX_BLOCKS(size, pos, inode-\u003ei_blkbits));\n+\thandle = ext4_journal_start(inode, EXT4_HT_EXT_CONVERT, credits);\n+\tif (IS_ERR(handle)) {\n+\t\tret = PTR_ERR(handle);\n+\t\tgoto out_err;\n+\t}\n+\n+\t/* Update on-disk size after I/O is completed. */\n+\tif (end \u003e READ_ONCE(EXT4_I(inode)-\u003ei_disksize) || is_disksize_grow) {\n+\t\tret = ext4_iomap_wb_update_disksize(handle, inode, end,\n+\t\t\t\t\t\t    is_disksize_grow);\n+\t\tif (ret)\n+\t\t\tgoto out_journal;\n+\t}\n+\n+\tif (ioend-\u003eio_flags \u0026 IOMAP_IOEND_UNWRITTEN)\n+\t\tret = ext4_convert_unwritten_extents(handle, inode, pos,\n+\t\t\t\t\t\t     size, NULL);\n+\n+out_journal:\n+\terr = ext4_journal_stop(handle);\n+\tif (!ret)\n+\t\tret = err;\n+out_err:\n+\tif (ret \u003c 0 \u0026\u0026 !ext4_emergency_state(sb)) {\n+\t\text4_msg(sb, KERN_EMERG,\n+\t\t\t \"failed to convert unwritten extents to written extents or update inode size -- potential data loss! (inode %llu, error %d)\",\n+\t\t\t inode-\u003ei_ino, ret);\n+\t}\n+out:\n+\tiomap_finish_ioends(ioend, ret);\n+}\n+\n+/*\n+ * Work on buffered iomap completed IO, to convert unwritten extents to\n+ * mapped extents\n+ */\n+void ext4_iomap_end_io(struct work_struct *work)\n+{\n+\tstruct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,\n+\t\t\t\t\t\t  i_rsv_conversion_work);\n+\tstruct iomap_ioend *ioend;\n+\tstruct list_head ioend_list;\n+\tunsigned long flags;\n+\n+\tspin_lock_irqsave(\u0026ei-\u003ei_completed_io_lock, flags);\n+\tlist_replace_init(\u0026ei-\u003ei_rsv_conversion_list, \u0026ioend_list);\n+\tspin_unlock_irqrestore(\u0026ei-\u003ei_completed_io_lock, flags);\n+\n+\tiomap_sort_ioends(\u0026ioend_list);\n+\twhile (!list_empty(\u0026ioend_list)) {\n+\t\tioend = list_entry(ioend_list.next, struct iomap_ioend, io_list);\n+\t\tlist_del_init(\u0026ioend-\u003eio_list);\n+\t\tiomap_ioend_try_merge(ioend, \u0026ioend_list);\n+\t\text4_iomap_finish_ioend(ioend);\n+\t}\n+}\n+\n+void ext4_iomap_end_bio(struct bio *bio)\n+{\n+\tstruct iomap_ioend *ioend = iomap_ioend_from_bio(bio);\n+\tstruct ext4_inode_info *ei = EXT4_I(ioend-\u003eio_inode);\n+\tunsigned long io_mode = (unsigned long)ioend-\u003eio_private;\n+\tunsigned long flags;\n+\n+\t/*\n+\t * This is a disksize-pending I/O: clear the disksize-pending\n+\t * state set in ext4_block_zero_eof() and wake up all waiters\n+\t * that will update the inode i_disksize.\n+\t */\n+\tif (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO)\n+\t\text4_iomap_clear_disksize_pending(ioend-\u003eio_inode);\n+\n+\tspin_lock_irqsave(\u0026ei-\u003ei_completed_io_lock, flags);\n+\tif (list_empty(\u0026ei-\u003ei_rsv_conversion_list))\n+\t\tqueue_work(EXT4_SB(ioend-\u003eio_inode-\u003ei_sb)-\u003ersv_conversion_wq,\n+\t\t\t   \u0026ei-\u003ei_rsv_conversion_work);\n+\tlist_add_tail(\u0026ioend-\u003eio_list, \u0026ei-\u003ei_rsv_conversion_list);\n+\tspin_unlock_irqrestore(\u0026ei-\u003ei_completed_io_lock, flags);\n+}\ndiff --git a/fs/ext4/super.c b/fs/ext4/super.c\nindex bca0dc87d0b7c..7db34ba0ea593 100644\n--- a/fs/ext4/super.c\n+++ b/fs/ext4/super.c\n@@ -100,13 +100,21 @@ static const struct fs_parameter_spec ext4_param_specs[];\n  * Lock ordering\n  *\n  * page fault path:\n- * mmap_lock -\u003e sb_start_pagefault -\u003e invalidate_lock (r) -\u003e transaction start\n- *   -\u003e page lock -\u003e i_data_sem (rw)\n+ * - buffer_head path:\n+ *   mmap_lock -\u003e sb_start_pagefault -\u003e invalidate_lock (r) -\u003e\n+ *     transaction start -\u003e folio lock -\u003e i_data_sem (rw)\n+ * - iomap path:\n+ *   mmap_lock -\u003e sb_start_pagefault -\u003e invalidate_lock (r) -\u003e\n+ *     folio lock -\u003e transaction start -\u003e i_data_sem (rw)\n  *\n  * buffered write path:\n- * sb_start_write -\u003e i_mutex -\u003e mmap_lock\n- * sb_start_write -\u003e i_mutex -\u003e transaction start -\u003e page lock -\u003e\n- *   i_data_sem (rw)\n+ * sb_start_write -\u003e i_rwsem (w) -\u003e mmap_lock\n+ * - buffer_head path:\n+ *   sb_start_write -\u003e i_rwsem (w) -\u003e transaction start -\u003e folio lock -\u003e\n+ *     i_data_sem (rw)\n+ * - iomap path:\n+ *   sb_start_write -\u003e i_rwsem (w) -\u003e transaction start -\u003e i_data_sem (rw)\n+ *   sb_start_write -\u003e i_rwsem (w) -\u003e folio lock (not under an active handle)\n  *\n  * truncate:\n  * sb_start_write -\u003e i_mutex -\u003e invalidate_lock (w) -\u003e i_mmap_rwsem (w) -\u003e\n@@ -119,7 +127,10 @@ static const struct fs_parameter_spec ext4_param_specs[];\n  * sb_start_write -\u003e i_mutex -\u003e transaction start -\u003e i_data_sem (rw)\n  *\n  * writepages:\n- * transaction start -\u003e page lock(s) -\u003e i_data_sem (rw)\n+ * - buffer_head path:\n+ *   transaction start -\u003e folio lock(s) -\u003e i_data_sem (rw)\n+ * - iomap path:\n+ *   folio lock -\u003e transaction start -\u003e i_data_sem (rw)\n  */\n \n static const struct fs_context_operations ext4_context_ops = {\n@@ -1486,6 +1497,14 @@ static void ext4_destroy_inode(struct inode *inode)\n \t\t\t \"Inode %llu (%p): i_reserved_data_blocks (%u) not cleared!\",\n \t\t\t inode-\u003ei_ino, EXT4_I(inode),\n \t\t\t EXT4_I(inode)-\u003ei_reserved_data_blocks);\n+\n+\tif (!(EXT4_SB(inode-\u003ei_sb)-\u003es_mount_state \u0026 EXT4_ERROR_FS) \u0026\u0026\n+\t    !ext4_emergency_state(inode-\u003ei_sb) \u0026\u0026\n+\t    WARN_ON_ONCE(ext4_test_inode_state(inode,\n+\t\t\t\tEXT4_STATE_DISKSIZE_GROW_PENDING)))\n+\t\text4_msg(inode-\u003ei_sb, KERN_ERR,\n+\t\t\t \"Inode %llu (%p): EXT4_STATE_DISKSIZE_GROW_PENDING not cleared!\",\n+\t\t\t inode-\u003ei_ino, EXT4_I(inode));\n }\n \n static void ext4_shutdown(struct super_block *sb)\n@@ -1728,6 +1747,7 @@ enum {\n \tOpt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,\n \tOpt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,\n \tOpt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,\n+\tOpt_buffered_iomap, Opt_nobuffered_iomap,\n \tOpt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,\n #ifdef CONFIG_EXT4_DEBUG\n \tOpt_fc_debug_max_replay, Opt_fc_debug_force\n@@ -1866,6 +1886,8 @@ static const struct fs_parameter_spec ext4_param_specs[] = {\n \tfsparam_flag\t(\"no_prefetch_block_bitmaps\",\n \t\t\t\t\t\tOpt_no_prefetch_block_bitmaps),\n \tfsparam_s32\t(\"mb_optimize_scan\",\tOpt_mb_optimize_scan),\n+\tfsparam_flag\t(\"buffered_iomap\",\tOpt_buffered_iomap),\n+\tfsparam_flag\t(\"nobuffered_iomap\",\tOpt_nobuffered_iomap),\n \tfsparam_string\t(\"check\",\t\tOpt_removed),\t/* mount option from ext2/3 */\n \tfsparam_flag\t(\"nocheck\",\t\tOpt_removed),\t/* mount option from ext2/3 */\n \tfsparam_flag\t(\"reservation\",\t\tOpt_removed),\t/* mount option from ext2/3 */\n@@ -1959,6 +1981,10 @@ static const struct mount_opts {\n \t{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},\n \t{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,\n \t MOPT_SET},\n+\t{Opt_buffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,\n+\t MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},\n+\t{Opt_nobuffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,\n+\t MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},\n #ifdef CONFIG_EXT4_DEBUG\n \t{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,\n \t MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},\ndiff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h\nindex 7028a28316fa3..92048da9dae43 100644\n--- a/include/trace/events/ext4.h\n+++ b/include/trace/events/ext4.h\n@@ -3157,6 +3157,215 @@ TRACE_EVENT(ext4_move_extent_exit,\n \t\t  __entry-\u003eret)\n );\n \n+DECLARE_EVENT_CLASS(ext4_set_iomap_class,\n+\tTP_PROTO(struct inode *inode, struct ext4_map_blocks *map,\n+\t\t loff_t offset, loff_t length, unsigned int flags),\n+\tTP_ARGS(inode, map, offset, length, flags),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(ext4_lblk_t, m_lblk)\n+\t\t__field(ext4_fsblk_t, pblk)\n+\t\t__field(unsigned int, m_len)\n+\t\t__field(unsigned int, m_flags)\n+\t\t__field(u64, m_seq)\n+\t\t__field(loff_t, offset)\n+\t\t__field(loff_t, length)\n+\t\t__field(unsigned int, iomap_flags)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev\t\t= inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino\t\t= inode-\u003ei_ino;\n+\t\t__entry-\u003em_lblk\t\t= map-\u003em_lblk;\n+\t\t__entry-\u003epblk\t\t= map-\u003em_pblk;\n+\t\t__entry-\u003em_len\t\t= map-\u003em_len;\n+\t\t__entry-\u003em_flags\t= map-\u003em_flags;\n+\t\t__entry-\u003em_seq\t\t= map-\u003em_seq;\n+\t\t__entry-\u003eoffset\t\t= offset;\n+\t\t__entry-\u003elength\t\t= length;\n+\t\t__entry-\u003eiomap_flags\t= flags;\n+\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu m_lblk %u pblk %llu m_len %u m_flags %s m_seq %llu orig_off 0x%llx orig_len 0x%llx iomap_flags 0x%x\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev),\n+\t\t  __entry-\u003eino, __entry-\u003em_lblk, __entry-\u003epblk, __entry-\u003em_len,\n+\t\t  show_mflags(__entry-\u003em_flags), __entry-\u003em_seq,\n+\t\t  __entry-\u003eoffset, __entry-\u003elength, __entry-\u003eiomap_flags)\n+)\n+\n+#define DEFINE_SET_IOMAP_EVENT(name) \\\n+DEFINE_EVENT(ext4_set_iomap_class, name, \\\n+\tTP_PROTO(struct inode *inode, struct ext4_map_blocks *map, \\\n+\t\t loff_t offset, loff_t length, unsigned int flags), \\\n+\tTP_ARGS(inode, map, offset, length, flags))\n+\n+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_read_begin);\n+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_write_begin);\n+DEFINE_SET_IOMAP_EVENT(ext4_iomap_map_writeback_range);\n+DEFINE_SET_IOMAP_EVENT(ext4_iomap_zero_begin);\n+\n+DECLARE_EVENT_CLASS(ext4_iomap_disksize_pending,\n+\tTP_PROTO(struct inode *inode),\n+\tTP_ARGS(inode),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(loff_t, i_disksize)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev = inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino = inode-\u003ei_ino;\n+\t\t__entry-\u003ei_disksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu i_disksize %lld\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev),\n+\t\t  __entry-\u003eino, __entry-\u003ei_disksize)\n+);\n+\n+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_mark_disksize_pending,\n+\tTP_PROTO(struct inode *inode),\n+\tTP_ARGS(inode)\n+);\n+\n+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_clear_disksize_pending,\n+\tTP_PROTO(struct inode *inode),\n+\tTP_ARGS(inode)\n+);\n+\n+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_wait_disksize_pending,\n+\tTP_PROTO(struct inode *inode),\n+\tTP_ARGS(inode)\n+);\n+\n+/* disksize pending I/O tracepoints for iomap Buffered I/O path */\n+DECLARE_EVENT_CLASS(ext4_iomap_wb_disksize_pending,\n+\tTP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),\n+\tTP_ARGS(inode, io_offset, io_size),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(loff_t, io_offset)\n+\t\t__field(size_t, io_size)\n+\t\t__field(loff_t, i_size)\n+\t\t__field(loff_t, i_disksize)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev = inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino = inode-\u003ei_ino;\n+\t\t__entry-\u003eio_offset = io_offset;\n+\t\t__entry-\u003eio_size = io_size;\n+\t\t__entry-\u003ei_size = i_size_read(inode);\n+\t\t__entry-\u003ei_disksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu io_offset %lld io_size %zu i_size %lld i_disksize %lld\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev),\n+\t\t  __entry-\u003eino, __entry-\u003eio_offset, __entry-\u003eio_size,\n+\t\t  __entry-\u003ei_size, __entry-\u003ei_disksize)\n+);\n+\n+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,\n+\text4_iomap_wb_disksize_pending_submit,\n+\tTP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),\n+\tTP_ARGS(inode, io_offset, io_size)\n+);\n+\n+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,\n+\text4_iomap_wb_disksize_pending_wait,\n+\tTP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),\n+\tTP_ARGS(inode, io_offset, io_size)\n+);\n+\n+TRACE_EVENT(ext4_iomap_wb_disksize_pending_complete,\n+\tTP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size,\n+\t\t int ret),\n+\tTP_ARGS(inode, io_offset, io_size, ret),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(loff_t, io_offset)\n+\t\t__field(size_t, io_size)\n+\t\t__field(loff_t, i_size)\n+\t\t__field(loff_t, i_disksize)\n+\t\t__field(int, ret)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev = inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino = inode-\u003ei_ino;\n+\t\t__entry-\u003eio_offset = io_offset;\n+\t\t__entry-\u003eio_size = io_size;\n+\t\t__entry-\u003ei_size = i_size_read(inode);\n+\t\t__entry-\u003ei_disksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\t\t__entry-\u003eret = ret;\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu io_offset %lld io_size %zu ret %d i_size %lld i_disksize %lld\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev),\n+\t\t  __entry-\u003eino, __entry-\u003eio_offset, __entry-\u003eio_size,\n+\t\t  __entry-\u003eret, __entry-\u003ei_size, __entry-\u003ei_disksize)\n+);\n+\n+/* i_disksize update tracepoint */\n+TRACE_EVENT(ext4_iomap_wb_update_disksize,\n+\tTP_PROTO(struct inode *inode, loff_t end, loff_t i_size,\n+\t\t loff_t i_disksize, loff_t new_disksize,\n+\t\t bool is_disksize_grow),\n+\tTP_ARGS(inode, end, i_size, i_disksize, new_disksize,\n+\t\tis_disksize_grow),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(loff_t, end)\n+\t\t__field(loff_t, i_size)\n+\t\t__field(loff_t, i_disksize)\n+\t\t__field(loff_t, new_disksize)\n+\t\t__field(bool, is_disksize_grow)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev = inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino = inode-\u003ei_ino;\n+\t\t__entry-\u003eend = end;\n+\t\t__entry-\u003ei_size = i_size;\n+\t\t__entry-\u003ei_disksize = i_disksize;\n+\t\t__entry-\u003enew_disksize = new_disksize;\n+\t\t__entry-\u003eis_disksize_grow = is_disksize_grow;\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu end %lld i_size %lld i_disksize %lld new_disksize %lld is_disksize_grow %d\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev),\n+\t\t  __entry-\u003eino, __entry-\u003eend, __entry-\u003ei_size,\n+\t\t  __entry-\u003ei_disksize, __entry-\u003enew_disksize,\n+\t\t  __entry-\u003eis_disksize_grow)\n+);\n+\n+/* Block zero EOF tracepoint */\n+TRACE_EVENT(ext4_block_zero_eof,\n+\tTP_PROTO(struct inode *inode, loff_t from, loff_t length,\n+\t\t bool did_zero, bool zero_written),\n+\tTP_ARGS(inode, from, length, did_zero, zero_written),\n+\tTP_STRUCT__entry(\n+\t\t__field(dev_t, dev)\n+\t\t__field(u64, ino)\n+\t\t__field(loff_t, from)\n+\t\t__field(loff_t, length)\n+\t\t__field(loff_t, i_size)\n+\t\t__field(loff_t, i_disksize)\n+\t\t__field(bool, did_zero)\n+\t\t__field(bool, zero_written)\n+\t),\n+\tTP_fast_assign(\n+\t\t__entry-\u003edev = inode-\u003ei_sb-\u003es_dev;\n+\t\t__entry-\u003eino = inode-\u003ei_ino;\n+\t\t__entry-\u003efrom = from;\n+\t\t__entry-\u003elength = length;\n+\t\t__entry-\u003ei_size = inode-\u003ei_size;\n+\t\t__entry-\u003ei_disksize = READ_ONCE(EXT4_I(inode)-\u003ei_disksize);\n+\t\t__entry-\u003edid_zero = did_zero;\n+\t\t__entry-\u003ezero_written = zero_written;\n+\t),\n+\tTP_printk(\"dev %d:%d ino %llu zero EOF from %lld length %lld i_size %lld i_disksize %lld did_zero %d zero_written %d\",\n+\t\t  MAJOR(__entry-\u003edev), MINOR(__entry-\u003edev), __entry-\u003eino,\n+\t\t  __entry-\u003efrom, __entry-\u003elength, __entry-\u003ei_size,\n+\t\t  __entry-\u003ei_disksize, __entry-\u003edid_zero, __entry-\u003ezero_written)\n+);\n+\n #endif /* _TRACE_EXT4_H */\n \n /* This part must be outside protection */\n"
}

2/1 2026/10/09 13:28 agent patch-evaluator
3m Model:
core

Args:
null

Results:
{
  "EnableConfigs": [
    "EXT4_FS"
  ],
  "FocusSymbols": [
    "ext4_enable_buffered_iomap",
    "ext4_iomap_buffered_write",
    "ext4_iomap_writepages",
    "ext4_iomap_page_mkwrite",
    "ext4_iomap_end_io",
    "ext4_iomap_end_bio",
    "ext4_iomap_sync_zeroed_eof",
    "ext4_convert_unwritten_extents",
    "ext4_truncate_up",
    "ext4_truncate_down"
  ],
  "Reasoning": "The patch implements buffered I/O via the iomap framework in ext4 (enabled via the 'buffered_iomap' mount option). It adds iomap read/write/writeback/page_mkwrite operations, alters unwritten extent conversion, restructures truncate-up and truncate-down handling, adds disksize-grow-pending synchronization logic, and adds new assertions (WARN_ON_ONCE). This is core filesystem code fully reachable in virtualized environments.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer and security engineer.
Your job is to review a provided patch series and evaluate whether it warrants fuzzing with syzkaller.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on internal assumptions. You must actively use your code access
tools to inspect the actual source code, callers, and surrounding context.

================================================================================
1. CORE TRIAGE PHILOSOPHY
================================================================================
The goal of patch fuzzing is to discover crashes, regressions, exposed latent bugs,
and newly triggered assertions introduced by the patch series.

- REACHABILITY IS THE PRIMARY GATE:
  Fuzzing can only discover bugs in code that can actually execute in standard virtualized
  environments (GCE or QEMU, utilizing software-emulated devices like USB gadgets, netdev, tun/tap).
  If the modified code is structurally unreachable (see Section 2), it MUST NOT be fuzzed,
  regardless of whether it adds assertions or complex logic.

- DO NOT BLINDLY TRUST "NO FUNCTIONAL CHANGE" (NFCI) OR "REFACTORING" CLAIMS:
  Patch authors routinely label changes as "cleanups", "refactorings", or state
  "No functional change intended". Do NOT take these claims at face value.
  Code refactorings that rearrange logic, introduce helper functions, or alter state management
  in core subsystems frequently introduce subtle semantic shifts or uncover latent kernel bugs.
  If reachable executable code is modified or refactored, it MUST be fuzzed.

- NEW OR MODIFIED ASSERTIONS IN REACHABLE CODE MUST BE FUZZED:
  When a patch introduces or modifies runtime checks or assertions (e.g., WARN_ON*, VM_WARN_ON*,
  BUG_ON*, lockdep_assert*) in reachable code paths, it enforces new or stricter invariants.
  Even if the author believes the invariant always holds, fuzzing is essential to verify whether
  an unusual sequence of operations can violate it.

================================================================================
2. WHEN TO RETURN WorthFuzzing=false (NEGATIVE CRITERIA)
================================================================================
Return WorthFuzzing=false ONLY IF all modified code falls strictly into one or more of these categories:

- Non-kernel and non-executable changes:
  * Modifications to Documentation/, comments, or spelling fixes.
  * User-space directories, self-tests, samples, or scripts (e.g., tools/, samples/, scripts/, usr/)
    that do not affect the compiled kernel image (vmlinux) or kernel modules.
  * Purely decorative logging (e.g., message strings in pr_err, printk, dev_info) or tracepoints
    that do not alter control flow or data structures.
  * Build system or Kconfig changes that do not alter compiled C logic.
- Structurally unreachable hardware:
  * Vendor-specific PCIe switches, SmartNICs, or GPU drivers (e.g., mlxsw, pds_core, qed,
    ionic, amdgpu) requiring physical ASIC/PCIe cards not emulated in standard QEMU.
- Unreachable execution paths:
  * Driver teardown callbacks (.remove, .shutdown, pci_unregister_driver) executed only during
    physical PCI hot-unplug or manual sysfs driver unbinding.
  * Code paths exclusive to architectures other than the target architecture.

================================================================================
3. WHEN TO RETURN WorthFuzzing=true (POSITIVE CRITERIA)
================================================================================
Return WorthFuzzing=true whenever the patch touches reachable executable code, including:
- Core Subsystems:
  * Any logic modifications in memory management (mm/), synchronization/locking (kernel/locking/),
    BPF, scheduler, core networking, VFS, or syscall handling.
- Refactorings and Code Cleanups:
  * Any restructuring of reachable data structures, helper abstractions, or algorithm flows.
- Runtime Assertions and Defensive Checks:
  * Any introduction or alteration of assertions (WARN_ON*, VM_WARN_ON*, BUG_ON*, etc.) in reachable paths.
- Reachable Drivers and Protocols:
  * Drivers accessible via virtual buses (virtio, USB gadget, loopback, netlink, binder, sockets, etc.).

================================================================================
4. EXTRACTING FocusSymbols (PREVENTING DILUTION)
================================================================================
When WorthFuzzing=true, you must extract specific kernel functions into FocusSymbols to guide the fuzzer:

- AVOID UBIQUITOUS LIFECYCLE HOT-PATHS:
  Do NOT list generic, ubiquitous functions called by almost every program in the corpus
  (including, but not limited to: general memory allocators and deallocators, page fault
  and trap handlers, or core synchronization primitives; this is not an exhaustive list).
  Listing ubiquitous functions causes the fuzzer to classify thousands of unrelated tests as "focused",
  which severely dilutes fuzzing effort away from the actual changes.

- TARGET SPECIFIC FEATURE LOGIC AND ENTRYPOINTS:
  List functions that specifically implement the logic being added or altered, or direct API entrypoints
  for the subsystem feature under review.

- HANDLING STATIC INLINE FUNCTIONS IN HEADERS (.h):
  Compiler-inlined static functions (such as static inlines in mm/*.h or include/linux/*.h) lack
  distinct symbol addresses in vmlinux and cannot be targeted directly by symbol coverage filters.
  If the changes are primarily in static inline helpers, identify non-static, feature-specific caller
  functions in .c files that exercise them (avoiding ubiquitous lifecycle wrappers).

================================================================================
5. IDENTIFYING EnableConfigs
================================================================================
Identify any specific CONFIG_ options required to properly compile and reach the modified code:
- Inspect Kconfig files and #ifdef guards; do not make assumptions.
- Check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed.
- Strip any 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 747cfe98d30bc59325d25d500da86aa585c6ddae
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Oct 9 13:28:25 2026 +0000

    syz-cluster: applied patch under review

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 724a27e8be613..8cfe77b45e21b 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1208,8 +1208,10 @@ struct ext4_inode_info {
 	/* Lock protecting lists below */
 	spinlock_t i_completed_io_lock;
 	/*
-	 * Completed IOs that need unwritten extents handling and have
-	 * transaction reserved
+	 * Completed IOs that need unwritten extents handling and have a
+	 * transaction reserved for the buffer_head writeback path, and
+	 * also used by the iomap writeback path to queue ioends needing
+	 * unwritten extents conversion, i_disksize update, etc.
 	 */
 	struct list_head i_rsv_conversion_list;
 	struct work_struct i_rsv_conversion_work;
@@ -1319,6 +1321,7 @@ struct ext4_inode_info {
 						    * scanning in mballoc
 						    */
 #define EXT4_MOUNT2_ABORT		0x00000100 /* Abort filesystem */
+#define EXT4_MOUNT2_BUFFERED_IOMAP	0x00000200 /* Use iomap for buffered I/O */
 
 #define clear_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt &= \
 						~EXT4_MOUNT_##opt
@@ -2049,6 +2052,10 @@ enum {
 	EXT4_STATE_FC_FLUSHING_DATA,	/* Fast commit flushing data */
 	EXT4_STATE_ORPHAN_FILE,		/* Inode orphaned in orphan file */
 	EXT4_STATE_FC_REQUEUE,		/* Inode modified during fast commit */
+	EXT4_STATE_BUFFERED_IOMAP,	/* Inode use iomap for buffered IO */
+	EXT4_STATE_DISKSIZE_GROW_PENDING,
+					/* Has zeroed EOF block straddles
+					 * i_disksize awaiting writeback */
 };
 
 #define EXT4_INODE_BIT_FNS(name, field, offset)				\
@@ -2148,6 +2155,12 @@ static inline struct mapping_metadata_bhs *ext4_i_metadata_bhs(
 	return READ_ONCE(EXT4_I(inode)->i_metadata_bhs);
 }
 
+/* Whether the inode pass through the iomap infrastructure for buffered I/O */
+static inline bool ext4_inode_buffered_iomap(struct inode *inode)
+{
+	return ext4_test_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+}
+
 /*
  * Codes for operating systems
  */
@@ -3158,6 +3171,8 @@ int ext4_walk_page_buffers(handle_t *handle,
 int do_journal_get_write_access(handle_t *handle, struct inode *inode,
 				struct buffer_head *bh);
 void ext4_set_inode_mapping_order(struct inode *inode);
+void ext4_enable_buffered_iomap(struct inode *inode);
+int ext4_nonda_switch(struct super_block *sb);
 #define FALL_BACK_TO_NONDELALLOC 1
 #define EXT4_WRITE_DATA_INLINE	 2
 
@@ -3209,6 +3224,12 @@ extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
 extern int ext4_chunk_trans_extent(struct inode *inode, int nrblocks);
 extern int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
 				  int pextents, int alloc_extents);
+void ext4_iomap_clear_disksize_pending(struct inode *inode);
+void ext4_iomap_wait_disksize_pending(struct inode *inode);
+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,
+						   loff_t *start);
+extern int ext4_iomap_sync_zeroed_eof(struct inode *inode,
+				      loff_t offset, loff_t end);
 extern int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end);
 
 #define EXT4_PARTIAL_ZERO_START	0x1
@@ -3588,30 +3609,67 @@ do {								\
 #define EXT4_FREECLUSTERS_WATERMARK 0
 #endif
 
-/* Update i_disksize. Requires i_rwsem to avoid races with truncate */
+/*
+ * Update i_disksize. Requires i_rwsem to avoid races with truncate.
+ *
+ * In the iomap buffered I/O path, the EXT4_STATE_DISKSIZE_GROW_PENDING
+ * inode state bit indicates that the zeroed EOF partial block which
+ * straddles i_disksize is still waiting writeback.  In that case,
+ * i_disksize will be updated after the pending zeroed data has been
+ * written out.
+ */
 static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
 {
 	WARN_ON_ONCE(S_ISREG(inode->i_mode) &&
 		     !inode_is_locked(inode));
 	down_write(&EXT4_I(inode)->i_data_sem);
-	if (newsize > EXT4_I(inode)->i_disksize)
+	if (newsize > EXT4_I(inode)->i_disksize &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
 		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
 	up_write(&EXT4_I(inode)->i_data_sem);
 }
 
-/* Update i_size, i_disksize. Requires i_rwsem to avoid races with truncate */
+static inline void __ext4_set_i_disksize(struct inode *inode, loff_t newsize)
+{
+	WARN_ON_ONCE(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
+
+	if (newsize < EXT4_I(inode)->i_disksize ||
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
+}
+
+/*
+ * Update i_size and i_disksize to @newsize.  Requires i_rwsem to avoid
+ * races with truncate.
+ *
+ * In the iomap buffered I/O path, i_disksize is updated only if no zeroed
+ * pending block straddles i_disksize (EXT4_STATE_DISKSIZE_GROW_PENDING
+ * clear), otherwise the ioend worker for the pending block will advance
+ * i_disksize once the pending block is written back.  Both updates happen
+ * under i_data_sem so that the writeback ioend worker can always see the
+ * latest i_size under the same semaphore.
+ *
+ * Returns 0 if nothing changed, 1 if i_size was raised, 2 if i_disksize
+ * was raised, or 3 if both were.
+ */
 static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
 {
 	int changed = 0;
 
+	if (newsize <= inode->i_size && newsize <= EXT4_I(inode)->i_disksize)
+		return 0;
+
+	down_write(&EXT4_I(inode)->i_data_sem);
 	if (newsize > inode->i_size) {
 		i_size_write(inode, newsize);
 		changed = 1;
 	}
-	if (newsize > EXT4_I(inode)->i_disksize) {
-		ext4_update_i_disksize(inode, newsize);
+	if (newsize > EXT4_I(inode)->i_disksize &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING)) {
+		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
 		changed |= 2;
 	}
+	up_write(&EXT4_I(inode)->i_data_sem);
 	return changed;
 }
 
@@ -3969,6 +4027,12 @@ extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
 			     __u64 len, __u64 *moved_len);
 
 /* page-io.c */
+/*
+ * The I/O range covers the zeroed EOF block that straddles i_disksize
+ * and will advance it upon completion.
+ */
+#define EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO	1UL
+
 extern int __init ext4_init_pageio(void);
 extern void ext4_exit_pageio(void);
 extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
@@ -3983,6 +4047,8 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *page,
 		size_t len);
 extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);
 extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);
+extern void ext4_iomap_end_io(struct work_struct *work);
+extern void ext4_iomap_end_bio(struct bio *bio);
 
 /* mmp.c */
 extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
@@ -4040,6 +4106,9 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
 
 extern const struct iomap_ops ext4_iomap_ops;
 extern const struct iomap_ops ext4_iomap_report_ops;
+extern const struct iomap_ops ext4_iomap_buffered_write_ops;
+extern const struct iomap_ops ext4_iomap_buffered_da_write_ops;
+extern const struct iomap_write_ops ext4_iomap_write_ops;
 
 int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		unsigned flags, struct iomap *iomap, struct iomap *srcmap);
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 53ddedb52a6f7..a4664ddecdcd6 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -17,8 +17,12 @@ int ext4_inode_journal_mode(struct inode *inode)
 	    test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
 	    (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) &&
 	    !test_opt(inode->i_sb, DELALLOC))) {
-		/* We do not support data journalling for encrypted data */
-		if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
+		/*
+		 * We do not support data journalling for encrypted data
+		 * and buffered IOMAP path.
+		 */
+		if (S_ISREG(inode->i_mode) &&
+		    (IS_ENCRYPTED(inode) || ext4_inode_buffered_iomap(inode)))
 			return EXT4_INODE_ORDERED_DATA_MODE;  /* ordered */
 		return EXT4_INODE_JOURNAL_DATA_MODE;	/* journal data */
 	}
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 2fbf48b3dfe21..be54e93bde0b1 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -379,7 +379,12 @@ static inline int ext4_should_journal_data(struct inode *inode)
 
 static inline int ext4_should_order_data(struct inode *inode)
 {
-	return ext4_inode_journal_mode(inode) & EXT4_INODE_ORDERED_DATA_MODE;
+	/*
+	 * inodes using the iomap buffered I/O path do not use the
+	 * data=ordered mode.
+	 */
+	return !ext4_inode_buffered_iomap(inode) &&
+		(ext4_inode_journal_mode(inode) & EXT4_INODE_ORDERED_DATA_MODE);
 }
 
 static inline int ext4_should_writeback_data(struct inode *inode)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 76038b6c36552..a10d8c6e3cf1f 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4561,7 +4561,7 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)
 	 */
 
 	/* we have to know where to truncate from in crash case */
-	EXT4_I(inode)->i_disksize = inode->i_size;
+	__ext4_set_i_disksize(inode, inode->i_size);
 	err = ext4_mark_inode_dirty(handle, inode);
 	if (err)
 		return err;
@@ -4857,6 +4857,17 @@ static long ext4_zero_range(struct file *file, loff_t offset,
 		if (ret)
 			return ret;
 	}
+
+	/*
+	 * In SYNC mode, sync the pending zeroed EOF block to ensure the
+	 * i_disksize update is persisted.
+	 */
+	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && new_size) {
+		ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+		if (ret)
+			return ret;
+	}
+
 	/* Finish zeroing out if it doesn't contain partial block */
 	if (IS_ALIGNED(offset | end, blocksize))
 		return ret;
@@ -4928,10 +4939,20 @@ static long ext4_do_fallocate(struct file *file, loff_t offset,
 	if (ret)
 		goto out;
 
-	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) &&
-	    EXT4_SB(inode->i_sb)->s_journal) {
-		ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
-					EXT4_I(inode)->i_sync_tid);
+	if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
+		/*
+		 * Sync the pending zeroed EOF block to ensure the
+		 * i_disksize update is persisted.
+		 */
+		if (new_size) {
+			ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+			if (ret)
+				goto out;
+		}
+		if (EXT4_SB(inode->i_sb)->s_journal) {
+			ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
+						EXT4_I(inode)->i_sync_tid);
+		}
 	}
 out:
 	trace_ext4_fallocate_exit(inode, offset,
@@ -5137,27 +5158,36 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 	int ret = 0, ret2 = 0, ret3 = 0;
 	struct ext4_map_blocks map;
 	unsigned int blkbits = inode->i_blkbits;
-	unsigned int credits = 0;
+	unsigned int credits;
+	bool internal_handle = !handle;
 
 	map.m_lblk = offset >> blkbits;
 	map.m_len = max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
 
-	if (!handle) {
-		/*
-		 * credits to insert 1 extent into extent tree
-		 */
-		credits = ext4_chunk_trans_blocks(inode, max_blocks);
+	/* Credits to convert one extent in this range to written state. */
+	credits = ext4_meta_trans_blocks(inode, max_blocks, 1, 0);
+
+	if (internal_handle) {
+		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);
+		if (IS_ERR(handle)) {
+			ret = PTR_ERR(handle);
+			goto out;
+		}
 	}
 
 	while (max_blocks) {
-		if (credits) {
-			handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
-						    credits);
-			if (IS_ERR(handle)) {
-				ret = PTR_ERR(handle);
-				break;
-			}
-		}
+		/*
+		 * The caller cannot know how many extents the range covers,
+		 * so make sure the transaction can take one more extent,
+		 * extending or restarting it once it is full.  The reserved
+		 * handle of the buffer_head writeback path is safe as it has
+		 * enough credits for the conversion, so it is not restarted
+		 * here.
+		 */
+		ret = ext4_journal_ensure_credits(handle, credits, 0);
+		if (ret < 0)
+			break;
+
 		/*
 		 * Do not cache any unrelated extents, as it does not hold the
 		 * i_rwsem or invalidate_lock, which could corrupt the extent
@@ -5177,11 +5207,6 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 		}
 
 		ret2 = ext4_mark_inode_dirty(handle, inode);
-		if (credits) {
-			ret3 = ext4_journal_stop(handle);
-			if (unlikely(ret3))
-				ret2 = ret3;
-		}
 		ret = ret < 0 ? ret : ret2;
 		if (ret)
 			break;
@@ -5189,6 +5214,13 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 		map.m_lblk += map.m_len;
 		map.m_len = (max_blocks -= map.m_len);
 	}
+
+	if (internal_handle) {
+		ret3 = ext4_journal_stop(handle);
+		if (!ret)
+			ret = ret3;
+	}
+out:
 	/* Converted some or all blocks successfully? */
 	if (converted)
 		*converted = conv_blocks;
@@ -5203,8 +5235,8 @@ int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
 
 	/*
 	 * This is somewhat ugly but the idea is clear: When transaction is
-	 * reserved, everything goes into it. Otherwise we rather start several
-	 * smaller transactions for conversion of each extent separately.
+	 * reserved, everything goes into it. Otherwise the conversion runs
+	 * its own transaction, extending or restarting it as it goes.
 	 */
 	if (handle) {
 		handle = ext4_journal_start_reserved(handle,
@@ -5662,6 +5694,14 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
 	if (end >= inode->i_size)
 		return -EINVAL;
 
+	/*
+	 * Persist the pending zeroed EOF block to ensure i_disksize
+	 * can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+	if (ret)
+		return ret;
+
 	/*
 	 * Write tail of the last page before removed range and data that
 	 * will be shifted since they will get removed from the page cache
@@ -5711,7 +5751,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
 
 	new_size = inode->i_size - len;
 	i_size_write(inode, new_size);
-	EXT4_I(inode)->i_disksize = new_size;
+	__ext4_set_i_disksize(inode, new_size);
 
 	up_write(&EXT4_I(inode)->i_data_sem);
 	ret = ext4_mark_inode_dirty(handle, inode);
@@ -5764,6 +5804,14 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
 	if (len > inode->i_sb->s_maxbytes - inode->i_size)
 		return -EFBIG;
 
+	/*
+	 * Persist the pending zeroed EOF block to ensure i_disksize
+	 * can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+	if (ret)
+		return ret;
+
 	/*
 	 * Write out all dirty pages. Need to round down to align start offset
 	 * to page size boundary for page size > block size.
@@ -5783,8 +5831,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
 	ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
 
 	/* Expand file to avoid data loss if there is error while shifting */
-	inode->i_size += len;
-	EXT4_I(inode)->i_disksize += len;
+	ext4_update_inode_size(inode, inode->i_size + len);
 	ret = ext4_mark_inode_dirty(handle, inode);
 	if (ret)
 		goto out_handle;
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 374b4bc25bd5d..50d3c92709c8b 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -330,6 +330,21 @@ static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
 	return count;
 }
 
+static ssize_t ext4_iomap_buffered_write(struct kiocb *iocb,
+					 struct iov_iter *from)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+	const struct iomap_ops *iomap_ops;
+
+	if (test_opt(inode->i_sb, DELALLOC) && !ext4_nonda_switch(inode->i_sb))
+		iomap_ops = &ext4_iomap_buffered_da_write_ops;
+	else
+		iomap_ops = &ext4_iomap_buffered_write_ops;
+
+	return iomap_file_buffered_write(iocb, from, iomap_ops,
+					 &ext4_iomap_write_ops, NULL);
+}
+
 static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 					struct iov_iter *from)
 {
@@ -351,7 +366,10 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 	if (ret <= 0)
 		goto out;
 
-	ret = generic_perform_write(iocb, from);
+	if (ext4_inode_buffered_iomap(inode))
+		ret = ext4_iomap_buffered_write(iocb, from);
+	else
+		ret = generic_perform_write(iocb, from);
 
 out:
 	inode_unlock(inode);
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index a5831fc536db9..f97a2f4904ebb 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -1346,6 +1346,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 		}
 	}
 
+	ext4_enable_buffered_iomap(inode);
 	ext4_set_inode_mapping_order(inode);
 
 	ext4_update_inode_fsync_trans(handle, inode, 1);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 26f0f9714f03c..1853fbdc2a5af 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -44,6 +44,7 @@
 #include <linux/iversion.h>
 
 #include "ext4_jbd2.h"
+#include "ext4_extents.h"
 #include "xattr.h"
 #include "acl.h"
 #include "truncate.h"
@@ -125,6 +126,70 @@ void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
 		raw->i_checksum_hi = cpu_to_le16(csum >> 16);
 }
 
+/*
+ * Clear the disksize-grow-pending state and wake up all waiters.
+ * Called when the pending zeroed EOF block which straddles i_disksize
+ * has completed writeback or its folio is discarded.
+ */
+void ext4_iomap_clear_disksize_pending(struct inode *inode)
+{
+	if (!test_and_clear_wake_up_bit(
+		ext4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),
+		ext4_inode_state_wait_word(inode)))
+		return;
+
+	trace_ext4_iomap_clear_disksize_pending(inode);
+}
+
+/*
+ * Wait for the disksize-grow-pending zeroed EOF block which straddles
+ * i_disksize to be written back or cleared.
+ */
+void ext4_iomap_wait_disksize_pending(struct inode *inode)
+{
+	/* Only emit the trace when the bit is actually set */
+	if (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		trace_ext4_iomap_wait_disksize_pending(inode);
+
+	wait_on_bit(ext4_inode_state_wait_word(inode),
+		    ext4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),
+		    TASK_UNINTERRUPTIBLE);
+}
+
+/*
+ * Get the range of the disksize-grow-pending zeroed EOF block range
+ * which straddles i_disksize if the EXT4_STATE_DISKSIZE_GROW_PENDING
+ * bit is set.
+ *
+ * Return the pending range, or zero if the BIT has already been cleared.
+ */
+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,
+						   loff_t *start)
+{
+	unsigned int blocksize = i_blocksize(inode);
+	loff_t disksize;
+
+	if (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		return 0;
+
+	/*
+	 * The pending bit should be set only when i_disksize is not
+	 * block-size aligned. While set, i_disksize must not be advanced,
+	 * and the bit must be cleared when i_disksize is shrunk.
+	 */
+	down_read(&EXT4_I(inode)->i_data_sem);
+	disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	if (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) ||
+	    WARN_ON_ONCE(IS_ALIGNED(disksize, blocksize))) {
+		up_read(&EXT4_I(inode)->i_data_sem);
+		return 0;
+	}
+
+	up_read(&EXT4_I(inode)->i_data_sem);
+	*start = disksize;
+	return blocksize - (disksize & (blocksize - 1));
+}
+
 static inline int ext4_begin_ordered_truncate(struct inode *inode,
 					      loff_t new_size)
 {
@@ -208,6 +273,8 @@ void ext4_evict_inode(struct inode *inode)
 
 	if (ext4_should_order_data(inode))
 		ext4_begin_ordered_truncate(inode, 0);
+	if (ext4_inode_buffered_iomap(inode))
+		ext4_iomap_clear_disksize_pending(inode);
 	truncate_inode_pages_final(&inode->i_data);
 
 	/*
@@ -686,6 +753,11 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  * based files
  *
+ * ext4_map_blocks() may also be called with a NULL @handle. In that case
+ * it starts its own transaction handle only when block allocation is
+ * actually needed, i.e. after the lookup confirms the blocks are not yet
+ * mapped, and stops it before returning.
+ *
  * On success, it returns the number of blocks being mapped or allocated.
  * If flags doesn't contain EXT4_GET_BLOCKS_CREATE the blocks are
  * pre-allocated and unwritten, the resulting @map is marked as unwritten.
@@ -703,7 +775,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	struct extent_status es;
 	int retval;
 	int ret = 0;
-	unsigned int orig_mlen = map->m_len;
+	bool internal_handle = false;
+	unsigned int orig_mlen;
 #ifdef ES_AGGRESSIVE_TEST
 	struct ext4_map_blocks orig_map;
 
@@ -719,6 +792,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 */
 	if (unlikely(map->m_len > INT_MAX))
 		map->m_len = INT_MAX;
+	orig_mlen = map->m_len;
 
 	/* We can handle the block number less than EXT_MAX_BLOCKS */
 	if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
@@ -734,6 +808,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	else
 		ext4_check_map_extents_env(inode);
 
+create_retry:
 	/* Lookup extent status tree firstly */
 	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
@@ -784,12 +859,14 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	down_read(&EXT4_I(inode)->i_data_sem);
 	retval = ext4_map_query_blocks(handle, inode, map, flags);
 	up_read((&EXT4_I(inode)->i_data_sem));
+	if (retval < 0)
+		goto out;
 
 found:
 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
 		ret = check_block_validity(inode, map);
 		if (ret != 0)
-			return ret;
+			goto out;
 	}
 
 	/* If it is only a block(s) look up */
@@ -802,15 +879,42 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 * Note that if blocks have been preallocated
 	 * ext4_ext_map_blocks() returns with buffer head unmapped
 	 */
-	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
+	if (retval > 0) {
 		/*
-		 * If we need to convert extent to unwritten
-		 * we continue and do the actual work in
-		 * ext4_ext_map_blocks()
+		 * If we need to convert written extent to unwritten or
+		 * convert unwritten extent to written, continue and do
+		 * the actual work in ext4_ext_map_blocks().
 		 */
-		if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
-			return retval;
+		if (map->m_flags & EXT4_MAP_MAPPED &&
+		    !(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
+			goto out;
+		if (map->m_flags & EXT4_MAP_UNWRITTEN &&
+		    (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) &&
+		    !(flags & EXT4_GET_BLOCKS_CONVERT)) {
+			/* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */
+			ret = check_block_validity(inode, map);
+			if (!ret)
+				map->m_flags |= EXT4_MAP_MAPPED;
+			goto out;
+		}
+	} else if (retval == 0) {
+		/*
+		 * Do not allocate blocks for holes in the context of
+		 * data submission path.
+		 */
+		if (!map->m_flags && (flags & EXT4_GET_BLOCKS_IO_SUBMIT))
+			goto out;
+	}
 
+	if (!handle) {
+		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
+				ext4_chunk_trans_blocks(inode, orig_mlen));
+		if (IS_ERR(handle)) {
+			ret = PTR_ERR(handle);
+			goto out;
+		}
+		internal_handle = true;
+	}
 
 	ext4_fc_track_inode(handle, inode);
 	/*
@@ -820,18 +924,33 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 * with create == 1 flag.
 	 */
 	down_write(&EXT4_I(inode)->i_data_sem);
+
+	/*
+	 * Check the validity of the mapping found via the extent status
+	 * tree or the disk query. A racing truncate may have changed the
+	 * extent, since writeback may not hold i_rwsem or the folio locks
+	 * covering the full extent(e.g., the iomap writeback path may
+	 * allocate an extent that extends beyond the length of the locked
+	 * folio in advance).
+	 */
+	if (map->m_seq != READ_ONCE(EXT4_I(inode)->i_es_seq)) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		map->m_flags = 0;
+		map->m_len = orig_mlen;
+		goto create_retry;
+	}
 	retval = ext4_map_create_blocks(handle, inode, map, flags);
 	up_write((&EXT4_I(inode)->i_data_sem));
 
 	if (retval < 0)
 		ext_debug(inode, "failed with err %d\n", retval);
 	if (retval <= 0)
-		return retval;
+		goto out;
 
 	if (map->m_flags & EXT4_MAP_MAPPED) {
 		ret = check_block_validity(inode, map);
 		if (ret != 0)
-			return ret;
+			goto out;
 
 		/*
 		 * Inodes with freshly allocated blocks where contents will be
@@ -853,12 +972,16 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 				ret = ext4_jbd2_inode_add_write(handle, inode,
 						start_byte, length);
 			if (ret)
-				return ret;
+				goto out;
 		}
 	}
 	ext4_fc_track_range(handle, inode, map->m_lblk, map->m_lblk +
 			    map->m_len - 1);
-	return retval;
+out:
+	if (internal_handle)
+		ext4_journal_stop(handle);
+
+	return ret ? ret : retval;
 }
 
 /*
@@ -916,6 +1039,9 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock,
 
 	if (ext4_has_inline_data(inode))
 		return -ERANGE;
+	/* inode using the iomap buffered I/O path should not go here. */
+	if (WARN_ON_ONCE(ext4_inode_buffered_iomap(inode)))
+		return -EINVAL;
 
 	map.m_lblk = iblock;
 	map.m_len = bh->b_size >> inode->i_blkbits;
@@ -1921,7 +2047,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 	ext4_check_map_extents_env(inode);
 
 	/* Lookup extent status tree firstly */
-	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
+	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		map->m_len = min_t(unsigned int, map->m_len,
 				   es.es_len - (map->m_lblk - es.es_lblk));
 
@@ -1974,7 +2100,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 	 * is held in write mode, before inserting a new da entry in
 	 * the extent status tree.
 	 */
-	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
+	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		map->m_len = min_t(unsigned int, map->m_len,
 				   es.es_len - (map->m_lblk - es.es_lblk));
 
@@ -1990,7 +2116,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 		}
 	}
 
-	map->m_flags |= EXT4_MAP_DELAYED;
+	map->m_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;
 	retval = ext4_insert_delayed_blocks(inode, map->m_lblk, map->m_len);
 	if (!retval)
 		map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
@@ -2379,6 +2505,12 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
 	err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
 	if (err < 0)
 		return err;
+	/*
+	 * A hole? This should never happen since mpage_add_bh_to_extent()
+	 * has filtered it out.
+	 */
+	if (WARN_ON_ONCE(!err))
+		return -EINVAL;
 	if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
 		if (!mpd->io_submit.io_end->handle &&
 		    ext4_handle_valid(handle)) {
@@ -2786,6 +2918,13 @@ static int ext4_do_writepages(struct mpage_da_data *mpd)
 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
 		goto out_writepages;
 
+	/*
+	 * Does ext4_change_inode_journal_flag() change the inode's
+	 * buffered I/O path?
+	 */
+	if (ext4_inode_buffered_iomap(inode))
+		goto out_writepages;
+
 	/*
 	 * If the filesystem has aborted, it is read-only, so return
 	 * right away instead of dumping stack traces later on that
@@ -3087,7 +3226,7 @@ static int ext4_dax_writepages(struct address_space *mapping,
 	return ret;
 }
 
-static int ext4_nonda_switch(struct super_block *sb)
+int ext4_nonda_switch(struct super_block *sb)
 {
 	s64 free_clusters, dirty_clusters;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -3459,6 +3598,15 @@ static bool ext4_inode_datasync_dirty(struct inode *inode)
 	return inode_state_read_once(inode) & I_DIRTY_DATASYNC;
 }
 
+static bool ext4_iomap_valid(struct inode *inode, const struct iomap *iomap)
+{
+	return iomap->validity_cookie == READ_ONCE(EXT4_I(inode)->i_es_seq);
+}
+
+const struct iomap_write_ops ext4_iomap_write_ops = {
+	.iomap_valid = ext4_iomap_valid,
+};
+
 static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
 			   struct ext4_map_blocks *map, loff_t offset,
 			   loff_t length, unsigned int flags)
@@ -3493,6 +3641,8 @@ static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
 	    !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
 		iomap->flags |= IOMAP_F_MERGED;
 
+	iomap->validity_cookie = map->m_seq;
+
 	/*
 	 * Flags passed to ext4_map_blocks() for direct I/O writes can result
 	 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
@@ -3907,151 +4057,636 @@ const struct iomap_ops ext4_iomap_report_ops = {
 	.iomap_next = ext4_iomap_next_report,
 };
 
-/*
- * For data=journal mode, folio should be marked dirty only when it was
- * writeably mapped. When that happens, it was already attached to the
- * transaction and marked as jbddirty (we take care of this in
- * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings
- * so we should have nothing to do here, except for the case when someone
- * had the page pinned and dirtied the page through this pin (e.g. by doing
- * direct IO to it). In that case we'd need to attach buffers here to the
- * transaction but we cannot due to lock ordering.  We cannot just dirty the
- * folio and leave attached buffers clean, because the buffers' dirty state is
- * "definitive".  We cannot just set the buffers dirty or jbddirty because all
- * the journalling code will explode.  So what we do is to mark the folio
- * "pending dirty" and next time ext4_writepages() is called, attach buffers
- * to the transaction appropriately.
- */
-static bool ext4_journalled_dirty_folio(struct address_space *mapping,
-		struct folio *folio)
+static int ext4_iomap_map_blocks(struct inode *inode, loff_t offset,
+				 loff_t length, struct ext4_map_blocks *map,
+				 int flags)
 {
-	WARN_ON_ONCE(!folio_buffers(folio));
-	if (folio_maybe_dma_pinned(folio))
-		folio_set_checked(folio);
-	return filemap_dirty_folio(mapping, folio);
+	u8 blkbits = inode->i_blkbits;
+
+	/* inode using the buffer_head buffered I/O path should not go here. */
+	if (WARN_ON_ONCE(!ext4_inode_buffered_iomap(inode)))
+		return -EINVAL;
+	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
+		return -EINVAL;
+
+	/* Calculate the first and last logical blocks respectively. */
+	map->m_lblk = offset >> blkbits;
+	map->m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
+			   EXT4_MAX_LOGICAL_BLOCK) - map->m_lblk + 1;
+
+	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
+		return ext4_da_map_blocks(inode, map);
+
+	return ext4_map_blocks(NULL, inode, map, flags);
 }
 
-static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)
+static int ext4_iomap_buffered_read_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
 {
-	WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio));
-	WARN_ON_ONCE(!folio_buffers(folio));
-	return block_dirty_folio(mapping, folio);
+	struct ext4_map_blocks map;
+	int ret;
+
+	if (unlikely(ext4_forced_shutdown(inode->i_sb)))
+		return -EIO;
+
+	/* Inline data support is not yet available. */
+	if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
+		return -ERANGE;
+
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, 0);
+	if (ret < 0)
+		return ret;
+
+	trace_ext4_iomap_buffered_read_begin(inode, &map, offset, length,
+					     flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	return 0;
 }
 
-static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
-				    struct file *file, sector_t *span)
+static int ext4_iomap_buffered_do_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap, bool delalloc)
 {
-	return iomap_swapfile_activate(sis, file, span,
-				       &ext4_iomap_report_ops);
-}
+	int ret, retries = 0;
+	struct ext4_map_blocks map;
+	int map_flags;
 
-static const struct address_space_operations ext4_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_write_begin,
-	.write_end		= ext4_write_end,
-	.dirty_folio		= ext4_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	ret = ext4_emergency_state(inode->i_sb);
+	if (unlikely(ret))
+		return ret;
 
-static const struct address_space_operations ext4_journalled_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_write_begin,
-	.write_end		= ext4_journalled_write_end,
-	.dirty_folio		= ext4_journalled_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_journalled_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio_norefs,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	/* Inline data and non-extent are not supported. */
+	if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
+		return -ERANGE;
+	if (WARN_ON_ONCE(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
+		return -EINVAL;
+	if (WARN_ON_ONCE(!(flags & (IOMAP_WRITE | IOMAP_FAULT))))
+		return -EINVAL;
 
-static const struct address_space_operations ext4_da_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_da_write_begin,
-	.write_end		= ext4_da_write_end,
-	.dirty_folio		= ext4_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	map_flags = delalloc ? EXT4_GET_BLOCKS_DELALLOC_RESERVE :
+			       EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
+retry:
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, map_flags);
+	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
+		goto retry;
+	if (ret < 0)
+		return ret;
 
-static const struct address_space_operations ext4_dax_aops = {
-	.writepages		= ext4_dax_writepages,
-	.dirty_folio		= noop_dirty_folio,
-	.bmap			= ext4_bmap,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	trace_ext4_iomap_buffered_write_begin(inode, &map, offset, length,
+					      flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	return 0;
+}
 
-void ext4_set_aops(struct inode *inode)
+static int ext4_iomap_buffered_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
 {
-	switch (ext4_inode_journal_mode(inode)) {
-	case EXT4_INODE_ORDERED_DATA_MODE:
-	case EXT4_INODE_WRITEBACK_DATA_MODE:
-		break;
-	case EXT4_INODE_JOURNAL_DATA_MODE:
-		inode->i_mapping->a_ops = &ext4_journalled_aops;
-		return;
-	default:
-		BUG();
-	}
-	if (IS_DAX(inode))
-		inode->i_mapping->a_ops = &ext4_dax_aops;
-	else if (test_opt(inode->i_sb, DELALLOC))
-		inode->i_mapping->a_ops = &ext4_da_aops;
-	else
-		inode->i_mapping->a_ops = &ext4_aops;
+	return ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,
+						  iomap, srcmap, false);
+}
+
+static int ext4_iomap_buffered_da_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
+{
+	return ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,
+						  iomap, srcmap, true);
 }
 
 /*
- * Here we can't skip an unwritten buffer even though it usually reads zero
- * because it might have data in pagecache (eg, if called from ext4_zero_range,
- * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a
- * racing writeback can come later and flush the stale pagecache to disk.
- *
- * Return the loaded bh if it actually needs zeroing - in written, dirty
- * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or
- * a clean unwritten block).
+ * On write failure, drop the stale delayed allocation range and release
+ * its reserved space for both start and end blocks. Otherwise, we may
+ * leave a range of delayed extents covered by a clean folio, which can
+ * result in inaccurate space reservation accounting.
  */
-static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)
+static void ext4_iomap_punch_delalloc(struct inode *inode, loff_t offset,
+				     loff_t length, struct iomap *iomap)
 {
-	unsigned int offset, blocksize, pos;
-	ext4_lblk_t iblock;
-	struct address_space *mapping = inode->i_mapping;
-	struct buffer_head *bh;
-	struct folio *folio;
-	int err = 0;
+	down_write(&EXT4_I(inode)->i_data_sem);
+	ext4_es_remove_extent(inode, offset >> inode->i_blkbits,
+			DIV_ROUND_UP_ULL(length, EXT4_BLOCK_SIZE(inode->i_sb)));
+	up_write(&EXT4_I(inode)->i_data_sem);
+}
 
-	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT,
-				    FGP_WRITEBEGIN | FGP_ACCESSED,
-				    mapping_gfp_constraint(mapping, ~__GFP_FS));
-	if (IS_ERR(folio))
-		return ERR_CAST(folio);
+static int ext4_iomap_buffered_da_write_end(struct inode *inode, loff_t offset,
+					    loff_t length, ssize_t written,
+					    unsigned int flags,
+					    struct iomap *iomap)
+{
+	loff_t start_byte, end_byte;
 
-	blocksize = inode->i_sb->s_blocksize;
+	/* If we didn't reserve the blocks, we're not allowed to punch them. */
+	if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW))
+		return 0;
 
-	iblock = EXT4_PG_TO_LBLK(inode, folio->index);
+	/*
+	 * iomap_page_mkwrite() will never fail in a way that requires delalloc
+	 * extents that it allocated to be revoked.  Hence never try to release
+	 * them here.
+	 */
+	if (flags & IOMAP_FAULT)
+		return 0;
 
-	bh = folio_buffers(folio);
-	if (!bh)
-		bh = create_empty_buffers(folio, blocksize, 0);
+	/* Nothing to do if we've written the entire delalloc extent */
+	start_byte = iomap_last_written_block(inode, offset, written);
+	end_byte = round_up(offset + length, i_blocksize(inode));
+	if (start_byte >= end_byte)
+		return 0;
+
+	filemap_invalidate_lock(inode->i_mapping);
+	iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
+				     iomap, ext4_iomap_punch_delalloc);
+	filemap_invalidate_unlock(inode->i_mapping);
+	return 0;
+}
+
+static int ext4_iomap_zero_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
+{
+	struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
+	struct ext4_map_blocks map;
+	u8 blkbits = inode->i_blkbits;
+	unsigned int iomap_flags;
+	int ret;
+
+	ret = ext4_emergency_state(inode->i_sb);
+	if (unlikely(ret))
+		return ret;
+
+	if (WARN_ON_ONCE(!(flags & IOMAP_ZERO)))
+		return -EINVAL;
+
+again:
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, 0);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Look up dirty folios for unwritten mappings within EOF. Providing
+	 * this bypasses the flush iomap uses to trigger extent conversion
+	 * when unwritten mappings have dirty pagecache in need of zeroing.
+	 */
+	iomap_flags = 0;
+	if (map.m_flags & EXT4_MAP_UNWRITTEN) {
+		loff_t start = ((loff_t)map.m_lblk) << blkbits;
+		loff_t end = ((loff_t)map.m_lblk + map.m_len) << blkbits;
+		unsigned int count;
+
+		count = iomap_fill_dirty_folios(iter, &start, end,
+						&iomap_flags);
+		if ((start >> blkbits) < map.m_lblk + map.m_len)
+			map.m_len = (start >> blkbits) - map.m_lblk;
+
+		/*
+		 * This can be raced by a concurrent writeback that cleans
+		 * the folio and converts the unwritten extent to written.
+		 * Recheck the mapping after a folio lock round in
+		 * iomap_fill_dirty_folios().
+		 */
+		if (count == 0 &&
+		    map.m_seq != READ_ONCE(EXT4_I(inode)->i_es_seq))
+			goto again;
+	}
+
+	trace_ext4_iomap_zero_begin(inode, &map, offset, length, flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	iomap->flags |= iomap_flags;
+
+	return 0;
+}
+
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_zero_next, ext4_iomap_zero_begin);
+
+static const struct iomap_ops ext4_iomap_zero_ops = {
+	.iomap_next = ext4_iomap_zero_next,
+};
+
+/*
+ * Since we always allocate unwritten extents, there is no need for
+ * iomap_end to clean up allocated blocks on a short write.
+ */
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next,
+			      ext4_iomap_buffered_write_begin);
+
+const struct iomap_ops ext4_iomap_buffered_write_ops = {
+	.iomap_next = ext4_iomap_buffered_write_next,
+};
+
+static DEFINE_IOMAP_ITER_NEXT_END(ext4_iomap_buffered_da_write_next,
+				  ext4_iomap_buffered_da_write_begin,
+				  ext4_iomap_buffered_da_write_end);
+
+const struct iomap_ops ext4_iomap_buffered_da_write_ops = {
+	.iomap_next = ext4_iomap_buffered_da_write_next,
+};
+
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_read_next,
+			      ext4_iomap_buffered_read_begin);
+
+static const struct iomap_ops ext4_iomap_buffered_read_ops = {
+	.iomap_next = ext4_iomap_buffered_read_next,
+};
+
+static int ext4_iomap_read_folio(struct file *file, struct folio *folio)
+{
+	iomap_bio_read_folio(folio, &ext4_iomap_buffered_read_ops);
+	return 0;
+}
+
+static void ext4_iomap_readahead(struct readahead_control *rac)
+{
+	iomap_bio_readahead(rac, &ext4_iomap_buffered_read_ops);
+}
+
+
+static int ext4_iomap_map_writeback_range(struct iomap_writepage_ctx *wpc,
+					  loff_t offset, unsigned int dirty_len)
+{
+	struct inode *inode = wpc->inode;
+	struct super_block *sb = inode->i_sb;
+	struct journal_s *journal = EXT4_SB(sb)->s_journal;
+	struct ext4_map_blocks map;
+	ext4_lblk_t index = offset >> inode->i_blkbits;
+	unsigned int blk_len, blk_end;
+	int retries = 0;
+	int ret;
+
+	ret = ext4_emergency_state(sb);
+	if (unlikely(ret))
+		return ret;
+
+	/* Check validity of the cached writeback mapping. */
+	if (offset >= wpc->iomap.offset &&
+	    offset < wpc->iomap.offset + wpc->iomap.length &&
+	    ext4_iomap_valid(inode, &wpc->iomap))
+		return 0;
+
+	blk_len = dirty_len >> inode->i_blkbits;
+	blk_end = umin(wpc->wbc->range_end >> inode->i_blkbits, UINT_MAX - 1);
+	if (blk_end > index + blk_len)
+		blk_len = blk_end - index + 1;
+
+retry:
+	map.m_lblk = index;
+	map.m_len = min_t(unsigned int, MAX_WRITEPAGES_EXTENT_LEN, blk_len);
+	ret = ext4_map_blocks(NULL, inode, &map,
+			      EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT |
+			      EXT4_GET_BLOCKS_METADATA_NOFAIL |
+			      EXT4_GET_BLOCKS_IO_SUBMIT |
+			      EXT4_EX_NOCACHE);
+	if (ret < 0) {
+		if (ext4_emergency_state(sb))
+			return ret;
+
+		/*
+		 * Retry transient ENOSPC errors, if
+		 * ext4_count_free_blocks() is non-zero, a commit
+		 * should free up blocks.
+		 */
+		if (ret == -ENOSPC && journal && ext4_count_free_clusters(sb) &&
+		    ++retries <= 3) {
+			jbd2_journal_force_commit_nested(journal);
+			goto retry;
+		}
+
+		ext4_msg(sb, KERN_CRIT,
+			 "Delayed block allocation failed for inode %llu at logical offset %llu with max blocks %u with error %d",
+			 inode->i_ino, (unsigned long long)map.m_lblk,
+			 (unsigned int)map.m_len, -ret);
+		ext4_msg(sb, KERN_CRIT,
+			 "This should not happen!! Data will be lost\n");
+		if (ret == -ENOSPC)
+			ext4_print_free_blocks(inode);
+		return ret;
+	}
+
+	trace_ext4_iomap_map_writeback_range(inode, &map, offset, dirty_len, 0);
+	ext4_set_iomap(inode, &wpc->iomap, &map, offset, dirty_len, 0);
+	return 0;
+}
+
+static void ext4_iomap_discard_folio(struct folio *folio, loff_t pos)
+{
+	struct inode *inode = folio->mapping->host;
+	loff_t length = folio_pos(folio) + folio_size(folio) - pos;
+	loff_t pstart, plen;
+
+	ext4_iomap_punch_delalloc(inode, pos, length, NULL);
+
+	/*
+	 * Clear the disksize-grow-pending state if the zeroed EOF block
+	 * fails to write back and is discarded.
+	 */
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (plen && pos <= pstart && folio_next_pos(folio) >= pstart + plen)
+		ext4_iomap_clear_disksize_pending(inode);
+}
+
+static ssize_t ext4_iomap_writeback_range(struct iomap_writepage_ctx *wpc,
+					  struct folio *folio, u64 offset,
+					  unsigned int len, u64 end_pos)
+{
+	ssize_t ret;
+
+	ret = ext4_iomap_map_writeback_range(wpc, offset, len);
+	if (!ret)
+		ret = iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
+	if (ret < 0)
+		ext4_iomap_discard_folio(folio, offset);
+	return ret;
+}
+
+static int ext4_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,
+				       int error)
+{
+	struct iomap_ioend *ioend = wpc->wb_ctx;
+	struct inode *inode = ioend->io_inode;
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	unsigned int blocksize = i_blocksize(inode);
+	loff_t pstart, plen;
+
+	/*
+	 * After I/O completion, a worker needs to be scheduled when:
+	 * 1) Unwritten extents require conversion.
+	 * 2) The file size needs to be extended.
+	 * 3) The journal needs to be aborted due to an I/O error.
+	 */
+	if ((ioend->io_flags & IOMAP_IOEND_UNWRITTEN) ||
+	    (ioend->io_offset + ioend->io_size > READ_ONCE(ei->i_disksize)) ||
+	    test_opt(ioend->io_inode->i_sb, DATA_ERR_ABORT))
+		ioend->io_bio.bi_end_io = ext4_iomap_end_bio;
+
+	/*
+	 * Mark the I/O as DISKSIZE_GROW_IO by setting io_private to
+	 * EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO if it covers the pending range.
+	 * Such I/O will allow or trigger i_disksize advancement in the
+	 * ioend worker.
+	 */
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (plen &&
+	    round_down(ioend->io_offset, blocksize) <= pstart &&
+	    round_up(ioend->io_offset + ioend->io_size, blocksize) >=
+			pstart + plen) {
+		trace_ext4_iomap_wb_disksize_pending_submit(inode,
+				ioend->io_offset, ioend->io_size);
+		ioend->io_bio.bi_end_io = ext4_iomap_end_bio;
+		ioend->io_private = (void *)EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO;
+	}
+
+	/*
+	 * ext4_iomap_end_bio() always defers endio processing, disable
+	 * generic BIO in task to avoid double deferral since we will use
+	 * a private defer endio handler in process context.
+	 *
+	 * TODO: Switch all defer handlers to the generic bio complete
+	 * in task framework.
+	 */
+	if (ioend->io_bio.bi_end_io)
+		bio_clear_flag(&ioend->io_bio, BIO_COMPLETE_IN_TASK);
+
+	return iomap_ioend_writeback_submit(wpc, error);
+}
+
+static const struct iomap_writeback_ops ext4_writeback_ops = {
+	.writeback_range = ext4_iomap_writeback_range,
+	.writeback_submit = ext4_iomap_writeback_submit,
+};
+
+/*
+ * If the current writeback range begins after the pending zeroed EOF
+ * block range which straddles i_disksize, issue a separate writeback to
+ * flush it first, so as to avoid prolonged waiting.
+ */
+static void ext4_iomap_wb_submit_zeroed_eof(struct inode *inode,
+					    struct writeback_control *wbc)
+{
+	struct address_space *mapping = inode->i_mapping;
+	loff_t pstart, plen, range_start;
+
+	if (wbc->range_cyclic)
+		range_start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
+	else
+		range_start = wbc->range_start;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen)
+		return;
+	if (round_down(range_start, mapping_min_folio_nrbytes(mapping)) <
+	    pstart + plen)
+		return;
+
+	/* Keep the caller's sync mode to avoid stalling the background flusher. */
+	if (wbc->sync_mode == WB_SYNC_ALL)
+		filemap_fdatawrite_range(mapping, pstart, pstart + plen - 1);
+	else
+		filemap_flush_range(mapping, pstart, pstart + plen - 1);
+}
+
+static int ext4_iomap_writepages(struct address_space *mapping,
+				 struct writeback_control *wbc)
+{
+	struct inode *inode = mapping->host;
+	struct super_block *sb = inode->i_sb;
+	long nr = wbc->nr_to_write;
+	int alloc_ctx, ret;
+	struct iomap_writepage_ctx wpc = {
+		.inode = inode,
+		.wbc = wbc,
+		.ops = &ext4_writeback_ops,
+	};
+
+	ret = ext4_emergency_state(sb);
+	if (unlikely(ret))
+		return ret;
+
+	/*
+	 * Submit the pending zeroed EOF block range if the entire
+	 * writeback range lies beyond it.
+	 */
+	ext4_iomap_wb_submit_zeroed_eof(inode, wbc);
+
+	alloc_ctx = ext4_writepages_down_read(sb);
+	/*
+	 * Does ext4_change_inode_journal_flag() change the inode's
+	 * buffered I/O path?
+	 */
+	if (!ext4_inode_buffered_iomap(inode)) {
+		ext4_writepages_up_read(sb, alloc_ctx);
+		return ret;
+	}
+
+	trace_ext4_writepages(inode, wbc);
+	ret = iomap_writepages(&wpc);
+	trace_ext4_writepages_result(inode, wbc, ret, nr - wbc->nr_to_write);
+	ext4_writepages_up_read(sb, alloc_ctx);
+
+	return ret;
+}
+
+/*
+ * For data=journal mode, folio should be marked dirty only when it was
+ * writeably mapped. When that happens, it was already attached to the
+ * transaction and marked as jbddirty (we take care of this in
+ * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings
+ * so we should have nothing to do here, except for the case when someone
+ * had the page pinned and dirtied the page through this pin (e.g. by doing
+ * direct IO to it). In that case we'd need to attach buffers here to the
+ * transaction but we cannot due to lock ordering.  We cannot just dirty the
+ * folio and leave attached buffers clean, because the buffers' dirty state is
+ * "definitive".  We cannot just set the buffers dirty or jbddirty because all
+ * the journalling code will explode.  So what we do is to mark the folio
+ * "pending dirty" and next time ext4_writepages() is called, attach buffers
+ * to the transaction appropriately.
+ */
+static bool ext4_journalled_dirty_folio(struct address_space *mapping,
+		struct folio *folio)
+{
+	WARN_ON_ONCE(!folio_buffers(folio));
+	if (folio_maybe_dma_pinned(folio))
+		folio_set_checked(folio);
+	return filemap_dirty_folio(mapping, folio);
+}
+
+static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)
+{
+	WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio));
+	WARN_ON_ONCE(!folio_buffers(folio));
+	return block_dirty_folio(mapping, folio);
+}
+
+static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
+				    struct file *file, sector_t *span)
+{
+	return iomap_swapfile_activate(sis, file, span,
+				       &ext4_iomap_report_ops);
+}
+
+static const struct address_space_operations ext4_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_write_begin,
+	.write_end		= ext4_write_end,
+	.dirty_folio		= ext4_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_journalled_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_write_begin,
+	.write_end		= ext4_journalled_write_end,
+	.dirty_folio		= ext4_journalled_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_journalled_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio_norefs,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_da_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_da_write_begin,
+	.write_end		= ext4_da_write_end,
+	.dirty_folio		= ext4_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_iomap_aops = {
+	.read_folio		= ext4_iomap_read_folio,
+	.readahead		= ext4_iomap_readahead,
+	.writepages		= ext4_iomap_writepages,
+	.dirty_folio		= iomap_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= iomap_invalidate_folio,
+	.release_folio		= iomap_release_folio,
+	.migrate_folio		= filemap_migrate_folio,
+	.is_partially_uptodate  = iomap_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_dax_aops = {
+	.writepages		= ext4_dax_writepages,
+	.dirty_folio		= noop_dirty_folio,
+	.bmap			= ext4_bmap,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+void ext4_set_aops(struct inode *inode)
+{
+	switch (ext4_inode_journal_mode(inode)) {
+	case EXT4_INODE_ORDERED_DATA_MODE:
+	case EXT4_INODE_WRITEBACK_DATA_MODE:
+		break;
+	case EXT4_INODE_JOURNAL_DATA_MODE:
+		inode->i_mapping->a_ops = &ext4_journalled_aops;
+		return;
+	default:
+		BUG();
+	}
+	if (IS_DAX(inode))
+		inode->i_mapping->a_ops = &ext4_dax_aops;
+	else if (ext4_inode_buffered_iomap(inode))
+		inode->i_mapping->a_ops = &ext4_iomap_aops;
+	else if (test_opt(inode->i_sb, DELALLOC))
+		inode->i_mapping->a_ops = &ext4_da_aops;
+	else
+		inode->i_mapping->a_ops = &ext4_aops;
+}
+
+/*
+ * Here we can't skip an unwritten buffer even though it usually reads zero
+ * because it might have data in pagecache (eg, if called from ext4_zero_range,
+ * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a
+ * racing writeback can come later and flush the stale pagecache to disk.
+ *
+ * Return the loaded bh if it actually needs zeroing - in written, dirty
+ * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or
+ * a clean unwritten block).
+ */
+static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)
+{
+	unsigned int offset, blocksize, pos;
+	ext4_lblk_t iblock;
+	struct address_space *mapping = inode->i_mapping;
+	struct buffer_head *bh;
+	struct folio *folio;
+	int err = 0;
+
+	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT,
+				    FGP_WRITEBEGIN | FGP_ACCESSED,
+				    mapping_gfp_constraint(mapping, ~__GFP_FS));
+	if (IS_ERR(folio))
+		return ERR_CAST(folio);
+
+	blocksize = inode->i_sb->s_blocksize;
+
+	iblock = EXT4_PG_TO_LBLK(inode, folio->index);
+
+	bh = folio_buffers(folio);
+	if (!bh)
+		bh = create_empty_buffers(folio, blocksize, 0);
 
 	/* Find the buffer that contains "offset" */
 	offset = offset_in_folio(folio, from);
@@ -4167,6 +4802,48 @@ static int ext4_block_journalled_zero_range(struct inode *inode, loff_t from,
 	return err;
 }
 
+static int ext4_block_iomap_zero_range(struct inode *inode, loff_t from,
+				       loff_t length, bool *did_zero,
+				       bool *zero_written)
+{
+	int ret;
+
+	/*
+	 * Zeroing out under an active handle can cause deadlock since
+	 * the order of acquiring the folio lock and starting a handle is
+	 * inconsistent with the iomap writeback procedure.
+	 */
+	if (WARN_ON_ONCE(ext4_handle_valid(journal_current_handle())))
+		return -EINVAL;
+
+	/* The zeroing scope should not extend across a block. */
+	if (WARN_ON_ONCE((from >> inode->i_blkbits) !=
+			 ((from + length - 1) >> inode->i_blkbits)))
+		return -EINVAL;
+
+	if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS) &&
+	    !(inode_state_read_once(inode) & (I_NEW | I_FREEING)))
+		WARN_ON_ONCE(!inode_is_locked(inode) &&
+			!rwsem_is_locked(&inode->i_mapping->invalidate_lock));
+
+	ret = iomap_zero_range(inode, from, length, did_zero,
+			       &ext4_iomap_zero_ops, &ext4_iomap_write_ops,
+			       NULL);
+	if (ret)
+		return ret;
+
+	/*
+	 * TODO: The iomap does not distinguish between different types
+	 * of zeroing operations. So we always set zero_written whenever
+	 * zeroing is performed, which may cause unnecessary folio
+	 * flushing when zeroing occurs on delayed-allocated blocks.
+	 */
+	if (did_zero && zero_written)
+		*zero_written = *did_zero;
+
+	return 0;
+}
+
 /*
  * Zeros out a mapping of length 'length' starting from file offset
  * 'from'.  The range to be zero'd must be contained with in one block.
@@ -4193,11 +4870,132 @@ static int ext4_block_zero_range(struct inode *inode,
 	} else if (ext4_should_journal_data(inode)) {
 		return ext4_block_journalled_zero_range(inode, from, length,
 							did_zero);
+	} else if (ext4_inode_buffered_iomap(inode)) {
+		return ext4_block_iomap_zero_range(inode, from, length,
+						   did_zero, zero_written);
 	}
 	return ext4_block_do_zero_range(inode, from, length, did_zero,
 					zero_written);
 }
 
+/*
+ * Inodes using the iomap buffered I/O path do not use data=ordered mode.
+ * Therefore, we mark the inode as disksize-grow-pending after zeroing the
+ * EOF block. The zeroed block will be submitted before any subsequent
+ * data.
+ *
+ * In the I/O completion path, ext4_iomap_wb_disksize_pending_wait() will
+ * wait for I/O completion before advancing i_disksize if the write
+ * extends beyond the zeroed boundary.
+ *
+ * When zeroed I/O is in progress, operations that extend i_disksize are
+ * handled as follows:
+ *
+ *  - Truncate up, append fallocate and zero_range:
+ *    Defer the update. The file size will be updated to i_size by the
+ *    end_io handler once the ongoing pending I/O completes.
+ *
+ *  - Insert range and collapse range operations:
+ *    Wait synchronously for the relevant I/O to complete before updating
+ *    i_disksize.
+ */
+static int ext4_iomap_mark_disksize_pending(struct inode *inode, loff_t from)
+{
+	struct folio *folio;
+
+	folio = filemap_lock_folio(inode->i_mapping, from >> PAGE_SHIFT);
+	if (IS_ERR(folio))
+		/* Already in writeback and cleared? */
+		return PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);
+
+	/*
+	 * Ensure that in-flight writeback, possibly started after
+	 * iomap_zero_range() unlocked the folio, has completed. Without
+	 * this wait folio_test_dirty() below may miss the zeroed data
+	 * (writeback clears PG_dirty), causing us to skip the
+	 * disksize-grow-pending tracking and potentially expose stale
+	 * on-disk data.
+	 */
+	folio_wait_writeback(folio);
+	WARN_ON_ONCE(folio_test_writeback(folio));
+
+	/*
+	 * If the zeroed range does not overlap the existing on-disk tail
+	 * block, the zeroed data lies beyond the currently on-disk data
+	 * and will be written back before i_disksize is advanced past it,
+	 * so no stale data can be exposed.
+	 *
+	 * Checking i_disksize here (after folio_wait_writeback()) is
+	 * necessary and safe. If a delalloc writeback of this folio was
+	 * in-flight, it could be raced by a concurrent mmap write which
+	 * corrupts the tail block beyond i_size but the i_disksize is not
+	 * advanced. folio_wait_writeback() has waited for its completion
+	 * and the ioend has advanced i_disksize accordingly. If writeback
+	 * had not started, we don't need to mark any pending state because
+	 * any future writeback will carry the pagecache content that now
+	 * includes the zeroed data, so no stale data can appear on disk
+	 * even without the pending tracking.
+	 */
+	if (from >= round_up(READ_ONCE(EXT4_I(inode)->i_disksize),
+			     i_blocksize(inode)))
+		goto out;
+
+	/*
+	 * Mark the inode as disksize-grow-pending. The zeroed block will
+	 * be written out by the generic writepages cycle or any other
+	 * syncing operation.
+	 *
+	 * Multiple overlapping unaligned EOF writes should not happen,
+	 * because we only mark the pending state after zeroing the on-disk
+	 * EOF block, and i_disksize can only be updated after the previous
+	 * zeroed pending block has been written back or the dirty folio
+	 * has been discarded.
+	 */
+	if (likely(folio_test_dirty(folio) &&
+		   !ext4_test_inode_state(inode,
+					  EXT4_STATE_DISKSIZE_GROW_PENDING))) {
+		trace_ext4_iomap_mark_disksize_pending(inode);
+		ext4_set_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING);
+	}
+out:
+	folio_unlock(folio);
+	folio_put(folio);
+	return 0;
+}
+
+/*
+ * Submit and wait for the pending zeroed EOF block range to complete
+ * if the given range [@offset, @end) fully covers it.  Must be called
+ * outside the context of an active journal handle and hold the i_rwsem.
+ */
+int ext4_iomap_sync_zeroed_eof(struct inode *inode, loff_t offset, loff_t end)
+{
+	loff_t pstart, plen;
+	int ret;
+
+	if (!ext4_inode_buffered_iomap(inode))
+		return 0;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen || offset > pstart || end < pstart + plen)
+		return 0;
+
+	ret = filemap_write_and_wait_range(inode->i_mapping, pstart,
+					   pstart + plen - 1);
+	if (ret)
+		return ret;
+
+	/*
+	 * The pending bit should have been cleared by the ioend worker,
+	 * which runs before the folio writeback flag is cleared.  The
+	 * caller holds i_rwsem, so no concurrent ext4_block_zero_eof()
+	 * can re-set it.
+	 */
+	WARN_ON_ONCE(ext4_test_inode_state(inode,
+				EXT4_STATE_DISKSIZE_GROW_PENDING));
+	return 0;
+}
+
 /*
  * Zero out a mapping from file offset 'from' up to the end of the block
  * which corresponds to 'from' or to the given 'end' inside this block.
@@ -4242,20 +5040,24 @@ int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)
 	 * exposing stale on-disk data which may caused by concurrent post-EOF
 	 * mmap write during folio writeback.
 	 */
-	if (ext4_should_order_data(inode) &&
-	    did_zero && zero_written && !IS_DAX(inode)) {
-		handle_t *handle;
+	if (did_zero && zero_written && !IS_DAX(inode)) {
+		if (ext4_should_order_data(inode)) {
+			handle_t *handle;
 
-		handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
-		if (IS_ERR(handle))
-			return PTR_ERR(handle);
+			handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
+			if (IS_ERR(handle))
+				return PTR_ERR(handle);
 
-		err = ext4_jbd2_inode_add_write(handle, inode, from, length);
-		ext4_journal_stop(handle);
+			err = ext4_jbd2_inode_add_write(handle, inode, from,
+							length);
+			ext4_journal_stop(handle);
+		} else if (ext4_inode_buffered_iomap(inode))
+			err = ext4_iomap_mark_disksize_pending(inode, from);
 		if (err)
 			return err;
 	}
 
+	trace_ext4_block_zero_eof(inode, from, length, did_zero, zero_written);
 	return 0;
 }
 
@@ -4352,6 +5154,14 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
 	if (offset > size)
 		return 0;
 
+	/*
+	 * We are going to punch the pending zeroed EOF block, persist
+	 * it to ensure i_disksize can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, offset, offset + len);
+	if (ret)
+		return ret;
+
 	if (offset + len < size)
 		size = offset + len;
 	if (EXT4_I(inode)->i_disksize >= size)
@@ -4503,7 +5313,14 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
 	ret = ext4_zero_partial_blocks(inode, offset, length, &partial_zeroed);
 	if (ret)
 		return ret;
-	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && partial_zeroed) {
+	/*
+	 * On the iomap path, partial invalidate of a folio without an ifs
+	 * leaves sub-block dirty bits uncleared.  Drain writeback before
+	 * removing extents so that any bio in flight on a partial folio
+	 * completes against blocks still owned by this inode.
+	 */
+	if (ext4_inode_buffered_iomap(inode) ||
+	    (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && partial_zeroed)) {
 		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
 						   end - 1);
 		if (ret)
@@ -5281,6 +6098,87 @@ static int check_igot_inode(struct inode *inode, ext4_iget_flags flags,
 	return -EFSCORRUPTED;
 }
 
+/*
+ * Determine whether an inode should use the iomap buffered I/O path.
+ * EXT4_STATE_BUFFERED_IOMAP is generally set at inode initialization
+ * time. Online switching of the buffered I/O path on an active inode is
+ * NOT supported, with the exception of changing a per-inode journal
+ * flag.
+ *
+ * For features like inline data, fsverity, and encryption that can be
+ * dynamically enabled or disabled, we check the superblock-level
+ * feature flags. If any of these is globally enabled, no inode is
+ * allowed into the iomap buffered I/O path. This avoids the complexity
+ * of dynamic toggling.
+ *
+ * For the global data journal mode (EXT4_MOUNT_JOURNAL_DATA), dynamic
+ * change through remount is deferred. It will only become available
+ * after the inode is re-initialized (i.e., after the last reference
+ * drops and the inode is re-read from disk with the journal flag
+ * cleared).
+ *
+ * For the per-inode data journal mode (EXT4_INODE_JOURNAL_DATA),
+ * dynamic changes take effect immediately. This is safe because
+ * address_space operations can be switched and all page cache can be
+ * dropped under i_rwsem and filemap_invalidate_lock.
+ *
+ * For extent-to-indirect block migration (via EXT4_IOC_SETFLAGS
+ * clearing EXT4_EXTENTS_FL), this operation is directly rejected for
+ * inodes using the iomap path.
+ *
+ * When remounting to toggle the buffered_iomap mount option, the change
+ * of I/O path is deferred as well, it will be available after the inode
+ * is re-initialized.
+ */
+void ext4_enable_buffered_iomap(struct inode *inode)
+{
+	struct super_block *sb = inode->i_sb;
+
+	if (!test_opt2(sb, BUFFERED_IOMAP))
+		return;
+	if (!S_ISREG(inode->i_mode))
+		return;
+	if (ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
+		return;
+
+	/* Unsupported Features */
+	if (ext4_has_feature_inline_data(sb))
+		return;
+	if (ext4_has_feature_verity(sb))
+		return;
+	if (ext4_has_feature_encrypt(sb))
+		return;
+	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
+	    ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA))
+		return;
+	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
+		return;
+
+	ext4_set_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+
+	/*
+	 * Install the iomap end_io handler on the shared conversion
+	 * work.  This is safe at inode initialization and during the
+	 * buffered I/O path changes where we flush all pending
+	 * writebacks and drop page cache under i_rwsem and
+	 * filemap_invalidate_lock.
+	 */
+	INIT_WORK(&EXT4_I(inode)->i_rsv_conversion_work, ext4_iomap_end_io);
+}
+
+static void ext4_disable_buffered_iomap(struct inode *inode)
+{
+	ext4_clear_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+
+	/*
+	 * Reinstall the buffer_head end_io handler on the shared
+	 * conversion work.  This is safe during the buffered I/O path
+	 * changes where we flush all pending writebacks and drop page
+	 * cache under i_rwsem and filemap_invalidate_lock.
+	 */
+	INIT_WORK(&EXT4_I(inode)->i_rsv_conversion_work, ext4_end_io_rsv_work);
+}
+
 void ext4_set_inode_mapping_order(struct inode *inode)
 {
 	struct super_block *sb = inode->i_sb;
@@ -5595,6 +6493,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 	if (ret)
 		goto bad_inode;
 
+	ext4_enable_buffered_iomap(inode);
+
 	if (S_ISREG(inode->i_mode)) {
 		inode->i_op = &ext4_file_inode_operations;
 		inode->i_fop = &ext4_file_operations;
@@ -5982,6 +6882,125 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 	}
 }
 
+/*
+ * Set i_size and i_disksize to 'newsize'.
+ *
+ * Both i_rwsem and i_data_sem are required here to avoid races between
+ * generic append writeback (or zeroed pending I/O writeback) and
+ * concurrent operations (e.g., fallocate, truncate) that also modify
+ * i_size and i_disksize. This also ensures that the writeback ioend worker
+ * observes the latest i_size under the same lock protection.
+ */
+static inline void ext4_set_inode_size(struct inode *inode, loff_t newsize)
+{
+	WARN_ON_ONCE(S_ISREG(inode->i_mode) && !inode_is_locked(inode));
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+	i_size_write(inode, newsize);
+	__ext4_set_i_disksize(inode, newsize);
+	up_write(&EXT4_I(inode)->i_data_sem);
+}
+
+static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)
+{
+	ext4_lblk_t old_lblk, new_lblk;
+	handle_t *handle;
+	int ret;
+
+	if (!IS_ALIGNED(oldsize | newsize, i_blocksize(inode))) {
+		ret = ext4_inode_attach_jinode(inode);
+		if (ret)
+			return ret;
+	}
+
+	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
+	if (!IS_ALIGNED(oldsize, i_blocksize(inode))) {
+		ret = ext4_block_zero_eof(inode, oldsize, LLONG_MAX);
+		if (ret)
+			return ret;
+	}
+
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
+	if (IS_ERR(handle))
+		return PTR_ERR(handle);
+
+	old_lblk = oldsize > 0 ? (oldsize - 1) >> inode->i_blkbits : 0;
+	new_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0;
+	ext4_fc_track_range(handle, inode, old_lblk, new_lblk);
+
+	ext4_set_inode_size(inode, newsize);
+
+	ret = ext4_mark_inode_dirty(handle, inode);
+	ext4_journal_stop(handle);
+	if (ret)
+		return ret;
+	/*
+	 * isize extend must be called outside an active handle due to
+	 * the lock ordering of transaction start and folio lock in the
+	 * iomap buffered I/O path (folio lock -> transaction start).
+	 */
+	pagecache_isize_extended(inode, oldsize, newsize);
+	return 0;
+}
+
+static int ext4_truncate_down(struct inode *inode, loff_t oldsize,
+			      loff_t newsize, int *orphan)
+{
+	ext4_lblk_t start_lblk;
+	handle_t *handle;
+	int ret;
+
+	/* Do not change i_size. */
+	if (newsize == oldsize)
+		goto truncate;
+
+	/* Shrink. */
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
+	if (IS_ERR(handle))
+		return PTR_ERR(handle);
+
+	if (ext4_handle_valid(handle)) {
+		ret = ext4_orphan_add(handle, inode);
+		*orphan = 1;
+		if (ret) {
+			ext4_journal_stop(handle);
+			return ret;
+		}
+	}
+
+	start_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0;
+	ext4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1);
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+	/*
+	 * Truncate the zeroed EOF block invalidates the pending disksize
+	 * update, so clear the disksize-grow-pending state.
+	 */
+	if (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) &&
+	    (newsize <= EXT4_I(inode)->i_disksize))
+		ext4_iomap_clear_disksize_pending(inode);
+
+	i_size_write(inode, newsize);
+	__ext4_set_i_disksize(inode, newsize);
+	up_write(&EXT4_I(inode)->i_data_sem);
+
+	ret = ext4_mark_inode_dirty(handle, inode);
+	ext4_journal_stop(handle);
+	if (ret)
+		return ret;
+
+	if (ext4_should_journal_data(inode))
+		ext4_wait_for_tail_page_commit(inode);
+truncate:
+	/*
+	 * Truncate pagecache after we've waited for commit in data=journal
+	 * mode to make pages freeable.  Call ext4_truncate() even if
+	 * i_size didn't change to truncate possible preallocated blocks.
+	 */
+	truncate_pagecache(inode, newsize);
+	return ext4_truncate(inode);
+}
+
 /*
  * ext4_setattr()
  *
@@ -6078,9 +7097,7 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	}
 
 	if (attr->ia_valid & ATTR_SIZE) {
-		handle_t *handle;
 		loff_t oldsize = inode->i_size;
-		loff_t old_disksize;
 		int shrink = (attr->ia_size < inode->i_size);
 
 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
@@ -6131,97 +7148,14 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 			goto err_out;
 		}
 
-		if (attr->ia_size != inode->i_size) {
-			/* attach jbd2 jinode for EOF folio tail zeroing */
-			if (attr->ia_size & (inode->i_sb->s_blocksize - 1) ||
-			    oldsize & (inode->i_sb->s_blocksize - 1)) {
-				error = ext4_inode_attach_jinode(inode);
-				if (error)
-					goto out_mmap_sem;
-			}
-
-			/*
-			 * Update c/mtime and tail zero the EOF folio on
-			 * truncate up. ext4_truncate() handles the shrink case
-			 * below.
-			 */
-			if (!shrink) {
-				inode_set_mtime_to_ts(inode,
-						      inode_set_ctime_current(inode));
-				if (oldsize & (inode->i_sb->s_blocksize - 1)) {
-					error = ext4_block_zero_eof(inode,
-							oldsize, LLONG_MAX);
-					if (error)
-						goto out_mmap_sem;
-				}
-			}
-
-			handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
-			if (IS_ERR(handle)) {
-				error = PTR_ERR(handle);
-				goto out_mmap_sem;
-			}
-			if (ext4_handle_valid(handle) && shrink) {
-				error = ext4_orphan_add(handle, inode);
-				orphan = 1;
-			}
-
-			if (shrink)
-				ext4_fc_track_range(handle, inode,
-					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
-					inode->i_sb->s_blocksize_bits,
-					EXT_MAX_BLOCKS - 1);
-			else
-				ext4_fc_track_range(
-					handle, inode,
-					(oldsize > 0 ? oldsize - 1 : oldsize) >>
-					inode->i_sb->s_blocksize_bits,
-					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
-					inode->i_sb->s_blocksize_bits);
-
-			down_write(&EXT4_I(inode)->i_data_sem);
-			old_disksize = EXT4_I(inode)->i_disksize;
-			EXT4_I(inode)->i_disksize = attr->ia_size;
-
-			/*
-			 * We have to update i_size under i_data_sem together
-			 * with i_disksize to avoid races with writeback code
-			 * running ext4_wb_update_i_disksize().
-			 */
-			if (!error)
-				i_size_write(inode, attr->ia_size);
-			else
-				EXT4_I(inode)->i_disksize = old_disksize;
-			up_write(&EXT4_I(inode)->i_data_sem);
-			rc = ext4_mark_inode_dirty(handle, inode);
-			if (!error)
-				error = rc;
-			ext4_journal_stop(handle);
-			if (error)
-				goto out_mmap_sem;
-			if (!shrink) {
-				pagecache_isize_extended(inode, oldsize,
-							 inode->i_size);
-			} else if (ext4_should_journal_data(inode)) {
-				ext4_wait_for_tail_page_commit(inode);
-			}
+		if (attr->ia_size > oldsize)
+			error = ext4_truncate_up(inode, oldsize, attr->ia_size);
+		else {
+			/* Shrink or do not change i_size. */
+			error = ext4_truncate_down(inode, oldsize,
+						   attr->ia_size, &orphan);
 		}
 
-		/*
-		 * Truncate pagecache after we've waited for commit
-		 * in data=journal mode to make pages freeable.
-		 */
-		truncate_pagecache(inode, inode->i_size);
-		/*
-		 * Call ext4_truncate() even if i_size didn't change to
-		 * truncate possible preallocated blocks.
-		 */
-		if (attr->ia_size <= oldsize) {
-			rc = ext4_truncate(inode);
-			if (rc)
-				error = rc;
-		}
-out_mmap_sem:
 		filemap_invalidate_unlock(inode->i_mapping);
 	}
 
@@ -6791,9 +7725,10 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
 	 * the inode's in-core data-journaling state flag now.
 	 */
 
-	if (val)
+	if (val) {
 		ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
-	else {
+		ext4_disable_buffered_iomap(inode);
+	} else {
 		err = jbd2_journal_flush(journal, 0);
 		if (err < 0) {
 			jbd2_journal_unlock_updates(journal);
@@ -6802,6 +7737,7 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
 			return err;
 		}
 		ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
+		ext4_enable_buffered_iomap(inode);
 	}
 	ext4_set_aops(inode);
 	ext4_set_inode_mapping_order(inode);
@@ -6881,6 +7817,23 @@ static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio,
 	return ret;
 }
 
+static vm_fault_t ext4_iomap_page_mkwrite(struct vm_fault *vmf)
+{
+	struct inode *inode = file_inode(vmf->vma->vm_file);
+	const struct iomap_ops *iomap_ops;
+
+	/*
+	 * ext4_nonda_switch() could writeback this folio, so have to
+	 * call it before lock folio.
+	 */
+	if (test_opt(inode->i_sb, DELALLOC) && !ext4_nonda_switch(inode->i_sb))
+		iomap_ops = &ext4_iomap_buffered_da_write_ops;
+	else
+		iomap_ops = &ext4_iomap_buffered_write_ops;
+
+	return iomap_page_mkwrite(vmf, iomap_ops, NULL);
+}
+
 vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
@@ -6907,6 +7860,11 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
 	if (err)
 		goto out_ret;
 
+	if (ext4_inode_buffered_iomap(inode)) {
+		ret = ext4_iomap_page_mkwrite(vmf);
+		goto out;
+	}
+
 	/*
 	 * On data journalling we skip straight to the transaction handle:
 	 * there's no delalloc; page truncated will be checked later; the
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index 5d60ef10fe110..09931d3ba2c6a 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -621,6 +621,8 @@ int ext4_ind_migrate(struct inode *inode)
 
 	if (ext4_has_feature_bigalloc(inode->i_sb))
 		return -EOPNOTSUPP;
+	if (ext4_inode_buffered_iomap(inode))
+		return -EOPNOTSUPP;
 
 	/*
 	 * In order to get correct extent info, force all delayed allocation
diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index 3329b7ad5dbd8..948ef3f44df5e 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -476,6 +476,17 @@ static int mext_check_validity(struct inode *orig_inode,
 		return -EOPNOTSUPP;
 	}
 
+	/*
+	 * TODO: support online defrag for inodes that use the buffered
+	 * I/O iomap path.
+	 */
+	if (ext4_inode_buffered_iomap(orig_inode) ||
+	    ext4_inode_buffered_iomap(donor_inode)) {
+		ext4_msg(sb, KERN_ERR,
+			 "Online defrag not supported for inode with iomap buffered IO path");
+		return -EOPNOTSUPP;
+	}
+
 	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
 		ext4_debug("ext4 move extent: suid or sgid is set to donor file [ino:orig %llu, donor %llu]\n",
 			   orig_inode->i_ino, donor_inode->i_ino);
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index 0236b6b9785a2..734d856a6d478 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -22,6 +22,7 @@
 #include <linux/bio.h>
 #include <linux/workqueue.h>
 #include <linux/kernel.h>
+#include <linux/iomap.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/sched/mm.h>
@@ -30,6 +31,8 @@
 #include "xattr.h"
 #include "acl.h"
 
+#include <trace/events/ext4.h>
+
 static struct kmem_cache *io_end_cachep;
 static struct kmem_cache *io_end_vec_cachep;
 
@@ -547,3 +550,201 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *folio,
 		io_submit_add_bh(io, inode, folio, bh);
 	} while ((bh = bh->b_this_page) != head);
 }
+
+/*
+ * If the current writeback range starts beyond the zeroed EOF pending
+ * range that straddles i_disksize, wait for the zeroed data from
+ * ext4_block_zero_eof() to be written out first. Otherwise, extending
+ * i_disksize may expose stale data in the old EOF block.
+ */
+static void ext4_iomap_wb_disksize_pending_wait(struct inode *inode,
+						loff_t pos, size_t size)
+{
+	loff_t disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	loff_t pstart, plen;
+
+	/*
+	 * Overwrite I/Os and I/Os covering the EOF block do not need to
+	 * wait: the former do not advance i_disksize past the pending
+	 * boundary, and the latter are the pending I/O itself (cleared in
+	 * the bio completion path).
+	 */
+	if (pos < round_up(disksize, i_blocksize(inode)))
+		return;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen || pos < pstart + plen)
+		return;
+
+	trace_ext4_iomap_wb_disksize_pending_wait(inode, pos, size);
+	ext4_iomap_wait_disksize_pending(inode);
+}
+
+static int ext4_iomap_wb_update_disksize(handle_t *handle, struct inode *inode,
+					 loff_t end, bool is_disksize_grow)
+{
+	loff_t new_disksize, i_size;
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	int ret;
+
+	/*
+	 * Races with truncate are avoided by checking i_size under
+	 * i_data_sem.
+	 */
+	down_write(&ei->i_data_sem);
+	i_size = i_size_read(inode);
+
+	/*
+	 * EXT4_STATE_DISKSIZE_GROW_PENDING is cleared when the pending
+	 * I/O completes. However, another thread may have re-set the bit
+	 * between that point and here, meaning i_disksize has already
+	 * been advanced and a new EOF zeroing has been initiated. In that
+	 * case, do not advance i_disksize to i_size; leave it to the
+	 * next pending grow ioend.
+	 */
+	if (is_disksize_grow &&
+	    ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		is_disksize_grow = false;
+
+	/*
+	 * Update i_disksize to i_size when EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO
+	 * completes. This is safe because we never directly allocate written
+	 * blocks during buffered writes.
+	 *
+	 * This ensures that i_disksize is correctly advanced during
+	 * truncate-up or append fallocate on a block-unaligned file,
+	 * preventing it from remaining stale. The tradeoff is that zeroed
+	 * data may be exposed after crash recovery if dirty data in this
+	 * range is not yet on disk, but stale data will never be exposed.
+	 * This is because the extent is only converted to written state
+	 * after the data has been persisted.
+	 */
+	new_disksize = is_disksize_grow ? i_size : min(end, i_size);
+	if (new_disksize > ei->i_disksize) {
+		trace_ext4_iomap_wb_update_disksize(inode, end, i_size,
+				ei->i_disksize, new_disksize, is_disksize_grow);
+		WRITE_ONCE(ei->i_disksize, new_disksize);
+	}
+	up_write(&ei->i_data_sem);
+	ret = ext4_mark_inode_dirty(handle, inode);
+	if (ret)
+		EXT4_ERROR_INODE_ERR(inode, -ret, "Failed to mark inode dirty");
+
+	return ret;
+}
+
+static void ext4_iomap_finish_ioend(struct iomap_ioend *ioend)
+{
+	struct inode *inode = ioend->io_inode;
+	struct super_block *sb = inode->i_sb;
+	loff_t pos = ioend->io_offset;
+	size_t size = ioend->io_size;
+	loff_t end = pos + size;
+	unsigned long io_mode = (unsigned long)ioend->io_private;
+	bool is_disksize_grow = (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO);
+	handle_t *handle;
+	int credits;
+	int ret, err;
+
+	ret = blk_status_to_errno(ioend->io_bio.bi_status);
+	if (is_disksize_grow)
+		trace_ext4_iomap_wb_disksize_pending_complete(ioend->io_inode,
+				ioend->io_offset, ioend->io_size, ret);
+	if (unlikely(ret)) {
+		if (test_opt(sb, DATA_ERR_ABORT) && !ext4_emergency_state(sb))
+			jbd2_journal_abort(EXT4_SB(sb)->s_journal, ret);
+		goto out;
+	}
+
+	if (!(ioend->io_flags & IOMAP_IOEND_UNWRITTEN) &&
+	    end <= READ_ONCE(EXT4_I(inode)->i_disksize) && !is_disksize_grow)
+		goto out;
+
+	/* Wait for disksize-pending zeroed data to be written out. */
+	ext4_iomap_wb_disksize_pending_wait(inode, pos, size);
+
+	/*
+	 * We may need to convert one extent, update the i_disksize and
+	 * dirty the inode.
+	 */
+	credits = ext4_chunk_trans_blocks(inode,
+			EXT4_MAX_BLOCKS(size, pos, inode->i_blkbits));
+	handle = ext4_journal_start(inode, EXT4_HT_EXT_CONVERT, credits);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_err;
+	}
+
+	/* Update on-disk size after I/O is completed. */
+	if (end > READ_ONCE(EXT4_I(inode)->i_disksize) || is_disksize_grow) {
+		ret = ext4_iomap_wb_update_disksize(handle, inode, end,
+						    is_disksize_grow);
+		if (ret)
+			goto out_journal;
+	}
+
+	if (ioend->io_flags & IOMAP_IOEND_UNWRITTEN)
+		ret = ext4_convert_unwritten_extents(handle, inode, pos,
+						     size, NULL);
+
+out_journal:
+	err = ext4_journal_stop(handle);
+	if (!ret)
+		ret = err;
+out_err:
+	if (ret < 0 && !ext4_emergency_state(sb)) {
+		ext4_msg(sb, KERN_EMERG,
+			 "failed to convert unwritten extents to written extents or update inode size -- potential data loss! (inode %llu, error %d)",
+			 inode->i_ino, ret);
+	}
+out:
+	iomap_finish_ioends(ioend, ret);
+}
+
+/*
+ * Work on buffered iomap completed IO, to convert unwritten extents to
+ * mapped extents
+ */
+void ext4_iomap_end_io(struct work_struct *work)
+{
+	struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
+						  i_rsv_conversion_work);
+	struct iomap_ioend *ioend;
+	struct list_head ioend_list;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ei->i_completed_io_lock, flags);
+	list_replace_init(&ei->i_rsv_conversion_list, &ioend_list);
+	spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
+
+	iomap_sort_ioends(&ioend_list);
+	while (!list_empty(&ioend_list)) {
+		ioend = list_entry(ioend_list.next, struct iomap_ioend, io_list);
+		list_del_init(&ioend->io_list);
+		iomap_ioend_try_merge(ioend, &ioend_list);
+		ext4_iomap_finish_ioend(ioend);
+	}
+}
+
+void ext4_iomap_end_bio(struct bio *bio)
+{
+	struct iomap_ioend *ioend = iomap_ioend_from_bio(bio);
+	struct ext4_inode_info *ei = EXT4_I(ioend->io_inode);
+	unsigned long io_mode = (unsigned long)ioend->io_private;
+	unsigned long flags;
+
+	/*
+	 * This is a disksize-pending I/O: clear the disksize-pending
+	 * state set in ext4_block_zero_eof() and wake up all waiters
+	 * that will update the inode i_disksize.
+	 */
+	if (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO)
+		ext4_iomap_clear_disksize_pending(ioend->io_inode);
+
+	spin_lock_irqsave(&ei->i_completed_io_lock, flags);
+	if (list_empty(&ei->i_rsv_conversion_list))
+		queue_work(EXT4_SB(ioend->io_inode->i_sb)->rsv_conversion_wq,
+			   &ei->i_rsv_conversion_work);
+	list_add_tail(&ioend->io_list, &ei->i_rsv_conversion_list);
+	spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
+}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index bca0dc87d0b7c..7db34ba0ea593 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -100,13 +100,21 @@ static const struct fs_parameter_spec ext4_param_specs[];
  * Lock ordering
  *
  * page fault path:
- * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
- *   -> page lock -> i_data_sem (rw)
+ * - buffer_head path:
+ *   mmap_lock -> sb_start_pagefault -> invalidate_lock (r) ->
+ *     transaction start -> folio lock -> i_data_sem (rw)
+ * - iomap path:
+ *   mmap_lock -> sb_start_pagefault -> invalidate_lock (r) ->
+ *     folio lock -> transaction start -> i_data_sem (rw)
  *
  * buffered write path:
- * sb_start_write -> i_mutex -> mmap_lock
- * sb_start_write -> i_mutex -> transaction start -> page lock ->
- *   i_data_sem (rw)
+ * sb_start_write -> i_rwsem (w) -> mmap_lock
+ * - buffer_head path:
+ *   sb_start_write -> i_rwsem (w) -> transaction start -> folio lock ->
+ *     i_data_sem (rw)
+ * - iomap path:
+ *   sb_start_write -> i_rwsem (w) -> transaction start -> i_data_sem (rw)
+ *   sb_start_write -> i_rwsem (w) -> folio lock (not under an active handle)
  *
  * truncate:
  * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
@@ -119,7 +127,10 @@ static const struct fs_parameter_spec ext4_param_specs[];
  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
  *
  * writepages:
- * transaction start -> page lock(s) -> i_data_sem (rw)
+ * - buffer_head path:
+ *   transaction start -> folio lock(s) -> i_data_sem (rw)
+ * - iomap path:
+ *   folio lock -> transaction start -> i_data_sem (rw)
  */
 
 static const struct fs_context_operations ext4_context_ops = {
@@ -1486,6 +1497,14 @@ static void ext4_destroy_inode(struct inode *inode)
 			 "Inode %llu (%p): i_reserved_data_blocks (%u) not cleared!",
 			 inode->i_ino, EXT4_I(inode),
 			 EXT4_I(inode)->i_reserved_data_blocks);
+
+	if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ERROR_FS) &&
+	    !ext4_emergency_state(inode->i_sb) &&
+	    WARN_ON_ONCE(ext4_test_inode_state(inode,
+				EXT4_STATE_DISKSIZE_GROW_PENDING)))
+		ext4_msg(inode->i_sb, KERN_ERR,
+			 "Inode %llu (%p): EXT4_STATE_DISKSIZE_GROW_PENDING not cleared!",
+			 inode->i_ino, EXT4_I(inode));
 }
 
 static void ext4_shutdown(struct super_block *sb)
@@ -1728,6 +1747,7 @@ enum {
 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
 	Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
+	Opt_buffered_iomap, Opt_nobuffered_iomap,
 	Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
 #ifdef CONFIG_EXT4_DEBUG
 	Opt_fc_debug_max_replay, Opt_fc_debug_force
@@ -1866,6 +1886,8 @@ static const struct fs_parameter_spec ext4_param_specs[] = {
 	fsparam_flag	("no_prefetch_block_bitmaps",
 						Opt_no_prefetch_block_bitmaps),
 	fsparam_s32	("mb_optimize_scan",	Opt_mb_optimize_scan),
+	fsparam_flag	("buffered_iomap",	Opt_buffered_iomap),
+	fsparam_flag	("nobuffered_iomap",	Opt_nobuffered_iomap),
 	fsparam_string	("check",		Opt_removed),	/* mount option from ext2/3 */
 	fsparam_flag	("nocheck",		Opt_removed),	/* mount option from ext2/3 */
 	fsparam_flag	("reservation",		Opt_removed),	/* mount option from ext2/3 */
@@ -1959,6 +1981,10 @@ static const struct mount_opts {
 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
 	{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
 	 MOPT_SET},
+	{Opt_buffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,
+	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
+	{Opt_nobuffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,
+	 MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},
 #ifdef CONFIG_EXT4_DEBUG
 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 7028a28316fa3..92048da9dae43 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -3157,6 +3157,215 @@ TRACE_EVENT(ext4_move_extent_exit,
 		  __entry->ret)
 );
 
+DECLARE_EVENT_CLASS(ext4_set_iomap_class,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 loff_t offset, loff_t length, unsigned int flags),
+	TP_ARGS(inode, map, offset, length, flags),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(ext4_lblk_t, m_lblk)
+		__field(ext4_fsblk_t, pblk)
+		__field(unsigned int, m_len)
+		__field(unsigned int, m_flags)
+		__field(u64, m_seq)
+		__field(loff_t, offset)
+		__field(loff_t, length)
+		__field(unsigned int, iomap_flags)
+	),
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->m_lblk		= map->m_lblk;
+		__entry->pblk		= map->m_pblk;
+		__entry->m_len		= map->m_len;
+		__entry->m_flags	= map->m_flags;
+		__entry->m_seq		= map->m_seq;
+		__entry->offset		= offset;
+		__entry->length		= length;
+		__entry->iomap_flags	= flags;
+
+	),
+	TP_printk("dev %d:%d ino %llu m_lblk %u pblk %llu m_len %u m_flags %s m_seq %llu orig_off 0x%llx orig_len 0x%llx iomap_flags 0x%x",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->m_lblk, __entry->pblk, __entry->m_len,
+		  show_mflags(__entry->m_flags), __entry->m_seq,
+		  __entry->offset, __entry->length, __entry->iomap_flags)
+)
+
+#define DEFINE_SET_IOMAP_EVENT(name) \
+DEFINE_EVENT(ext4_set_iomap_class, name, \
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map, \
+		 loff_t offset, loff_t length, unsigned int flags), \
+	TP_ARGS(inode, map, offset, length, flags))
+
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_read_begin);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_write_begin);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_map_writeback_range);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_zero_begin);
+
+DECLARE_EVENT_CLASS(ext4_iomap_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, i_disksize)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	),
+	TP_printk("dev %d:%d ino %llu i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->i_disksize)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_mark_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_clear_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_wait_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+/* disksize pending I/O tracepoints for iomap Buffered I/O path */
+DECLARE_EVENT_CLASS(ext4_iomap_wb_disksize_pending,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, io_offset)
+		__field(size_t, io_size)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->io_offset = io_offset;
+		__entry->io_size = io_size;
+		__entry->i_size = i_size_read(inode);
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	),
+	TP_printk("dev %d:%d ino %llu io_offset %lld io_size %zu i_size %lld i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->io_offset, __entry->io_size,
+		  __entry->i_size, __entry->i_disksize)
+);
+
+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,
+	ext4_iomap_wb_disksize_pending_submit,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size)
+);
+
+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,
+	ext4_iomap_wb_disksize_pending_wait,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size)
+);
+
+TRACE_EVENT(ext4_iomap_wb_disksize_pending_complete,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size,
+		 int ret),
+	TP_ARGS(inode, io_offset, io_size, ret),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, io_offset)
+		__field(size_t, io_size)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(int, ret)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->io_offset = io_offset;
+		__entry->io_size = io_size;
+		__entry->i_size = i_size_read(inode);
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+		__entry->ret = ret;
+	),
+	TP_printk("dev %d:%d ino %llu io_offset %lld io_size %zu ret %d i_size %lld i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->io_offset, __entry->io_size,
+		  __entry->ret, __entry->i_size, __entry->i_disksize)
+);
+
+/* i_disksize update tracepoint */
+TRACE_EVENT(ext4_iomap_wb_update_disksize,
+	TP_PROTO(struct inode *inode, loff_t end, loff_t i_size,
+		 loff_t i_disksize, loff_t new_disksize,
+		 bool is_disksize_grow),
+	TP_ARGS(inode, end, i_size, i_disksize, new_disksize,
+		is_disksize_grow),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, end)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(loff_t, new_disksize)
+		__field(bool, is_disksize_grow)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->end = end;
+		__entry->i_size = i_size;
+		__entry->i_disksize = i_disksize;
+		__entry->new_disksize = new_disksize;
+		__entry->is_disksize_grow = is_disksize_grow;
+	),
+	TP_printk("dev %d:%d ino %llu end %lld i_size %lld i_disksize %lld new_disksize %lld is_disksize_grow %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->end, __entry->i_size,
+		  __entry->i_disksize, __entry->new_disksize,
+		  __entry->is_disksize_grow)
+);
+
+/* Block zero EOF tracepoint */
+TRACE_EVENT(ext4_block_zero_eof,
+	TP_PROTO(struct inode *inode, loff_t from, loff_t length,
+		 bool did_zero, bool zero_written),
+	TP_ARGS(inode, from, length, did_zero, zero_written),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, from)
+		__field(loff_t, length)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(bool, did_zero)
+		__field(bool, zero_written)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->from = from;
+		__entry->length = length;
+		__entry->i_size = inode->i_size;
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+		__entry->did_zero = did_zero;
+		__entry->zero_written = zero_written;
+	),
+	TP_printk("dev %d:%d ino %llu zero EOF from %lld length %lld i_size %lld i_disksize %lld did_zero %d zero_written %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino,
+		  __entry->from, __entry->length, __entry->i_size,
+		  __entry->i_disksize, __entry->did_zero, __entry->zero_written)
+);
+
 #endif /* _TRACE_EXT4_H */
 
 /* This part must be outside protection */

3/2 2026/10/09 13:28 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 40978
							output: 31
							thoughts: 459
						

Thoughts:
**Evaluating the ext4 Buffered I/O Patch Series**

Okay, here we go. The user wants me to take a look at a patch series for the Linux kernel. Specifically, it's about adding buffered I/O support using iomap to the ext4 filesystem. The patch touches a lot of key areas: `EXT4_MOUNT2_BUFFERED_IOMAP`, `ext4_inode_buffered_iomap`, `ext4_iomap_buffered_write`, `ext4_iomap_aops`, `ext4_iomap_writepages`, etc. This looks like a substantial piece of work.

My initial thought is to quickly apply the core triage philosophy. The main point is reachability, and ext4, of course, is completely reachable in a standard environment, like QEMU or syzkaller. The philosophy flags logic changes, memory management modifications, sync concerns, VFS interactions, and filesystems as automatically worth fuzzing. Considering this is a *major* new feature, or, at the least, a significant refactoring of ext4, the "WorthFuzzing" flag is almost certainly true. That's a strong indicator.

Now, I need to get into the details. The obvious question is whether this is an enabled-by-default mount option, or something that needs to be explicitly enabled. Let's dig into the patch itself.

Alright, here it is: `Opt_buffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP` and `fsparam_flag("buffered_iomap", Opt_buffered_iomap)`. Okay, that suggests it's controlled by a mount option. Excellent. And then there is `ext4_enable_buffered_iomap(struct inode *inode)`:

```c
void ext4_enable_buffered_iomap(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;

	if (!test_opt2(sb, BUFFERED_IOMAP))
		return;
...
```

Right, it uses `test_opt2(sb, BUFFERED_IOMAP)`, so it's a mount-time option. We know that we can mount with `-o buffered_iomap`.  Now I need to confirm: is it default, or is it opt-in? It's essential to understand that for the next stages of analysis. I need to grep super.c. to be sure, or look at how the option is handled within the ext4 source.



4/2 2026/10/09 13:28 tool grepper
0m Args:
{
  "Expression": "BUFFERED_IOMAP",
  "PathPrefixes": [
    "fs/ext4"
  ]
}

Results:
{
  "Output": "fs/ext4/ext4.h=1048=struct ext4_inode_info {\n--\nfs/ext4/ext4.h-1323-#define EXT4_MOUNT2_ABORT\t\t0x00000100 /* Abort filesystem */\nfs/ext4/ext4.h:1324:#define EXT4_MOUNT2_BUFFERED_IOMAP\t0x00000200 /* Use iomap for buffered I/O */\nfs/ext4/ext4.h-1325-\n--\nfs/ext4/ext4.h=2040=enum {\n--\nfs/ext4/ext4.h-2054-\tEXT4_STATE_FC_REQUEUE,\t\t/* Inode modified during fast commit */\nfs/ext4/ext4.h:2055:\tEXT4_STATE_BUFFERED_IOMAP,\t/* Inode use iomap for buffered IO */\nfs/ext4/ext4.h-2056-\tEXT4_STATE_DISKSIZE_GROW_PENDING,\n--\nfs/ext4/ext4.h=2159=static inline bool ext4_inode_buffered_iomap(struct inode *inode)\nfs/ext4/ext4.h-2160-{\nfs/ext4/ext4.h:2161:\treturn ext4_test_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\nfs/ext4/ext4.h-2162-}\n--\nfs/ext4/inode.c=6062=static int check_igot_inode(struct inode *inode, ext4_iget_flags flags,\n--\nfs/ext4/inode.c-6102- * Determine whether an inode should use the iomap buffered I/O path.\nfs/ext4/inode.c:6103: * EXT4_STATE_BUFFERED_IOMAP is generally set at inode initialization\nfs/ext4/inode.c-6104- * time. Online switching of the buffered I/O path on an active inode is\n--\nfs/ext4/inode.c=6133=void ext4_enable_buffered_iomap(struct inode *inode)\n--\nfs/ext4/inode.c-6136-\nfs/ext4/inode.c:6137:\tif (!test_opt2(sb, BUFFERED_IOMAP))\nfs/ext4/inode.c-6138-\t\treturn;\n--\nfs/ext4/inode.c-6156-\nfs/ext4/inode.c:6157:\text4_set_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\nfs/ext4/inode.c-6158-\n--\nfs/ext4/inode.c=6169=static void ext4_disable_buffered_iomap(struct inode *inode)\nfs/ext4/inode.c-6170-{\nfs/ext4/inode.c:6171:\text4_clear_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);\nfs/ext4/inode.c-6172-\n--\nfs/ext4/super.c=1917=static const struct mount_opts {\n--\nfs/ext4/super.c-1983-\t MOPT_SET},\nfs/ext4/super.c:1984:\t{Opt_buffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,\nfs/ext4/super.c-1985-\t MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},\nfs/ext4/super.c:1986:\t{Opt_nobuffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,\nfs/ext4/super.c-1987-\t MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},\n"
}

5/2 2026/10/09 13:28 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43019
							output: 28
							thoughts: 31
						

Thoughts:
Here's my take on the process, thinking through the problem as a seasoned developer would:

**Digging into the ext4 Filesystem Configuration**

Okay, so I need to understand the configuration options for ext4 and iomap. Time to dive into the Kconfig files. The usual suspects would be in `fs/ext4`, so let's start there. I'm going to look for any files named `Kconfig` or anything that seems related to kernel configuration within that directory. I want to see what knobs we have for ext4's core behavior, especially anything influencing I/O mapping. This will help to understand the default settings and how to tune them for my specific use case. I'm expecting to find options that control features like journaling, block sizes, and any potential optimizations related to iomap specifically.



6/2 2026/10/09 13:28 tool grepper
0m Args:
{
  "Expression": "config EXT4",
  "PathPrefixes": [
    "fs/ext4"
  ]
}

Results:
{
  "Output": "fs/ext4/Kconfig-1-# SPDX-License-Identifier: GPL-2.0-only\nfs/ext4/Kconfig:2:config EXT4_FS\nfs/ext4/Kconfig-3-\ttristate \"The Extended 4 (ext4) filesystem\"\n--\nfs/ext4/Kconfig-34-\nfs/ext4/Kconfig:35:config EXT4_USE_FOR_EXT2\nfs/ext4/Kconfig-36-\tbool \"Use ext4 for ext2 file systems\"\n--\nfs/ext4/Kconfig-45-\nfs/ext4/Kconfig:46:config EXT4_FS_POSIX_ACL\nfs/ext4/Kconfig-47-\tbool \"Ext4 POSIX Access Control Lists\"\n--\nfs/ext4/Kconfig-55-\nfs/ext4/Kconfig:56:config EXT4_FS_SECURITY\nfs/ext4/Kconfig-57-\tbool \"Ext4 Security Labels\"\n--\nfs/ext4/Kconfig-67-\nfs/ext4/Kconfig:68:config EXT4_DEBUG\nfs/ext4/Kconfig-69-\tbool \"Ext4 debugging support\"\n--\nfs/ext4/Kconfig-76-\nfs/ext4/Kconfig:77:config EXT4_KUNIT_TESTS\nfs/ext4/Kconfig-78-\ttristate \"KUnit tests for ext4\" if !KUNIT_ALL_TESTS\n"
}

7/2 2026/10/09 13:28 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43621
							output: 33
							thoughts: 29
						

Thoughts:
Here's a summary of my thought process, as requested:

**Diving into the EXT4 Configuration**

Okay, so I need to figure out how `FS_IOMAP` (or something comparable) is being handled within the EXT4 filesystem configuration. My immediate thought is to jump directly into `fs/ext4/Kconfig`. Lines 1 through 40 seem like a reasonable starting point to get an overview of the initial configuration options and dependencies. I'll scan that section first, looking for the relevant `select` statements or any other clues about how `FS_IOMAP` might be triggered or enabled, and trace down through the dependencies to fully understand it.



8/2 2026/10/09 13:30 tool read-file
0m Args:
{
  "File": "fs/ext4/Kconfig",
  "FirstLine": 1,
  "LineCount": 40
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0-only\n   2:\tconfig EXT4_FS\n   3:\t\ttristate \"The Extended 4 (ext4) filesystem\"\n   4:\t\tselect BUFFER_HEAD\n   5:\t\tselect JBD2\n   6:\t\tselect CRC16\n   7:\t\tselect CRC32\n   8:\t\tselect FS_IOMAP\n   9:\t\tselect FS_ENCRYPTION_ALGS if FS_ENCRYPTION\n  10:\t\thelp\n  11:\t\t  This is the next generation of the ext3 filesystem.\n  12:\t\n  13:\t\t  Unlike the change from ext2 filesystem to ext3 filesystem,\n  14:\t\t  the on-disk format of ext4 is not forwards compatible with\n  15:\t\t  ext3; it is based on extent maps and it supports 48-bit\n  16:\t\t  physical block numbers.  The ext4 filesystem also supports delayed\n  17:\t\t  allocation, persistent preallocation, high resolution time stamps,\n  18:\t\t  and a number of other features to improve performance and speed\n  19:\t\t  up fsck time.  For more information, please see the web pages at\n  20:\t\t  http://ext4.wiki.kernel.org.\n  21:\t\n  22:\t\t  The ext4 filesystem supports mounting an ext3 filesystem; while there\n  23:\t\t  are some performance gains from the delayed allocation and inode\n  24:\t\t  table readahead, the best performance gains require enabling ext4\n  25:\t\t  features in the filesystem using tune2fs, or formatting a new\n  26:\t\t  filesystem as an ext4 filesystem initially. Without explicit enabling\n  27:\t\t  of ext4 features, the on disk filesystem format stays fully backward\n  28:\t\t  compatible.\n  29:\t\n  30:\t\t  To compile this file system support as a module, choose M here. The\n  31:\t\t  module will be called ext4.\n  32:\t\n  33:\t\t  If unsure, say N.\n  34:\t\n  35:\tconfig EXT4_USE_FOR_EXT2\n  36:\t\tbool \"Use ext4 for ext2 file systems\"\n  37:\t\tdepends on EXT4_FS\n  38:\t\tdepends on EXT2_FS=n\n  39:\t\tdefault y\n  40:\t\thelp\n"
}

9/2 2026/10/09 13:30 llm patch-evaluator
1m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 44416
							output: 32
							thoughts: 47
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating EXT4 and IOMAP Dependencies**

Alright, so `EXT4_FS` triggers `FS_IOMAP`. My first question is: are there *any* other configuration dependencies, either specific config options, or more general checks within the EXT4 code itself? I need to be sure I understand the complete picture of what controls the inclusion of this new IOMAP-related functionality, and I'm looking for things that might be easily missed when dealing with a configuration this complex. I need to be sure that I have the complete picture before I move forward, no assumptions.

Let's dive into the `fs/ext4` directory and check out its `Makefile`. The makefile is typically the most direct way to understand how the build system integrates and builds the kernel code. Hopefully, that will immediately reveal other conditional compilations or dependencies. I need to ensure a complete understanding of how this new functionality is managed within the EXT4 filesystem build process.



10/2 2026/10/09 13:31 tool read-file
0m Args:
{
  "File": "fs/ext4/Makefile",
  "FirstLine": 1,
  "LineCount": 40
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0\n   2:\t#\n   3:\t# Makefile for the linux ext4-filesystem routines.\n   4:\t#\n   5:\t\n   6:\tobj-$(CONFIG_EXT4_FS) += ext4.o\n   7:\t\n   8:\text4-y\t:= balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \\\n   9:\t\t\textents_status.o file.o fsmap.o fsync.o hash.o ialloc.o \\\n  10:\t\t\tindirect.o inline.o inode.o ioctl.o mballoc.o migrate.o \\\n  11:\t\t\tmmp.o move_extent.o namei.o page-io.o readpage.o resize.o \\\n  12:\t\t\tsuper.o symlink.o sysfs.o xattr.o xattr_hurd.o xattr_trusted.o \\\n  13:\t\t\txattr_user.o fast_commit.o orphan.o\n  14:\t\n  15:\text4-$(CONFIG_EXT4_FS_POSIX_ACL)\t+= acl.o\n  16:\text4-$(CONFIG_EXT4_FS_SECURITY)\t\t+= xattr_security.o\n  17:\text4-test-objs\t\t\t\t+= inode-test.o mballoc-test.o \\\n  18:\t\t\t\t\t\t   extents-test.o hash-test.o\n  19:\tobj-$(CONFIG_EXT4_KUNIT_TESTS)\t\t+= ext4-test.o\n  20:\text4-$(CONFIG_FS_VERITY)\t\t+= verity.o\n  21:\text4-$(CONFIG_FS_ENCRYPTION)\t\t+= crypto.o\n"
}

11/2 2026/10/09 13:31 llm patch-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 45166
							output: 260
							thoughts: 2609
						

Thoughts:
**Analysis of Ext4 Buffered I/O Implementation for Fuzzing**

Okay, so I've got a new patch that introduces a buffered I/O implementation using iomap for ext4. This touches a significant number of files – `ext4.h`, `ext4_jbd2.c`, `ext4_jbd2.h`, `extents.c`, `file.c`, `ialloc.c`, `inode.c`, `migrate.c`, `move_extent.c`, `page-io.c`, `super.c`, and `include/trace/events/ext4.h` – all compiled under `obj-$(CONFIG_EXT4_FS) += ext4.o`. No special config options beyond `EXT4_FS` are needed, which is good.

First, let's assess if this is `WorthFuzzing`. Absolutely. This patch fundamentally alters ext4 by adding `buffered_iomap`, impacting inode size handling, extent conversions, unwritten extent handling, truncate logic, writeback, folio handling, and throws in some `WARN_ON_ONCE` assertions. Plus, it's user-accessible via the `buffered_iomap` mount option and standard file operations like read, write, truncate, fallocate, page fault mkwrite, punch hole, collapse range, insert range, zero range, etc. That means this is a great target for fuzzing.

Now, the critical part: `FocusSymbols`. We need specific kernel functions, not ubiquitous ones. I need to pinpoint the key functions related to *this* new feature.

Let's dissect the code changes to isolate those functions:

*   In `fs/ext4/file.c`, `ext4_iomap_buffered_write` is called by `ext4_buffered_write_iter`. While `ext4_buffered_write_iter` itself is not ubiquitous, I want to make sure I am focusing on non-ubiquitous entrypoints.
*   In `fs/ext4/inode.c`: I've got a whole list of functions. The non-static functions are: `ext4_iomap_clear_disksize_pending`, `ext4_iomap_wait_disksize_pending`, `ext4_iomap_get_disksize_pending_range`, `ext4_iomap_sync_zeroed_eof`, `ext4_enable_buffered_iomap`, plus a raft of static ones like `ext4_iomap_buffered_read_begin`, `ext4_iomap_buffered_do_write_begin`, `ext4_iomap_buffered_write_begin`, `ext4_iomap_buffered_da_write_begin`, `ext4_iomap_buffered_da_write_end`, `ext4_iomap_zero_begin`, `ext4_iomap_read_folio`, `ext4_iomap_readahead`, `ext4_iomap_map_writeback_range`, `ext4_iomap_discard_folio`, `ext4_iomap_writeback_range`, `ext4_iomap_writeback_submit`, `ext4_iomap_writepages`, `ext4_block_iomap_zero_range`, `ext4_iomap_mark_disksize_pending`, `ext4_truncate_up`, `ext4_truncate_down`, `ext4_iomap_page_mkwrite`. Some modified functions like `ext4_nonda_switch`, `ext4_map_blocks`, `ext4_da_map_blocks`, `ext4_set_aops`, `ext4_block_zero_eof`, `ext4_punch_hole`, `ext4_page_mkwrite`, `ext4_change_inode_journal_flag`.
*   In `fs/ext4/page-io.c`, `ext4_iomap_wb_disksize_pending_wait`, `ext4_iomap_wb_update_disksize`, `ext4_iomap_finish_ioend`, and the non-static ones `ext4_iomap_end_io`, `ext4_iomap_end_bio`.
*   In `fs/ext4/extents.c`, the modified functions `ext4_convert_unwritten_extents`, `ext4_zero_range`, `ext4_do_fallocate`, `ext4_collapse_range`, `ext4_insert_range`.
*   In `fs/ext4/file.c`, there's `ext


12/2 2026/10/09 13:31 tool set-results
0m Args:
{
  "EnableConfigs": [
    "EXT4_FS"
  ],
  "FocusSymbols": [
    "ext4_enable_buffered_iomap",
    "ext4_iomap_buffered_write",
    "ext4_iomap_writepages",
    "ext4_iomap_page_mkwrite",
    "ext4_iomap_end_io",
    "ext4_iomap_end_bio",
    "ext4_iomap_sync_zeroed_eof",
    "ext4_convert_unwritten_extents",
    "ext4_truncate_up",
    "ext4_truncate_down"
  ],
  "Reasoning": "The patch implements buffered I/O via the iomap framework in ext4 (enabled via the 'buffered_iomap' mount option). It adds iomap read/write/writeback/page_mkwrite operations, alters unwritten extent conversion, restructures truncate-up and truncate-down handling, adds disksize-grow-pending synchronization logic, and adds new assertions (WARN_ON_ONCE). This is core filesystem code fully reachable in virtualized environments.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "EXT4_FS"
  ],
  "FocusSymbols": [
    "ext4_enable_buffered_iomap",
    "ext4_iomap_buffered_write",
    "ext4_iomap_writepages",
    "ext4_iomap_page_mkwrite",
    "ext4_iomap_end_io",
    "ext4_iomap_end_bio",
    "ext4_iomap_sync_zeroed_eof",
    "ext4_convert_unwritten_extents",
    "ext4_truncate_up",
    "ext4_truncate_down"
  ],
  "Reasoning": "The patch implements buffered I/O via the iomap framework in ext4 (enabled via the 'buffered_iomap' mount option). It adds iomap read/write/writeback/page_mkwrite operations, alters unwritten extent conversion, restructures truncate-up and truncate-down handling, adds disksize-grow-pending synchronization logic, and adds new assertions (WARN_ON_ONCE). This is core filesystem code fully reachable in virtualized environments.",
  "WorthFuzzing": true
}

13/1 2026/10/09 13:31 action If
3m Args:
{
  "WorthFuzzing": true
}

Results:
null

14/2 2026/10/09 13:31 agent kmsan-evaluator
3m Model:
core

Args:
null

Results:
{
  "KMSANReasoning": "The patch series implements support for the iomap buffered I/O path in ext4 (enabled via the 'buffered_iomap' mount option), introducing iomap address space operations (ext4_iomap_aops), extent mapping helpers, writeback completion and disksize pending synchronization routines, and extent conversion handling.\n\nAnalysis regarding KMSAN vs. KASAN/LOCKDEP:\n1. No uninitialized memory or info-leak risk:\n   - The changes do not expose any kernel data structures, buffers, or padded structs to user space (no copy_to_user, put_user, netlink skb generation, or ioctl output structs).\n   - All stack variables (e.g., `struct ext4_map_blocks map`, `struct iomap_writepage_ctx wpc`) are properly initialized: `wpc` uses designated initialization zeroing all unspecified fields; `ext4_map_blocks` and `ext4_da_map_blocks` explicitly zero `map-\u003em_flags` and assign `map-\u003em_seq` / `map-\u003em_pblk` before use; helper functions like `ext4_iomap_get_disksize_pending_range()` initialize the out-parameter whenever a non-zero length is returned, and callers strictly check the return value before accessing the out-parameter.\n2. KASAN and LOCKDEP coverage:\n   - The primary risks of this patch are complex lock ordering changes (between folio locks, journal transaction starts, i_data_sem, and i_rwsem) which are scrutinized by LOCKDEP, and potential bio/ioend lifetime or concurrency bugs (use-after-free or out-of-bounds access) which are covered by standard KASAN.\n\nBecause the changes do not introduce or expose uninitialized memory access or user-space information leaks, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Instruction:
You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.

CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
  a comprehensive suite of debugging tools and sanitizers, including KASAN
  (out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
  (locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
  or page allocations) and kernel-to-user memory info-leaks.

Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.

Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.

Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
   attributes, ioctl output arguments, socket options, or BPF buffers) where fields
   or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
   or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
   could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
   uninitialized bytes of existing buffers.

Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
  dereferences, locking deadlocks, or use-after-free bugs (these are already caught
  by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
  or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.

Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
Target architecture: amd64

For your convenience, here is the diff of the changes:
commit 747cfe98d30bc59325d25d500da86aa585c6ddae
Author: syz-cluster <triage@syzkaller.com>
Date:   Fri Oct 9 13:28:25 2026 +0000

    syz-cluster: applied patch under review

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 724a27e8be613..8cfe77b45e21b 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1208,8 +1208,10 @@ struct ext4_inode_info {
 	/* Lock protecting lists below */
 	spinlock_t i_completed_io_lock;
 	/*
-	 * Completed IOs that need unwritten extents handling and have
-	 * transaction reserved
+	 * Completed IOs that need unwritten extents handling and have a
+	 * transaction reserved for the buffer_head writeback path, and
+	 * also used by the iomap writeback path to queue ioends needing
+	 * unwritten extents conversion, i_disksize update, etc.
 	 */
 	struct list_head i_rsv_conversion_list;
 	struct work_struct i_rsv_conversion_work;
@@ -1319,6 +1321,7 @@ struct ext4_inode_info {
 						    * scanning in mballoc
 						    */
 #define EXT4_MOUNT2_ABORT		0x00000100 /* Abort filesystem */
+#define EXT4_MOUNT2_BUFFERED_IOMAP	0x00000200 /* Use iomap for buffered I/O */
 
 #define clear_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt &= \
 						~EXT4_MOUNT_##opt
@@ -2049,6 +2052,10 @@ enum {
 	EXT4_STATE_FC_FLUSHING_DATA,	/* Fast commit flushing data */
 	EXT4_STATE_ORPHAN_FILE,		/* Inode orphaned in orphan file */
 	EXT4_STATE_FC_REQUEUE,		/* Inode modified during fast commit */
+	EXT4_STATE_BUFFERED_IOMAP,	/* Inode use iomap for buffered IO */
+	EXT4_STATE_DISKSIZE_GROW_PENDING,
+					/* Has zeroed EOF block straddles
+					 * i_disksize awaiting writeback */
 };
 
 #define EXT4_INODE_BIT_FNS(name, field, offset)				\
@@ -2148,6 +2155,12 @@ static inline struct mapping_metadata_bhs *ext4_i_metadata_bhs(
 	return READ_ONCE(EXT4_I(inode)->i_metadata_bhs);
 }
 
+/* Whether the inode pass through the iomap infrastructure for buffered I/O */
+static inline bool ext4_inode_buffered_iomap(struct inode *inode)
+{
+	return ext4_test_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+}
+
 /*
  * Codes for operating systems
  */
@@ -3158,6 +3171,8 @@ int ext4_walk_page_buffers(handle_t *handle,
 int do_journal_get_write_access(handle_t *handle, struct inode *inode,
 				struct buffer_head *bh);
 void ext4_set_inode_mapping_order(struct inode *inode);
+void ext4_enable_buffered_iomap(struct inode *inode);
+int ext4_nonda_switch(struct super_block *sb);
 #define FALL_BACK_TO_NONDELALLOC 1
 #define EXT4_WRITE_DATA_INLINE	 2
 
@@ -3209,6 +3224,12 @@ extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
 extern int ext4_chunk_trans_extent(struct inode *inode, int nrblocks);
 extern int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
 				  int pextents, int alloc_extents);
+void ext4_iomap_clear_disksize_pending(struct inode *inode);
+void ext4_iomap_wait_disksize_pending(struct inode *inode);
+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,
+						   loff_t *start);
+extern int ext4_iomap_sync_zeroed_eof(struct inode *inode,
+				      loff_t offset, loff_t end);
 extern int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end);
 
 #define EXT4_PARTIAL_ZERO_START	0x1
@@ -3588,30 +3609,67 @@ do {								\
 #define EXT4_FREECLUSTERS_WATERMARK 0
 #endif
 
-/* Update i_disksize. Requires i_rwsem to avoid races with truncate */
+/*
+ * Update i_disksize. Requires i_rwsem to avoid races with truncate.
+ *
+ * In the iomap buffered I/O path, the EXT4_STATE_DISKSIZE_GROW_PENDING
+ * inode state bit indicates that the zeroed EOF partial block which
+ * straddles i_disksize is still waiting writeback.  In that case,
+ * i_disksize will be updated after the pending zeroed data has been
+ * written out.
+ */
 static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
 {
 	WARN_ON_ONCE(S_ISREG(inode->i_mode) &&
 		     !inode_is_locked(inode));
 	down_write(&EXT4_I(inode)->i_data_sem);
-	if (newsize > EXT4_I(inode)->i_disksize)
+	if (newsize > EXT4_I(inode)->i_disksize &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
 		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
 	up_write(&EXT4_I(inode)->i_data_sem);
 }
 
-/* Update i_size, i_disksize. Requires i_rwsem to avoid races with truncate */
+static inline void __ext4_set_i_disksize(struct inode *inode, loff_t newsize)
+{
+	WARN_ON_ONCE(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
+
+	if (newsize < EXT4_I(inode)->i_disksize ||
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
+}
+
+/*
+ * Update i_size and i_disksize to @newsize.  Requires i_rwsem to avoid
+ * races with truncate.
+ *
+ * In the iomap buffered I/O path, i_disksize is updated only if no zeroed
+ * pending block straddles i_disksize (EXT4_STATE_DISKSIZE_GROW_PENDING
+ * clear), otherwise the ioend worker for the pending block will advance
+ * i_disksize once the pending block is written back.  Both updates happen
+ * under i_data_sem so that the writeback ioend worker can always see the
+ * latest i_size under the same semaphore.
+ *
+ * Returns 0 if nothing changed, 1 if i_size was raised, 2 if i_disksize
+ * was raised, or 3 if both were.
+ */
 static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
 {
 	int changed = 0;
 
+	if (newsize <= inode->i_size && newsize <= EXT4_I(inode)->i_disksize)
+		return 0;
+
+	down_write(&EXT4_I(inode)->i_data_sem);
 	if (newsize > inode->i_size) {
 		i_size_write(inode, newsize);
 		changed = 1;
 	}
-	if (newsize > EXT4_I(inode)->i_disksize) {
-		ext4_update_i_disksize(inode, newsize);
+	if (newsize > EXT4_I(inode)->i_disksize &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING)) {
+		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
 		changed |= 2;
 	}
+	up_write(&EXT4_I(inode)->i_data_sem);
 	return changed;
 }
 
@@ -3969,6 +4027,12 @@ extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
 			     __u64 len, __u64 *moved_len);
 
 /* page-io.c */
+/*
+ * The I/O range covers the zeroed EOF block that straddles i_disksize
+ * and will advance it upon completion.
+ */
+#define EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO	1UL
+
 extern int __init ext4_init_pageio(void);
 extern void ext4_exit_pageio(void);
 extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
@@ -3983,6 +4047,8 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *page,
 		size_t len);
 extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);
 extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);
+extern void ext4_iomap_end_io(struct work_struct *work);
+extern void ext4_iomap_end_bio(struct bio *bio);
 
 /* mmp.c */
 extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
@@ -4040,6 +4106,9 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
 
 extern const struct iomap_ops ext4_iomap_ops;
 extern const struct iomap_ops ext4_iomap_report_ops;
+extern const struct iomap_ops ext4_iomap_buffered_write_ops;
+extern const struct iomap_ops ext4_iomap_buffered_da_write_ops;
+extern const struct iomap_write_ops ext4_iomap_write_ops;
 
 int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		unsigned flags, struct iomap *iomap, struct iomap *srcmap);
diff --git a/fs/ext4/ext4_jbd2.c b/fs/ext4/ext4_jbd2.c
index 53ddedb52a6f7..a4664ddecdcd6 100644
--- a/fs/ext4/ext4_jbd2.c
+++ b/fs/ext4/ext4_jbd2.c
@@ -17,8 +17,12 @@ int ext4_inode_journal_mode(struct inode *inode)
 	    test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
 	    (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) &&
 	    !test_opt(inode->i_sb, DELALLOC))) {
-		/* We do not support data journalling for encrypted data */
-		if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
+		/*
+		 * We do not support data journalling for encrypted data
+		 * and buffered IOMAP path.
+		 */
+		if (S_ISREG(inode->i_mode) &&
+		    (IS_ENCRYPTED(inode) || ext4_inode_buffered_iomap(inode)))
 			return EXT4_INODE_ORDERED_DATA_MODE;  /* ordered */
 		return EXT4_INODE_JOURNAL_DATA_MODE;	/* journal data */
 	}
diff --git a/fs/ext4/ext4_jbd2.h b/fs/ext4/ext4_jbd2.h
index 2fbf48b3dfe21..be54e93bde0b1 100644
--- a/fs/ext4/ext4_jbd2.h
+++ b/fs/ext4/ext4_jbd2.h
@@ -379,7 +379,12 @@ static inline int ext4_should_journal_data(struct inode *inode)
 
 static inline int ext4_should_order_data(struct inode *inode)
 {
-	return ext4_inode_journal_mode(inode) & EXT4_INODE_ORDERED_DATA_MODE;
+	/*
+	 * inodes using the iomap buffered I/O path do not use the
+	 * data=ordered mode.
+	 */
+	return !ext4_inode_buffered_iomap(inode) &&
+		(ext4_inode_journal_mode(inode) & EXT4_INODE_ORDERED_DATA_MODE);
 }
 
 static inline int ext4_should_writeback_data(struct inode *inode)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 76038b6c36552..a10d8c6e3cf1f 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4561,7 +4561,7 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)
 	 */
 
 	/* we have to know where to truncate from in crash case */
-	EXT4_I(inode)->i_disksize = inode->i_size;
+	__ext4_set_i_disksize(inode, inode->i_size);
 	err = ext4_mark_inode_dirty(handle, inode);
 	if (err)
 		return err;
@@ -4857,6 +4857,17 @@ static long ext4_zero_range(struct file *file, loff_t offset,
 		if (ret)
 			return ret;
 	}
+
+	/*
+	 * In SYNC mode, sync the pending zeroed EOF block to ensure the
+	 * i_disksize update is persisted.
+	 */
+	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && new_size) {
+		ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+		if (ret)
+			return ret;
+	}
+
 	/* Finish zeroing out if it doesn't contain partial block */
 	if (IS_ALIGNED(offset | end, blocksize))
 		return ret;
@@ -4928,10 +4939,20 @@ static long ext4_do_fallocate(struct file *file, loff_t offset,
 	if (ret)
 		goto out;
 
-	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) &&
-	    EXT4_SB(inode->i_sb)->s_journal) {
-		ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
-					EXT4_I(inode)->i_sync_tid);
+	if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
+		/*
+		 * Sync the pending zeroed EOF block to ensure the
+		 * i_disksize update is persisted.
+		 */
+		if (new_size) {
+			ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+			if (ret)
+				goto out;
+		}
+		if (EXT4_SB(inode->i_sb)->s_journal) {
+			ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
+						EXT4_I(inode)->i_sync_tid);
+		}
 	}
 out:
 	trace_ext4_fallocate_exit(inode, offset,
@@ -5137,27 +5158,36 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 	int ret = 0, ret2 = 0, ret3 = 0;
 	struct ext4_map_blocks map;
 	unsigned int blkbits = inode->i_blkbits;
-	unsigned int credits = 0;
+	unsigned int credits;
+	bool internal_handle = !handle;
 
 	map.m_lblk = offset >> blkbits;
 	map.m_len = max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
 
-	if (!handle) {
-		/*
-		 * credits to insert 1 extent into extent tree
-		 */
-		credits = ext4_chunk_trans_blocks(inode, max_blocks);
+	/* Credits to convert one extent in this range to written state. */
+	credits = ext4_meta_trans_blocks(inode, max_blocks, 1, 0);
+
+	if (internal_handle) {
+		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);
+		if (IS_ERR(handle)) {
+			ret = PTR_ERR(handle);
+			goto out;
+		}
 	}
 
 	while (max_blocks) {
-		if (credits) {
-			handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
-						    credits);
-			if (IS_ERR(handle)) {
-				ret = PTR_ERR(handle);
-				break;
-			}
-		}
+		/*
+		 * The caller cannot know how many extents the range covers,
+		 * so make sure the transaction can take one more extent,
+		 * extending or restarting it once it is full.  The reserved
+		 * handle of the buffer_head writeback path is safe as it has
+		 * enough credits for the conversion, so it is not restarted
+		 * here.
+		 */
+		ret = ext4_journal_ensure_credits(handle, credits, 0);
+		if (ret < 0)
+			break;
+
 		/*
 		 * Do not cache any unrelated extents, as it does not hold the
 		 * i_rwsem or invalidate_lock, which could corrupt the extent
@@ -5177,11 +5207,6 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 		}
 
 		ret2 = ext4_mark_inode_dirty(handle, inode);
-		if (credits) {
-			ret3 = ext4_journal_stop(handle);
-			if (unlikely(ret3))
-				ret2 = ret3;
-		}
 		ret = ret < 0 ? ret : ret2;
 		if (ret)
 			break;
@@ -5189,6 +5214,13 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
 		map.m_lblk += map.m_len;
 		map.m_len = (max_blocks -= map.m_len);
 	}
+
+	if (internal_handle) {
+		ret3 = ext4_journal_stop(handle);
+		if (!ret)
+			ret = ret3;
+	}
+out:
 	/* Converted some or all blocks successfully? */
 	if (converted)
 		*converted = conv_blocks;
@@ -5203,8 +5235,8 @@ int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
 
 	/*
 	 * This is somewhat ugly but the idea is clear: When transaction is
-	 * reserved, everything goes into it. Otherwise we rather start several
-	 * smaller transactions for conversion of each extent separately.
+	 * reserved, everything goes into it. Otherwise the conversion runs
+	 * its own transaction, extending or restarting it as it goes.
 	 */
 	if (handle) {
 		handle = ext4_journal_start_reserved(handle,
@@ -5662,6 +5694,14 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
 	if (end >= inode->i_size)
 		return -EINVAL;
 
+	/*
+	 * Persist the pending zeroed EOF block to ensure i_disksize
+	 * can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+	if (ret)
+		return ret;
+
 	/*
 	 * Write tail of the last page before removed range and data that
 	 * will be shifted since they will get removed from the page cache
@@ -5711,7 +5751,7 @@ static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len)
 
 	new_size = inode->i_size - len;
 	i_size_write(inode, new_size);
-	EXT4_I(inode)->i_disksize = new_size;
+	__ext4_set_i_disksize(inode, new_size);
 
 	up_write(&EXT4_I(inode)->i_data_sem);
 	ret = ext4_mark_inode_dirty(handle, inode);
@@ -5764,6 +5804,14 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
 	if (len > inode->i_sb->s_maxbytes - inode->i_size)
 		return -EFBIG;
 
+	/*
+	 * Persist the pending zeroed EOF block to ensure i_disksize
+	 * can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, 0, LLONG_MAX);
+	if (ret)
+		return ret;
+
 	/*
 	 * Write out all dirty pages. Need to round down to align start offset
 	 * to page size boundary for page size > block size.
@@ -5783,8 +5831,7 @@ static int ext4_insert_range(struct file *file, loff_t offset, loff_t len)
 	ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle);
 
 	/* Expand file to avoid data loss if there is error while shifting */
-	inode->i_size += len;
-	EXT4_I(inode)->i_disksize += len;
+	ext4_update_inode_size(inode, inode->i_size + len);
 	ret = ext4_mark_inode_dirty(handle, inode);
 	if (ret)
 		goto out_handle;
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 374b4bc25bd5d..50d3c92709c8b 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -330,6 +330,21 @@ static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
 	return count;
 }
 
+static ssize_t ext4_iomap_buffered_write(struct kiocb *iocb,
+					 struct iov_iter *from)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+	const struct iomap_ops *iomap_ops;
+
+	if (test_opt(inode->i_sb, DELALLOC) && !ext4_nonda_switch(inode->i_sb))
+		iomap_ops = &ext4_iomap_buffered_da_write_ops;
+	else
+		iomap_ops = &ext4_iomap_buffered_write_ops;
+
+	return iomap_file_buffered_write(iocb, from, iomap_ops,
+					 &ext4_iomap_write_ops, NULL);
+}
+
 static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 					struct iov_iter *from)
 {
@@ -351,7 +366,10 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
 	if (ret <= 0)
 		goto out;
 
-	ret = generic_perform_write(iocb, from);
+	if (ext4_inode_buffered_iomap(inode))
+		ret = ext4_iomap_buffered_write(iocb, from);
+	else
+		ret = generic_perform_write(iocb, from);
 
 out:
 	inode_unlock(inode);
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index a5831fc536db9..f97a2f4904ebb 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -1346,6 +1346,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 		}
 	}
 
+	ext4_enable_buffered_iomap(inode);
 	ext4_set_inode_mapping_order(inode);
 
 	ext4_update_inode_fsync_trans(handle, inode, 1);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 26f0f9714f03c..1853fbdc2a5af 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -44,6 +44,7 @@
 #include <linux/iversion.h>
 
 #include "ext4_jbd2.h"
+#include "ext4_extents.h"
 #include "xattr.h"
 #include "acl.h"
 #include "truncate.h"
@@ -125,6 +126,70 @@ void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
 		raw->i_checksum_hi = cpu_to_le16(csum >> 16);
 }
 
+/*
+ * Clear the disksize-grow-pending state and wake up all waiters.
+ * Called when the pending zeroed EOF block which straddles i_disksize
+ * has completed writeback or its folio is discarded.
+ */
+void ext4_iomap_clear_disksize_pending(struct inode *inode)
+{
+	if (!test_and_clear_wake_up_bit(
+		ext4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),
+		ext4_inode_state_wait_word(inode)))
+		return;
+
+	trace_ext4_iomap_clear_disksize_pending(inode);
+}
+
+/*
+ * Wait for the disksize-grow-pending zeroed EOF block which straddles
+ * i_disksize to be written back or cleared.
+ */
+void ext4_iomap_wait_disksize_pending(struct inode *inode)
+{
+	/* Only emit the trace when the bit is actually set */
+	if (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		trace_ext4_iomap_wait_disksize_pending(inode);
+
+	wait_on_bit(ext4_inode_state_wait_word(inode),
+		    ext4_inode_state_wait_bit(EXT4_STATE_DISKSIZE_GROW_PENDING),
+		    TASK_UNINTERRUPTIBLE);
+}
+
+/*
+ * Get the range of the disksize-grow-pending zeroed EOF block range
+ * which straddles i_disksize if the EXT4_STATE_DISKSIZE_GROW_PENDING
+ * bit is set.
+ *
+ * Return the pending range, or zero if the BIT has already been cleared.
+ */
+unsigned int ext4_iomap_get_disksize_pending_range(struct inode *inode,
+						   loff_t *start)
+{
+	unsigned int blocksize = i_blocksize(inode);
+	loff_t disksize;
+
+	if (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		return 0;
+
+	/*
+	 * The pending bit should be set only when i_disksize is not
+	 * block-size aligned. While set, i_disksize must not be advanced,
+	 * and the bit must be cleared when i_disksize is shrunk.
+	 */
+	down_read(&EXT4_I(inode)->i_data_sem);
+	disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	if (!ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) ||
+	    WARN_ON_ONCE(IS_ALIGNED(disksize, blocksize))) {
+		up_read(&EXT4_I(inode)->i_data_sem);
+		return 0;
+	}
+
+	up_read(&EXT4_I(inode)->i_data_sem);
+	*start = disksize;
+	return blocksize - (disksize & (blocksize - 1));
+}
+
 static inline int ext4_begin_ordered_truncate(struct inode *inode,
 					      loff_t new_size)
 {
@@ -208,6 +273,8 @@ void ext4_evict_inode(struct inode *inode)
 
 	if (ext4_should_order_data(inode))
 		ext4_begin_ordered_truncate(inode, 0);
+	if (ext4_inode_buffered_iomap(inode))
+		ext4_iomap_clear_disksize_pending(inode);
 	truncate_inode_pages_final(&inode->i_data);
 
 	/*
@@ -686,6 +753,11 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  * based files
  *
+ * ext4_map_blocks() may also be called with a NULL @handle. In that case
+ * it starts its own transaction handle only when block allocation is
+ * actually needed, i.e. after the lookup confirms the blocks are not yet
+ * mapped, and stops it before returning.
+ *
  * On success, it returns the number of blocks being mapped or allocated.
  * If flags doesn't contain EXT4_GET_BLOCKS_CREATE the blocks are
  * pre-allocated and unwritten, the resulting @map is marked as unwritten.
@@ -703,7 +775,8 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	struct extent_status es;
 	int retval;
 	int ret = 0;
-	unsigned int orig_mlen = map->m_len;
+	bool internal_handle = false;
+	unsigned int orig_mlen;
 #ifdef ES_AGGRESSIVE_TEST
 	struct ext4_map_blocks orig_map;
 
@@ -719,6 +792,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 */
 	if (unlikely(map->m_len > INT_MAX))
 		map->m_len = INT_MAX;
+	orig_mlen = map->m_len;
 
 	/* We can handle the block number less than EXT_MAX_BLOCKS */
 	if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
@@ -734,6 +808,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	else
 		ext4_check_map_extents_env(inode);
 
+create_retry:
 	/* Lookup extent status tree firstly */
 	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
@@ -784,12 +859,14 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	down_read(&EXT4_I(inode)->i_data_sem);
 	retval = ext4_map_query_blocks(handle, inode, map, flags);
 	up_read((&EXT4_I(inode)->i_data_sem));
+	if (retval < 0)
+		goto out;
 
 found:
 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
 		ret = check_block_validity(inode, map);
 		if (ret != 0)
-			return ret;
+			goto out;
 	}
 
 	/* If it is only a block(s) look up */
@@ -802,15 +879,42 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 * Note that if blocks have been preallocated
 	 * ext4_ext_map_blocks() returns with buffer head unmapped
 	 */
-	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
+	if (retval > 0) {
 		/*
-		 * If we need to convert extent to unwritten
-		 * we continue and do the actual work in
-		 * ext4_ext_map_blocks()
+		 * If we need to convert written extent to unwritten or
+		 * convert unwritten extent to written, continue and do
+		 * the actual work in ext4_ext_map_blocks().
 		 */
-		if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
-			return retval;
+		if (map->m_flags & EXT4_MAP_MAPPED &&
+		    !(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
+			goto out;
+		if (map->m_flags & EXT4_MAP_UNWRITTEN &&
+		    (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) &&
+		    !(flags & EXT4_GET_BLOCKS_CONVERT)) {
+			/* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */
+			ret = check_block_validity(inode, map);
+			if (!ret)
+				map->m_flags |= EXT4_MAP_MAPPED;
+			goto out;
+		}
+	} else if (retval == 0) {
+		/*
+		 * Do not allocate blocks for holes in the context of
+		 * data submission path.
+		 */
+		if (!map->m_flags && (flags & EXT4_GET_BLOCKS_IO_SUBMIT))
+			goto out;
+	}
 
+	if (!handle) {
+		handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
+				ext4_chunk_trans_blocks(inode, orig_mlen));
+		if (IS_ERR(handle)) {
+			ret = PTR_ERR(handle);
+			goto out;
+		}
+		internal_handle = true;
+	}
 
 	ext4_fc_track_inode(handle, inode);
 	/*
@@ -820,18 +924,33 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	 * with create == 1 flag.
 	 */
 	down_write(&EXT4_I(inode)->i_data_sem);
+
+	/*
+	 * Check the validity of the mapping found via the extent status
+	 * tree or the disk query. A racing truncate may have changed the
+	 * extent, since writeback may not hold i_rwsem or the folio locks
+	 * covering the full extent(e.g., the iomap writeback path may
+	 * allocate an extent that extends beyond the length of the locked
+	 * folio in advance).
+	 */
+	if (map->m_seq != READ_ONCE(EXT4_I(inode)->i_es_seq)) {
+		up_write(&EXT4_I(inode)->i_data_sem);
+		map->m_flags = 0;
+		map->m_len = orig_mlen;
+		goto create_retry;
+	}
 	retval = ext4_map_create_blocks(handle, inode, map, flags);
 	up_write((&EXT4_I(inode)->i_data_sem));
 
 	if (retval < 0)
 		ext_debug(inode, "failed with err %d\n", retval);
 	if (retval <= 0)
-		return retval;
+		goto out;
 
 	if (map->m_flags & EXT4_MAP_MAPPED) {
 		ret = check_block_validity(inode, map);
 		if (ret != 0)
-			return ret;
+			goto out;
 
 		/*
 		 * Inodes with freshly allocated blocks where contents will be
@@ -853,12 +972,16 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 				ret = ext4_jbd2_inode_add_write(handle, inode,
 						start_byte, length);
 			if (ret)
-				return ret;
+				goto out;
 		}
 	}
 	ext4_fc_track_range(handle, inode, map->m_lblk, map->m_lblk +
 			    map->m_len - 1);
-	return retval;
+out:
+	if (internal_handle)
+		ext4_journal_stop(handle);
+
+	return ret ? ret : retval;
 }
 
 /*
@@ -916,6 +1039,9 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock,
 
 	if (ext4_has_inline_data(inode))
 		return -ERANGE;
+	/* inode using the iomap buffered I/O path should not go here. */
+	if (WARN_ON_ONCE(ext4_inode_buffered_iomap(inode)))
+		return -EINVAL;
 
 	map.m_lblk = iblock;
 	map.m_len = bh->b_size >> inode->i_blkbits;
@@ -1921,7 +2047,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 	ext4_check_map_extents_env(inode);
 
 	/* Lookup extent status tree firstly */
-	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
+	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		map->m_len = min_t(unsigned int, map->m_len,
 				   es.es_len - (map->m_lblk - es.es_lblk));
 
@@ -1974,7 +2100,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 	 * is held in write mode, before inserting a new da entry in
 	 * the extent status tree.
 	 */
-	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, NULL)) {
+	if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
 		map->m_len = min_t(unsigned int, map->m_len,
 				   es.es_len - (map->m_lblk - es.es_lblk));
 
@@ -1990,7 +2116,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
 		}
 	}
 
-	map->m_flags |= EXT4_MAP_DELAYED;
+	map->m_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;
 	retval = ext4_insert_delayed_blocks(inode, map->m_lblk, map->m_len);
 	if (!retval)
 		map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
@@ -2379,6 +2505,12 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
 	err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
 	if (err < 0)
 		return err;
+	/*
+	 * A hole? This should never happen since mpage_add_bh_to_extent()
+	 * has filtered it out.
+	 */
+	if (WARN_ON_ONCE(!err))
+		return -EINVAL;
 	if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
 		if (!mpd->io_submit.io_end->handle &&
 		    ext4_handle_valid(handle)) {
@@ -2786,6 +2918,13 @@ static int ext4_do_writepages(struct mpage_da_data *mpd)
 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
 		goto out_writepages;
 
+	/*
+	 * Does ext4_change_inode_journal_flag() change the inode's
+	 * buffered I/O path?
+	 */
+	if (ext4_inode_buffered_iomap(inode))
+		goto out_writepages;
+
 	/*
 	 * If the filesystem has aborted, it is read-only, so return
 	 * right away instead of dumping stack traces later on that
@@ -3087,7 +3226,7 @@ static int ext4_dax_writepages(struct address_space *mapping,
 	return ret;
 }
 
-static int ext4_nonda_switch(struct super_block *sb)
+int ext4_nonda_switch(struct super_block *sb)
 {
 	s64 free_clusters, dirty_clusters;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
@@ -3459,6 +3598,15 @@ static bool ext4_inode_datasync_dirty(struct inode *inode)
 	return inode_state_read_once(inode) & I_DIRTY_DATASYNC;
 }
 
+static bool ext4_iomap_valid(struct inode *inode, const struct iomap *iomap)
+{
+	return iomap->validity_cookie == READ_ONCE(EXT4_I(inode)->i_es_seq);
+}
+
+const struct iomap_write_ops ext4_iomap_write_ops = {
+	.iomap_valid = ext4_iomap_valid,
+};
+
 static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
 			   struct ext4_map_blocks *map, loff_t offset,
 			   loff_t length, unsigned int flags)
@@ -3493,6 +3641,8 @@ static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
 	    !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
 		iomap->flags |= IOMAP_F_MERGED;
 
+	iomap->validity_cookie = map->m_seq;
+
 	/*
 	 * Flags passed to ext4_map_blocks() for direct I/O writes can result
 	 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
@@ -3907,151 +4057,636 @@ const struct iomap_ops ext4_iomap_report_ops = {
 	.iomap_next = ext4_iomap_next_report,
 };
 
-/*
- * For data=journal mode, folio should be marked dirty only when it was
- * writeably mapped. When that happens, it was already attached to the
- * transaction and marked as jbddirty (we take care of this in
- * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings
- * so we should have nothing to do here, except for the case when someone
- * had the page pinned and dirtied the page through this pin (e.g. by doing
- * direct IO to it). In that case we'd need to attach buffers here to the
- * transaction but we cannot due to lock ordering.  We cannot just dirty the
- * folio and leave attached buffers clean, because the buffers' dirty state is
- * "definitive".  We cannot just set the buffers dirty or jbddirty because all
- * the journalling code will explode.  So what we do is to mark the folio
- * "pending dirty" and next time ext4_writepages() is called, attach buffers
- * to the transaction appropriately.
- */
-static bool ext4_journalled_dirty_folio(struct address_space *mapping,
-		struct folio *folio)
+static int ext4_iomap_map_blocks(struct inode *inode, loff_t offset,
+				 loff_t length, struct ext4_map_blocks *map,
+				 int flags)
 {
-	WARN_ON_ONCE(!folio_buffers(folio));
-	if (folio_maybe_dma_pinned(folio))
-		folio_set_checked(folio);
-	return filemap_dirty_folio(mapping, folio);
+	u8 blkbits = inode->i_blkbits;
+
+	/* inode using the buffer_head buffered I/O path should not go here. */
+	if (WARN_ON_ONCE(!ext4_inode_buffered_iomap(inode)))
+		return -EINVAL;
+	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
+		return -EINVAL;
+
+	/* Calculate the first and last logical blocks respectively. */
+	map->m_lblk = offset >> blkbits;
+	map->m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
+			   EXT4_MAX_LOGICAL_BLOCK) - map->m_lblk + 1;
+
+	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
+		return ext4_da_map_blocks(inode, map);
+
+	return ext4_map_blocks(NULL, inode, map, flags);
 }
 
-static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)
+static int ext4_iomap_buffered_read_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
 {
-	WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio));
-	WARN_ON_ONCE(!folio_buffers(folio));
-	return block_dirty_folio(mapping, folio);
+	struct ext4_map_blocks map;
+	int ret;
+
+	if (unlikely(ext4_forced_shutdown(inode->i_sb)))
+		return -EIO;
+
+	/* Inline data support is not yet available. */
+	if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
+		return -ERANGE;
+
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, 0);
+	if (ret < 0)
+		return ret;
+
+	trace_ext4_iomap_buffered_read_begin(inode, &map, offset, length,
+					     flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	return 0;
 }
 
-static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
-				    struct file *file, sector_t *span)
+static int ext4_iomap_buffered_do_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap, bool delalloc)
 {
-	return iomap_swapfile_activate(sis, file, span,
-				       &ext4_iomap_report_ops);
-}
+	int ret, retries = 0;
+	struct ext4_map_blocks map;
+	int map_flags;
 
-static const struct address_space_operations ext4_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_write_begin,
-	.write_end		= ext4_write_end,
-	.dirty_folio		= ext4_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	ret = ext4_emergency_state(inode->i_sb);
+	if (unlikely(ret))
+		return ret;
 
-static const struct address_space_operations ext4_journalled_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_write_begin,
-	.write_end		= ext4_journalled_write_end,
-	.dirty_folio		= ext4_journalled_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_journalled_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio_norefs,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	/* Inline data and non-extent are not supported. */
+	if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
+		return -ERANGE;
+	if (WARN_ON_ONCE(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
+		return -EINVAL;
+	if (WARN_ON_ONCE(!(flags & (IOMAP_WRITE | IOMAP_FAULT))))
+		return -EINVAL;
 
-static const struct address_space_operations ext4_da_aops = {
-	.read_folio		= ext4_read_folio,
-	.readahead		= ext4_readahead,
-	.writepages		= ext4_writepages,
-	.write_begin		= ext4_da_write_begin,
-	.write_end		= ext4_da_write_end,
-	.dirty_folio		= ext4_dirty_folio,
-	.bmap			= ext4_bmap,
-	.invalidate_folio	= ext4_invalidate_folio,
-	.release_folio		= ext4_release_folio,
-	.migrate_folio		= buffer_migrate_folio,
-	.is_partially_uptodate  = block_is_partially_uptodate,
-	.error_remove_folio	= generic_error_remove_folio,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	map_flags = delalloc ? EXT4_GET_BLOCKS_DELALLOC_RESERVE :
+			       EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
+retry:
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, map_flags);
+	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
+		goto retry;
+	if (ret < 0)
+		return ret;
 
-static const struct address_space_operations ext4_dax_aops = {
-	.writepages		= ext4_dax_writepages,
-	.dirty_folio		= noop_dirty_folio,
-	.bmap			= ext4_bmap,
-	.swap_activate		= ext4_iomap_swap_activate,
-};
+	trace_ext4_iomap_buffered_write_begin(inode, &map, offset, length,
+					      flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	return 0;
+}
 
-void ext4_set_aops(struct inode *inode)
+static int ext4_iomap_buffered_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
 {
-	switch (ext4_inode_journal_mode(inode)) {
-	case EXT4_INODE_ORDERED_DATA_MODE:
-	case EXT4_INODE_WRITEBACK_DATA_MODE:
-		break;
-	case EXT4_INODE_JOURNAL_DATA_MODE:
-		inode->i_mapping->a_ops = &ext4_journalled_aops;
-		return;
-	default:
-		BUG();
-	}
-	if (IS_DAX(inode))
-		inode->i_mapping->a_ops = &ext4_dax_aops;
-	else if (test_opt(inode->i_sb, DELALLOC))
-		inode->i_mapping->a_ops = &ext4_da_aops;
-	else
-		inode->i_mapping->a_ops = &ext4_aops;
+	return ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,
+						  iomap, srcmap, false);
+}
+
+static int ext4_iomap_buffered_da_write_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
+{
+	return ext4_iomap_buffered_do_write_begin(inode, offset, length, flags,
+						  iomap, srcmap, true);
 }
 
 /*
- * Here we can't skip an unwritten buffer even though it usually reads zero
- * because it might have data in pagecache (eg, if called from ext4_zero_range,
- * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a
- * racing writeback can come later and flush the stale pagecache to disk.
- *
- * Return the loaded bh if it actually needs zeroing - in written, dirty
- * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or
- * a clean unwritten block).
+ * On write failure, drop the stale delayed allocation range and release
+ * its reserved space for both start and end blocks. Otherwise, we may
+ * leave a range of delayed extents covered by a clean folio, which can
+ * result in inaccurate space reservation accounting.
  */
-static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)
+static void ext4_iomap_punch_delalloc(struct inode *inode, loff_t offset,
+				     loff_t length, struct iomap *iomap)
 {
-	unsigned int offset, blocksize, pos;
-	ext4_lblk_t iblock;
-	struct address_space *mapping = inode->i_mapping;
-	struct buffer_head *bh;
-	struct folio *folio;
-	int err = 0;
+	down_write(&EXT4_I(inode)->i_data_sem);
+	ext4_es_remove_extent(inode, offset >> inode->i_blkbits,
+			DIV_ROUND_UP_ULL(length, EXT4_BLOCK_SIZE(inode->i_sb)));
+	up_write(&EXT4_I(inode)->i_data_sem);
+}
 
-	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT,
-				    FGP_WRITEBEGIN | FGP_ACCESSED,
-				    mapping_gfp_constraint(mapping, ~__GFP_FS));
-	if (IS_ERR(folio))
-		return ERR_CAST(folio);
+static int ext4_iomap_buffered_da_write_end(struct inode *inode, loff_t offset,
+					    loff_t length, ssize_t written,
+					    unsigned int flags,
+					    struct iomap *iomap)
+{
+	loff_t start_byte, end_byte;
 
-	blocksize = inode->i_sb->s_blocksize;
+	/* If we didn't reserve the blocks, we're not allowed to punch them. */
+	if (iomap->type != IOMAP_DELALLOC || !(iomap->flags & IOMAP_F_NEW))
+		return 0;
 
-	iblock = EXT4_PG_TO_LBLK(inode, folio->index);
+	/*
+	 * iomap_page_mkwrite() will never fail in a way that requires delalloc
+	 * extents that it allocated to be revoked.  Hence never try to release
+	 * them here.
+	 */
+	if (flags & IOMAP_FAULT)
+		return 0;
 
-	bh = folio_buffers(folio);
-	if (!bh)
-		bh = create_empty_buffers(folio, blocksize, 0);
+	/* Nothing to do if we've written the entire delalloc extent */
+	start_byte = iomap_last_written_block(inode, offset, written);
+	end_byte = round_up(offset + length, i_blocksize(inode));
+	if (start_byte >= end_byte)
+		return 0;
+
+	filemap_invalidate_lock(inode->i_mapping);
+	iomap_write_delalloc_release(inode, start_byte, end_byte, flags,
+				     iomap, ext4_iomap_punch_delalloc);
+	filemap_invalidate_unlock(inode->i_mapping);
+	return 0;
+}
+
+static int ext4_iomap_zero_begin(struct inode *inode,
+		loff_t offset, loff_t length, unsigned int flags,
+		struct iomap *iomap, struct iomap *srcmap)
+{
+	struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
+	struct ext4_map_blocks map;
+	u8 blkbits = inode->i_blkbits;
+	unsigned int iomap_flags;
+	int ret;
+
+	ret = ext4_emergency_state(inode->i_sb);
+	if (unlikely(ret))
+		return ret;
+
+	if (WARN_ON_ONCE(!(flags & IOMAP_ZERO)))
+		return -EINVAL;
+
+again:
+	ret = ext4_iomap_map_blocks(inode, offset, length, &map, 0);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Look up dirty folios for unwritten mappings within EOF. Providing
+	 * this bypasses the flush iomap uses to trigger extent conversion
+	 * when unwritten mappings have dirty pagecache in need of zeroing.
+	 */
+	iomap_flags = 0;
+	if (map.m_flags & EXT4_MAP_UNWRITTEN) {
+		loff_t start = ((loff_t)map.m_lblk) << blkbits;
+		loff_t end = ((loff_t)map.m_lblk + map.m_len) << blkbits;
+		unsigned int count;
+
+		count = iomap_fill_dirty_folios(iter, &start, end,
+						&iomap_flags);
+		if ((start >> blkbits) < map.m_lblk + map.m_len)
+			map.m_len = (start >> blkbits) - map.m_lblk;
+
+		/*
+		 * This can be raced by a concurrent writeback that cleans
+		 * the folio and converts the unwritten extent to written.
+		 * Recheck the mapping after a folio lock round in
+		 * iomap_fill_dirty_folios().
+		 */
+		if (count == 0 &&
+		    map.m_seq != READ_ONCE(EXT4_I(inode)->i_es_seq))
+			goto again;
+	}
+
+	trace_ext4_iomap_zero_begin(inode, &map, offset, length, flags);
+	ext4_set_iomap(inode, iomap, &map, offset, length, flags);
+	iomap->flags |= iomap_flags;
+
+	return 0;
+}
+
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_zero_next, ext4_iomap_zero_begin);
+
+static const struct iomap_ops ext4_iomap_zero_ops = {
+	.iomap_next = ext4_iomap_zero_next,
+};
+
+/*
+ * Since we always allocate unwritten extents, there is no need for
+ * iomap_end to clean up allocated blocks on a short write.
+ */
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next,
+			      ext4_iomap_buffered_write_begin);
+
+const struct iomap_ops ext4_iomap_buffered_write_ops = {
+	.iomap_next = ext4_iomap_buffered_write_next,
+};
+
+static DEFINE_IOMAP_ITER_NEXT_END(ext4_iomap_buffered_da_write_next,
+				  ext4_iomap_buffered_da_write_begin,
+				  ext4_iomap_buffered_da_write_end);
+
+const struct iomap_ops ext4_iomap_buffered_da_write_ops = {
+	.iomap_next = ext4_iomap_buffered_da_write_next,
+};
+
+static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_buffered_read_next,
+			      ext4_iomap_buffered_read_begin);
+
+static const struct iomap_ops ext4_iomap_buffered_read_ops = {
+	.iomap_next = ext4_iomap_buffered_read_next,
+};
+
+static int ext4_iomap_read_folio(struct file *file, struct folio *folio)
+{
+	iomap_bio_read_folio(folio, &ext4_iomap_buffered_read_ops);
+	return 0;
+}
+
+static void ext4_iomap_readahead(struct readahead_control *rac)
+{
+	iomap_bio_readahead(rac, &ext4_iomap_buffered_read_ops);
+}
+
+
+static int ext4_iomap_map_writeback_range(struct iomap_writepage_ctx *wpc,
+					  loff_t offset, unsigned int dirty_len)
+{
+	struct inode *inode = wpc->inode;
+	struct super_block *sb = inode->i_sb;
+	struct journal_s *journal = EXT4_SB(sb)->s_journal;
+	struct ext4_map_blocks map;
+	ext4_lblk_t index = offset >> inode->i_blkbits;
+	unsigned int blk_len, blk_end;
+	int retries = 0;
+	int ret;
+
+	ret = ext4_emergency_state(sb);
+	if (unlikely(ret))
+		return ret;
+
+	/* Check validity of the cached writeback mapping. */
+	if (offset >= wpc->iomap.offset &&
+	    offset < wpc->iomap.offset + wpc->iomap.length &&
+	    ext4_iomap_valid(inode, &wpc->iomap))
+		return 0;
+
+	blk_len = dirty_len >> inode->i_blkbits;
+	blk_end = umin(wpc->wbc->range_end >> inode->i_blkbits, UINT_MAX - 1);
+	if (blk_end > index + blk_len)
+		blk_len = blk_end - index + 1;
+
+retry:
+	map.m_lblk = index;
+	map.m_len = min_t(unsigned int, MAX_WRITEPAGES_EXTENT_LEN, blk_len);
+	ret = ext4_map_blocks(NULL, inode, &map,
+			      EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT |
+			      EXT4_GET_BLOCKS_METADATA_NOFAIL |
+			      EXT4_GET_BLOCKS_IO_SUBMIT |
+			      EXT4_EX_NOCACHE);
+	if (ret < 0) {
+		if (ext4_emergency_state(sb))
+			return ret;
+
+		/*
+		 * Retry transient ENOSPC errors, if
+		 * ext4_count_free_blocks() is non-zero, a commit
+		 * should free up blocks.
+		 */
+		if (ret == -ENOSPC && journal && ext4_count_free_clusters(sb) &&
+		    ++retries <= 3) {
+			jbd2_journal_force_commit_nested(journal);
+			goto retry;
+		}
+
+		ext4_msg(sb, KERN_CRIT,
+			 "Delayed block allocation failed for inode %llu at logical offset %llu with max blocks %u with error %d",
+			 inode->i_ino, (unsigned long long)map.m_lblk,
+			 (unsigned int)map.m_len, -ret);
+		ext4_msg(sb, KERN_CRIT,
+			 "This should not happen!! Data will be lost\n");
+		if (ret == -ENOSPC)
+			ext4_print_free_blocks(inode);
+		return ret;
+	}
+
+	trace_ext4_iomap_map_writeback_range(inode, &map, offset, dirty_len, 0);
+	ext4_set_iomap(inode, &wpc->iomap, &map, offset, dirty_len, 0);
+	return 0;
+}
+
+static void ext4_iomap_discard_folio(struct folio *folio, loff_t pos)
+{
+	struct inode *inode = folio->mapping->host;
+	loff_t length = folio_pos(folio) + folio_size(folio) - pos;
+	loff_t pstart, plen;
+
+	ext4_iomap_punch_delalloc(inode, pos, length, NULL);
+
+	/*
+	 * Clear the disksize-grow-pending state if the zeroed EOF block
+	 * fails to write back and is discarded.
+	 */
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (plen && pos <= pstart && folio_next_pos(folio) >= pstart + plen)
+		ext4_iomap_clear_disksize_pending(inode);
+}
+
+static ssize_t ext4_iomap_writeback_range(struct iomap_writepage_ctx *wpc,
+					  struct folio *folio, u64 offset,
+					  unsigned int len, u64 end_pos)
+{
+	ssize_t ret;
+
+	ret = ext4_iomap_map_writeback_range(wpc, offset, len);
+	if (!ret)
+		ret = iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
+	if (ret < 0)
+		ext4_iomap_discard_folio(folio, offset);
+	return ret;
+}
+
+static int ext4_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,
+				       int error)
+{
+	struct iomap_ioend *ioend = wpc->wb_ctx;
+	struct inode *inode = ioend->io_inode;
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	unsigned int blocksize = i_blocksize(inode);
+	loff_t pstart, plen;
+
+	/*
+	 * After I/O completion, a worker needs to be scheduled when:
+	 * 1) Unwritten extents require conversion.
+	 * 2) The file size needs to be extended.
+	 * 3) The journal needs to be aborted due to an I/O error.
+	 */
+	if ((ioend->io_flags & IOMAP_IOEND_UNWRITTEN) ||
+	    (ioend->io_offset + ioend->io_size > READ_ONCE(ei->i_disksize)) ||
+	    test_opt(ioend->io_inode->i_sb, DATA_ERR_ABORT))
+		ioend->io_bio.bi_end_io = ext4_iomap_end_bio;
+
+	/*
+	 * Mark the I/O as DISKSIZE_GROW_IO by setting io_private to
+	 * EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO if it covers the pending range.
+	 * Such I/O will allow or trigger i_disksize advancement in the
+	 * ioend worker.
+	 */
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (plen &&
+	    round_down(ioend->io_offset, blocksize) <= pstart &&
+	    round_up(ioend->io_offset + ioend->io_size, blocksize) >=
+			pstart + plen) {
+		trace_ext4_iomap_wb_disksize_pending_submit(inode,
+				ioend->io_offset, ioend->io_size);
+		ioend->io_bio.bi_end_io = ext4_iomap_end_bio;
+		ioend->io_private = (void *)EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO;
+	}
+
+	/*
+	 * ext4_iomap_end_bio() always defers endio processing, disable
+	 * generic BIO in task to avoid double deferral since we will use
+	 * a private defer endio handler in process context.
+	 *
+	 * TODO: Switch all defer handlers to the generic bio complete
+	 * in task framework.
+	 */
+	if (ioend->io_bio.bi_end_io)
+		bio_clear_flag(&ioend->io_bio, BIO_COMPLETE_IN_TASK);
+
+	return iomap_ioend_writeback_submit(wpc, error);
+}
+
+static const struct iomap_writeback_ops ext4_writeback_ops = {
+	.writeback_range = ext4_iomap_writeback_range,
+	.writeback_submit = ext4_iomap_writeback_submit,
+};
+
+/*
+ * If the current writeback range begins after the pending zeroed EOF
+ * block range which straddles i_disksize, issue a separate writeback to
+ * flush it first, so as to avoid prolonged waiting.
+ */
+static void ext4_iomap_wb_submit_zeroed_eof(struct inode *inode,
+					    struct writeback_control *wbc)
+{
+	struct address_space *mapping = inode->i_mapping;
+	loff_t pstart, plen, range_start;
+
+	if (wbc->range_cyclic)
+		range_start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
+	else
+		range_start = wbc->range_start;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen)
+		return;
+	if (round_down(range_start, mapping_min_folio_nrbytes(mapping)) <
+	    pstart + plen)
+		return;
+
+	/* Keep the caller's sync mode to avoid stalling the background flusher. */
+	if (wbc->sync_mode == WB_SYNC_ALL)
+		filemap_fdatawrite_range(mapping, pstart, pstart + plen - 1);
+	else
+		filemap_flush_range(mapping, pstart, pstart + plen - 1);
+}
+
+static int ext4_iomap_writepages(struct address_space *mapping,
+				 struct writeback_control *wbc)
+{
+	struct inode *inode = mapping->host;
+	struct super_block *sb = inode->i_sb;
+	long nr = wbc->nr_to_write;
+	int alloc_ctx, ret;
+	struct iomap_writepage_ctx wpc = {
+		.inode = inode,
+		.wbc = wbc,
+		.ops = &ext4_writeback_ops,
+	};
+
+	ret = ext4_emergency_state(sb);
+	if (unlikely(ret))
+		return ret;
+
+	/*
+	 * Submit the pending zeroed EOF block range if the entire
+	 * writeback range lies beyond it.
+	 */
+	ext4_iomap_wb_submit_zeroed_eof(inode, wbc);
+
+	alloc_ctx = ext4_writepages_down_read(sb);
+	/*
+	 * Does ext4_change_inode_journal_flag() change the inode's
+	 * buffered I/O path?
+	 */
+	if (!ext4_inode_buffered_iomap(inode)) {
+		ext4_writepages_up_read(sb, alloc_ctx);
+		return ret;
+	}
+
+	trace_ext4_writepages(inode, wbc);
+	ret = iomap_writepages(&wpc);
+	trace_ext4_writepages_result(inode, wbc, ret, nr - wbc->nr_to_write);
+	ext4_writepages_up_read(sb, alloc_ctx);
+
+	return ret;
+}
+
+/*
+ * For data=journal mode, folio should be marked dirty only when it was
+ * writeably mapped. When that happens, it was already attached to the
+ * transaction and marked as jbddirty (we take care of this in
+ * ext4_page_mkwrite()). On transaction commit, we writeprotect page mappings
+ * so we should have nothing to do here, except for the case when someone
+ * had the page pinned and dirtied the page through this pin (e.g. by doing
+ * direct IO to it). In that case we'd need to attach buffers here to the
+ * transaction but we cannot due to lock ordering.  We cannot just dirty the
+ * folio and leave attached buffers clean, because the buffers' dirty state is
+ * "definitive".  We cannot just set the buffers dirty or jbddirty because all
+ * the journalling code will explode.  So what we do is to mark the folio
+ * "pending dirty" and next time ext4_writepages() is called, attach buffers
+ * to the transaction appropriately.
+ */
+static bool ext4_journalled_dirty_folio(struct address_space *mapping,
+		struct folio *folio)
+{
+	WARN_ON_ONCE(!folio_buffers(folio));
+	if (folio_maybe_dma_pinned(folio))
+		folio_set_checked(folio);
+	return filemap_dirty_folio(mapping, folio);
+}
+
+static bool ext4_dirty_folio(struct address_space *mapping, struct folio *folio)
+{
+	WARN_ON_ONCE(!folio_test_locked(folio) && !folio_test_dirty(folio));
+	WARN_ON_ONCE(!folio_buffers(folio));
+	return block_dirty_folio(mapping, folio);
+}
+
+static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
+				    struct file *file, sector_t *span)
+{
+	return iomap_swapfile_activate(sis, file, span,
+				       &ext4_iomap_report_ops);
+}
+
+static const struct address_space_operations ext4_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_write_begin,
+	.write_end		= ext4_write_end,
+	.dirty_folio		= ext4_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_journalled_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_write_begin,
+	.write_end		= ext4_journalled_write_end,
+	.dirty_folio		= ext4_journalled_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_journalled_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio_norefs,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_da_aops = {
+	.read_folio		= ext4_read_folio,
+	.readahead		= ext4_readahead,
+	.writepages		= ext4_writepages,
+	.write_begin		= ext4_da_write_begin,
+	.write_end		= ext4_da_write_end,
+	.dirty_folio		= ext4_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= ext4_invalidate_folio,
+	.release_folio		= ext4_release_folio,
+	.migrate_folio		= buffer_migrate_folio,
+	.is_partially_uptodate  = block_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_iomap_aops = {
+	.read_folio		= ext4_iomap_read_folio,
+	.readahead		= ext4_iomap_readahead,
+	.writepages		= ext4_iomap_writepages,
+	.dirty_folio		= iomap_dirty_folio,
+	.bmap			= ext4_bmap,
+	.invalidate_folio	= iomap_invalidate_folio,
+	.release_folio		= iomap_release_folio,
+	.migrate_folio		= filemap_migrate_folio,
+	.is_partially_uptodate  = iomap_is_partially_uptodate,
+	.error_remove_folio	= generic_error_remove_folio,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+static const struct address_space_operations ext4_dax_aops = {
+	.writepages		= ext4_dax_writepages,
+	.dirty_folio		= noop_dirty_folio,
+	.bmap			= ext4_bmap,
+	.swap_activate		= ext4_iomap_swap_activate,
+};
+
+void ext4_set_aops(struct inode *inode)
+{
+	switch (ext4_inode_journal_mode(inode)) {
+	case EXT4_INODE_ORDERED_DATA_MODE:
+	case EXT4_INODE_WRITEBACK_DATA_MODE:
+		break;
+	case EXT4_INODE_JOURNAL_DATA_MODE:
+		inode->i_mapping->a_ops = &ext4_journalled_aops;
+		return;
+	default:
+		BUG();
+	}
+	if (IS_DAX(inode))
+		inode->i_mapping->a_ops = &ext4_dax_aops;
+	else if (ext4_inode_buffered_iomap(inode))
+		inode->i_mapping->a_ops = &ext4_iomap_aops;
+	else if (test_opt(inode->i_sb, DELALLOC))
+		inode->i_mapping->a_ops = &ext4_da_aops;
+	else
+		inode->i_mapping->a_ops = &ext4_aops;
+}
+
+/*
+ * Here we can't skip an unwritten buffer even though it usually reads zero
+ * because it might have data in pagecache (eg, if called from ext4_zero_range,
+ * ext4_punch_hole, etc) which needs to be properly zeroed out. Otherwise a
+ * racing writeback can come later and flush the stale pagecache to disk.
+ *
+ * Return the loaded bh if it actually needs zeroing - in written, dirty
+ * unwritten, or delalloc state. Return NULL if it's clean (i.e., a hole or
+ * a clean unwritten block).
+ */
+static struct buffer_head *ext4_load_tail_bh(struct inode *inode, loff_t from)
+{
+	unsigned int offset, blocksize, pos;
+	ext4_lblk_t iblock;
+	struct address_space *mapping = inode->i_mapping;
+	struct buffer_head *bh;
+	struct folio *folio;
+	int err = 0;
+
+	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT,
+				    FGP_WRITEBEGIN | FGP_ACCESSED,
+				    mapping_gfp_constraint(mapping, ~__GFP_FS));
+	if (IS_ERR(folio))
+		return ERR_CAST(folio);
+
+	blocksize = inode->i_sb->s_blocksize;
+
+	iblock = EXT4_PG_TO_LBLK(inode, folio->index);
+
+	bh = folio_buffers(folio);
+	if (!bh)
+		bh = create_empty_buffers(folio, blocksize, 0);
 
 	/* Find the buffer that contains "offset" */
 	offset = offset_in_folio(folio, from);
@@ -4167,6 +4802,48 @@ static int ext4_block_journalled_zero_range(struct inode *inode, loff_t from,
 	return err;
 }
 
+static int ext4_block_iomap_zero_range(struct inode *inode, loff_t from,
+				       loff_t length, bool *did_zero,
+				       bool *zero_written)
+{
+	int ret;
+
+	/*
+	 * Zeroing out under an active handle can cause deadlock since
+	 * the order of acquiring the folio lock and starting a handle is
+	 * inconsistent with the iomap writeback procedure.
+	 */
+	if (WARN_ON_ONCE(ext4_handle_valid(journal_current_handle())))
+		return -EINVAL;
+
+	/* The zeroing scope should not extend across a block. */
+	if (WARN_ON_ONCE((from >> inode->i_blkbits) !=
+			 ((from + length - 1) >> inode->i_blkbits)))
+		return -EINVAL;
+
+	if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS) &&
+	    !(inode_state_read_once(inode) & (I_NEW | I_FREEING)))
+		WARN_ON_ONCE(!inode_is_locked(inode) &&
+			!rwsem_is_locked(&inode->i_mapping->invalidate_lock));
+
+	ret = iomap_zero_range(inode, from, length, did_zero,
+			       &ext4_iomap_zero_ops, &ext4_iomap_write_ops,
+			       NULL);
+	if (ret)
+		return ret;
+
+	/*
+	 * TODO: The iomap does not distinguish between different types
+	 * of zeroing operations. So we always set zero_written whenever
+	 * zeroing is performed, which may cause unnecessary folio
+	 * flushing when zeroing occurs on delayed-allocated blocks.
+	 */
+	if (did_zero && zero_written)
+		*zero_written = *did_zero;
+
+	return 0;
+}
+
 /*
  * Zeros out a mapping of length 'length' starting from file offset
  * 'from'.  The range to be zero'd must be contained with in one block.
@@ -4193,11 +4870,132 @@ static int ext4_block_zero_range(struct inode *inode,
 	} else if (ext4_should_journal_data(inode)) {
 		return ext4_block_journalled_zero_range(inode, from, length,
 							did_zero);
+	} else if (ext4_inode_buffered_iomap(inode)) {
+		return ext4_block_iomap_zero_range(inode, from, length,
+						   did_zero, zero_written);
 	}
 	return ext4_block_do_zero_range(inode, from, length, did_zero,
 					zero_written);
 }
 
+/*
+ * Inodes using the iomap buffered I/O path do not use data=ordered mode.
+ * Therefore, we mark the inode as disksize-grow-pending after zeroing the
+ * EOF block. The zeroed block will be submitted before any subsequent
+ * data.
+ *
+ * In the I/O completion path, ext4_iomap_wb_disksize_pending_wait() will
+ * wait for I/O completion before advancing i_disksize if the write
+ * extends beyond the zeroed boundary.
+ *
+ * When zeroed I/O is in progress, operations that extend i_disksize are
+ * handled as follows:
+ *
+ *  - Truncate up, append fallocate and zero_range:
+ *    Defer the update. The file size will be updated to i_size by the
+ *    end_io handler once the ongoing pending I/O completes.
+ *
+ *  - Insert range and collapse range operations:
+ *    Wait synchronously for the relevant I/O to complete before updating
+ *    i_disksize.
+ */
+static int ext4_iomap_mark_disksize_pending(struct inode *inode, loff_t from)
+{
+	struct folio *folio;
+
+	folio = filemap_lock_folio(inode->i_mapping, from >> PAGE_SHIFT);
+	if (IS_ERR(folio))
+		/* Already in writeback and cleared? */
+		return PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);
+
+	/*
+	 * Ensure that in-flight writeback, possibly started after
+	 * iomap_zero_range() unlocked the folio, has completed. Without
+	 * this wait folio_test_dirty() below may miss the zeroed data
+	 * (writeback clears PG_dirty), causing us to skip the
+	 * disksize-grow-pending tracking and potentially expose stale
+	 * on-disk data.
+	 */
+	folio_wait_writeback(folio);
+	WARN_ON_ONCE(folio_test_writeback(folio));
+
+	/*
+	 * If the zeroed range does not overlap the existing on-disk tail
+	 * block, the zeroed data lies beyond the currently on-disk data
+	 * and will be written back before i_disksize is advanced past it,
+	 * so no stale data can be exposed.
+	 *
+	 * Checking i_disksize here (after folio_wait_writeback()) is
+	 * necessary and safe. If a delalloc writeback of this folio was
+	 * in-flight, it could be raced by a concurrent mmap write which
+	 * corrupts the tail block beyond i_size but the i_disksize is not
+	 * advanced. folio_wait_writeback() has waited for its completion
+	 * and the ioend has advanced i_disksize accordingly. If writeback
+	 * had not started, we don't need to mark any pending state because
+	 * any future writeback will carry the pagecache content that now
+	 * includes the zeroed data, so no stale data can appear on disk
+	 * even without the pending tracking.
+	 */
+	if (from >= round_up(READ_ONCE(EXT4_I(inode)->i_disksize),
+			     i_blocksize(inode)))
+		goto out;
+
+	/*
+	 * Mark the inode as disksize-grow-pending. The zeroed block will
+	 * be written out by the generic writepages cycle or any other
+	 * syncing operation.
+	 *
+	 * Multiple overlapping unaligned EOF writes should not happen,
+	 * because we only mark the pending state after zeroing the on-disk
+	 * EOF block, and i_disksize can only be updated after the previous
+	 * zeroed pending block has been written back or the dirty folio
+	 * has been discarded.
+	 */
+	if (likely(folio_test_dirty(folio) &&
+		   !ext4_test_inode_state(inode,
+					  EXT4_STATE_DISKSIZE_GROW_PENDING))) {
+		trace_ext4_iomap_mark_disksize_pending(inode);
+		ext4_set_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING);
+	}
+out:
+	folio_unlock(folio);
+	folio_put(folio);
+	return 0;
+}
+
+/*
+ * Submit and wait for the pending zeroed EOF block range to complete
+ * if the given range [@offset, @end) fully covers it.  Must be called
+ * outside the context of an active journal handle and hold the i_rwsem.
+ */
+int ext4_iomap_sync_zeroed_eof(struct inode *inode, loff_t offset, loff_t end)
+{
+	loff_t pstart, plen;
+	int ret;
+
+	if (!ext4_inode_buffered_iomap(inode))
+		return 0;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen || offset > pstart || end < pstart + plen)
+		return 0;
+
+	ret = filemap_write_and_wait_range(inode->i_mapping, pstart,
+					   pstart + plen - 1);
+	if (ret)
+		return ret;
+
+	/*
+	 * The pending bit should have been cleared by the ioend worker,
+	 * which runs before the folio writeback flag is cleared.  The
+	 * caller holds i_rwsem, so no concurrent ext4_block_zero_eof()
+	 * can re-set it.
+	 */
+	WARN_ON_ONCE(ext4_test_inode_state(inode,
+				EXT4_STATE_DISKSIZE_GROW_PENDING));
+	return 0;
+}
+
 /*
  * Zero out a mapping from file offset 'from' up to the end of the block
  * which corresponds to 'from' or to the given 'end' inside this block.
@@ -4242,20 +5040,24 @@ int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)
 	 * exposing stale on-disk data which may caused by concurrent post-EOF
 	 * mmap write during folio writeback.
 	 */
-	if (ext4_should_order_data(inode) &&
-	    did_zero && zero_written && !IS_DAX(inode)) {
-		handle_t *handle;
+	if (did_zero && zero_written && !IS_DAX(inode)) {
+		if (ext4_should_order_data(inode)) {
+			handle_t *handle;
 
-		handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
-		if (IS_ERR(handle))
-			return PTR_ERR(handle);
+			handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
+			if (IS_ERR(handle))
+				return PTR_ERR(handle);
 
-		err = ext4_jbd2_inode_add_write(handle, inode, from, length);
-		ext4_journal_stop(handle);
+			err = ext4_jbd2_inode_add_write(handle, inode, from,
+							length);
+			ext4_journal_stop(handle);
+		} else if (ext4_inode_buffered_iomap(inode))
+			err = ext4_iomap_mark_disksize_pending(inode, from);
 		if (err)
 			return err;
 	}
 
+	trace_ext4_block_zero_eof(inode, from, length, did_zero, zero_written);
 	return 0;
 }
 
@@ -4352,6 +5154,14 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
 	if (offset > size)
 		return 0;
 
+	/*
+	 * We are going to punch the pending zeroed EOF block, persist
+	 * it to ensure i_disksize can be safely updated thereafter.
+	 */
+	ret = ext4_iomap_sync_zeroed_eof(inode, offset, offset + len);
+	if (ret)
+		return ret;
+
 	if (offset + len < size)
 		size = offset + len;
 	if (EXT4_I(inode)->i_disksize >= size)
@@ -4503,7 +5313,14 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
 	ret = ext4_zero_partial_blocks(inode, offset, length, &partial_zeroed);
 	if (ret)
 		return ret;
-	if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && partial_zeroed) {
+	/*
+	 * On the iomap path, partial invalidate of a folio without an ifs
+	 * leaves sub-block dirty bits uncleared.  Drain writeback before
+	 * removing extents so that any bio in flight on a partial folio
+	 * completes against blocks still owned by this inode.
+	 */
+	if (ext4_inode_buffered_iomap(inode) ||
+	    (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && partial_zeroed)) {
 		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
 						   end - 1);
 		if (ret)
@@ -5281,6 +6098,87 @@ static int check_igot_inode(struct inode *inode, ext4_iget_flags flags,
 	return -EFSCORRUPTED;
 }
 
+/*
+ * Determine whether an inode should use the iomap buffered I/O path.
+ * EXT4_STATE_BUFFERED_IOMAP is generally set at inode initialization
+ * time. Online switching of the buffered I/O path on an active inode is
+ * NOT supported, with the exception of changing a per-inode journal
+ * flag.
+ *
+ * For features like inline data, fsverity, and encryption that can be
+ * dynamically enabled or disabled, we check the superblock-level
+ * feature flags. If any of these is globally enabled, no inode is
+ * allowed into the iomap buffered I/O path. This avoids the complexity
+ * of dynamic toggling.
+ *
+ * For the global data journal mode (EXT4_MOUNT_JOURNAL_DATA), dynamic
+ * change through remount is deferred. It will only become available
+ * after the inode is re-initialized (i.e., after the last reference
+ * drops and the inode is re-read from disk with the journal flag
+ * cleared).
+ *
+ * For the per-inode data journal mode (EXT4_INODE_JOURNAL_DATA),
+ * dynamic changes take effect immediately. This is safe because
+ * address_space operations can be switched and all page cache can be
+ * dropped under i_rwsem and filemap_invalidate_lock.
+ *
+ * For extent-to-indirect block migration (via EXT4_IOC_SETFLAGS
+ * clearing EXT4_EXTENTS_FL), this operation is directly rejected for
+ * inodes using the iomap path.
+ *
+ * When remounting to toggle the buffered_iomap mount option, the change
+ * of I/O path is deferred as well, it will be available after the inode
+ * is re-initialized.
+ */
+void ext4_enable_buffered_iomap(struct inode *inode)
+{
+	struct super_block *sb = inode->i_sb;
+
+	if (!test_opt2(sb, BUFFERED_IOMAP))
+		return;
+	if (!S_ISREG(inode->i_mode))
+		return;
+	if (ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
+		return;
+
+	/* Unsupported Features */
+	if (ext4_has_feature_inline_data(sb))
+		return;
+	if (ext4_has_feature_verity(sb))
+		return;
+	if (ext4_has_feature_encrypt(sb))
+		return;
+	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
+	    ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA))
+		return;
+	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
+		return;
+
+	ext4_set_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+
+	/*
+	 * Install the iomap end_io handler on the shared conversion
+	 * work.  This is safe at inode initialization and during the
+	 * buffered I/O path changes where we flush all pending
+	 * writebacks and drop page cache under i_rwsem and
+	 * filemap_invalidate_lock.
+	 */
+	INIT_WORK(&EXT4_I(inode)->i_rsv_conversion_work, ext4_iomap_end_io);
+}
+
+static void ext4_disable_buffered_iomap(struct inode *inode)
+{
+	ext4_clear_inode_state(inode, EXT4_STATE_BUFFERED_IOMAP);
+
+	/*
+	 * Reinstall the buffer_head end_io handler on the shared
+	 * conversion work.  This is safe during the buffered I/O path
+	 * changes where we flush all pending writebacks and drop page
+	 * cache under i_rwsem and filemap_invalidate_lock.
+	 */
+	INIT_WORK(&EXT4_I(inode)->i_rsv_conversion_work, ext4_end_io_rsv_work);
+}
+
 void ext4_set_inode_mapping_order(struct inode *inode)
 {
 	struct super_block *sb = inode->i_sb;
@@ -5595,6 +6493,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
 	if (ret)
 		goto bad_inode;
 
+	ext4_enable_buffered_iomap(inode);
+
 	if (S_ISREG(inode->i_mode)) {
 		inode->i_op = &ext4_file_inode_operations;
 		inode->i_fop = &ext4_file_operations;
@@ -5982,6 +6882,125 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
 	}
 }
 
+/*
+ * Set i_size and i_disksize to 'newsize'.
+ *
+ * Both i_rwsem and i_data_sem are required here to avoid races between
+ * generic append writeback (or zeroed pending I/O writeback) and
+ * concurrent operations (e.g., fallocate, truncate) that also modify
+ * i_size and i_disksize. This also ensures that the writeback ioend worker
+ * observes the latest i_size under the same lock protection.
+ */
+static inline void ext4_set_inode_size(struct inode *inode, loff_t newsize)
+{
+	WARN_ON_ONCE(S_ISREG(inode->i_mode) && !inode_is_locked(inode));
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+	i_size_write(inode, newsize);
+	__ext4_set_i_disksize(inode, newsize);
+	up_write(&EXT4_I(inode)->i_data_sem);
+}
+
+static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)
+{
+	ext4_lblk_t old_lblk, new_lblk;
+	handle_t *handle;
+	int ret;
+
+	if (!IS_ALIGNED(oldsize | newsize, i_blocksize(inode))) {
+		ret = ext4_inode_attach_jinode(inode);
+		if (ret)
+			return ret;
+	}
+
+	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
+	if (!IS_ALIGNED(oldsize, i_blocksize(inode))) {
+		ret = ext4_block_zero_eof(inode, oldsize, LLONG_MAX);
+		if (ret)
+			return ret;
+	}
+
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
+	if (IS_ERR(handle))
+		return PTR_ERR(handle);
+
+	old_lblk = oldsize > 0 ? (oldsize - 1) >> inode->i_blkbits : 0;
+	new_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0;
+	ext4_fc_track_range(handle, inode, old_lblk, new_lblk);
+
+	ext4_set_inode_size(inode, newsize);
+
+	ret = ext4_mark_inode_dirty(handle, inode);
+	ext4_journal_stop(handle);
+	if (ret)
+		return ret;
+	/*
+	 * isize extend must be called outside an active handle due to
+	 * the lock ordering of transaction start and folio lock in the
+	 * iomap buffered I/O path (folio lock -> transaction start).
+	 */
+	pagecache_isize_extended(inode, oldsize, newsize);
+	return 0;
+}
+
+static int ext4_truncate_down(struct inode *inode, loff_t oldsize,
+			      loff_t newsize, int *orphan)
+{
+	ext4_lblk_t start_lblk;
+	handle_t *handle;
+	int ret;
+
+	/* Do not change i_size. */
+	if (newsize == oldsize)
+		goto truncate;
+
+	/* Shrink. */
+	handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
+	if (IS_ERR(handle))
+		return PTR_ERR(handle);
+
+	if (ext4_handle_valid(handle)) {
+		ret = ext4_orphan_add(handle, inode);
+		*orphan = 1;
+		if (ret) {
+			ext4_journal_stop(handle);
+			return ret;
+		}
+	}
+
+	start_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0;
+	ext4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1);
+
+	down_write(&EXT4_I(inode)->i_data_sem);
+	/*
+	 * Truncate the zeroed EOF block invalidates the pending disksize
+	 * update, so clear the disksize-grow-pending state.
+	 */
+	if (ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING) &&
+	    (newsize <= EXT4_I(inode)->i_disksize))
+		ext4_iomap_clear_disksize_pending(inode);
+
+	i_size_write(inode, newsize);
+	__ext4_set_i_disksize(inode, newsize);
+	up_write(&EXT4_I(inode)->i_data_sem);
+
+	ret = ext4_mark_inode_dirty(handle, inode);
+	ext4_journal_stop(handle);
+	if (ret)
+		return ret;
+
+	if (ext4_should_journal_data(inode))
+		ext4_wait_for_tail_page_commit(inode);
+truncate:
+	/*
+	 * Truncate pagecache after we've waited for commit in data=journal
+	 * mode to make pages freeable.  Call ext4_truncate() even if
+	 * i_size didn't change to truncate possible preallocated blocks.
+	 */
+	truncate_pagecache(inode, newsize);
+	return ext4_truncate(inode);
+}
+
 /*
  * ext4_setattr()
  *
@@ -6078,9 +7097,7 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	}
 
 	if (attr->ia_valid & ATTR_SIZE) {
-		handle_t *handle;
 		loff_t oldsize = inode->i_size;
-		loff_t old_disksize;
 		int shrink = (attr->ia_size < inode->i_size);
 
 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
@@ -6131,97 +7148,14 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 			goto err_out;
 		}
 
-		if (attr->ia_size != inode->i_size) {
-			/* attach jbd2 jinode for EOF folio tail zeroing */
-			if (attr->ia_size & (inode->i_sb->s_blocksize - 1) ||
-			    oldsize & (inode->i_sb->s_blocksize - 1)) {
-				error = ext4_inode_attach_jinode(inode);
-				if (error)
-					goto out_mmap_sem;
-			}
-
-			/*
-			 * Update c/mtime and tail zero the EOF folio on
-			 * truncate up. ext4_truncate() handles the shrink case
-			 * below.
-			 */
-			if (!shrink) {
-				inode_set_mtime_to_ts(inode,
-						      inode_set_ctime_current(inode));
-				if (oldsize & (inode->i_sb->s_blocksize - 1)) {
-					error = ext4_block_zero_eof(inode,
-							oldsize, LLONG_MAX);
-					if (error)
-						goto out_mmap_sem;
-				}
-			}
-
-			handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
-			if (IS_ERR(handle)) {
-				error = PTR_ERR(handle);
-				goto out_mmap_sem;
-			}
-			if (ext4_handle_valid(handle) && shrink) {
-				error = ext4_orphan_add(handle, inode);
-				orphan = 1;
-			}
-
-			if (shrink)
-				ext4_fc_track_range(handle, inode,
-					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
-					inode->i_sb->s_blocksize_bits,
-					EXT_MAX_BLOCKS - 1);
-			else
-				ext4_fc_track_range(
-					handle, inode,
-					(oldsize > 0 ? oldsize - 1 : oldsize) >>
-					inode->i_sb->s_blocksize_bits,
-					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
-					inode->i_sb->s_blocksize_bits);
-
-			down_write(&EXT4_I(inode)->i_data_sem);
-			old_disksize = EXT4_I(inode)->i_disksize;
-			EXT4_I(inode)->i_disksize = attr->ia_size;
-
-			/*
-			 * We have to update i_size under i_data_sem together
-			 * with i_disksize to avoid races with writeback code
-			 * running ext4_wb_update_i_disksize().
-			 */
-			if (!error)
-				i_size_write(inode, attr->ia_size);
-			else
-				EXT4_I(inode)->i_disksize = old_disksize;
-			up_write(&EXT4_I(inode)->i_data_sem);
-			rc = ext4_mark_inode_dirty(handle, inode);
-			if (!error)
-				error = rc;
-			ext4_journal_stop(handle);
-			if (error)
-				goto out_mmap_sem;
-			if (!shrink) {
-				pagecache_isize_extended(inode, oldsize,
-							 inode->i_size);
-			} else if (ext4_should_journal_data(inode)) {
-				ext4_wait_for_tail_page_commit(inode);
-			}
+		if (attr->ia_size > oldsize)
+			error = ext4_truncate_up(inode, oldsize, attr->ia_size);
+		else {
+			/* Shrink or do not change i_size. */
+			error = ext4_truncate_down(inode, oldsize,
+						   attr->ia_size, &orphan);
 		}
 
-		/*
-		 * Truncate pagecache after we've waited for commit
-		 * in data=journal mode to make pages freeable.
-		 */
-		truncate_pagecache(inode, inode->i_size);
-		/*
-		 * Call ext4_truncate() even if i_size didn't change to
-		 * truncate possible preallocated blocks.
-		 */
-		if (attr->ia_size <= oldsize) {
-			rc = ext4_truncate(inode);
-			if (rc)
-				error = rc;
-		}
-out_mmap_sem:
 		filemap_invalidate_unlock(inode->i_mapping);
 	}
 
@@ -6791,9 +7725,10 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
 	 * the inode's in-core data-journaling state flag now.
 	 */
 
-	if (val)
+	if (val) {
 		ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
-	else {
+		ext4_disable_buffered_iomap(inode);
+	} else {
 		err = jbd2_journal_flush(journal, 0);
 		if (err < 0) {
 			jbd2_journal_unlock_updates(journal);
@@ -6802,6 +7737,7 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
 			return err;
 		}
 		ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
+		ext4_enable_buffered_iomap(inode);
 	}
 	ext4_set_aops(inode);
 	ext4_set_inode_mapping_order(inode);
@@ -6881,6 +7817,23 @@ static int ext4_block_page_mkwrite(struct inode *inode, struct folio *folio,
 	return ret;
 }
 
+static vm_fault_t ext4_iomap_page_mkwrite(struct vm_fault *vmf)
+{
+	struct inode *inode = file_inode(vmf->vma->vm_file);
+	const struct iomap_ops *iomap_ops;
+
+	/*
+	 * ext4_nonda_switch() could writeback this folio, so have to
+	 * call it before lock folio.
+	 */
+	if (test_opt(inode->i_sb, DELALLOC) && !ext4_nonda_switch(inode->i_sb))
+		iomap_ops = &ext4_iomap_buffered_da_write_ops;
+	else
+		iomap_ops = &ext4_iomap_buffered_write_ops;
+
+	return iomap_page_mkwrite(vmf, iomap_ops, NULL);
+}
+
 vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
@@ -6907,6 +7860,11 @@ vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
 	if (err)
 		goto out_ret;
 
+	if (ext4_inode_buffered_iomap(inode)) {
+		ret = ext4_iomap_page_mkwrite(vmf);
+		goto out;
+	}
+
 	/*
 	 * On data journalling we skip straight to the transaction handle:
 	 * there's no delalloc; page truncated will be checked later; the
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index 5d60ef10fe110..09931d3ba2c6a 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -621,6 +621,8 @@ int ext4_ind_migrate(struct inode *inode)
 
 	if (ext4_has_feature_bigalloc(inode->i_sb))
 		return -EOPNOTSUPP;
+	if (ext4_inode_buffered_iomap(inode))
+		return -EOPNOTSUPP;
 
 	/*
 	 * In order to get correct extent info, force all delayed allocation
diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index 3329b7ad5dbd8..948ef3f44df5e 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -476,6 +476,17 @@ static int mext_check_validity(struct inode *orig_inode,
 		return -EOPNOTSUPP;
 	}
 
+	/*
+	 * TODO: support online defrag for inodes that use the buffered
+	 * I/O iomap path.
+	 */
+	if (ext4_inode_buffered_iomap(orig_inode) ||
+	    ext4_inode_buffered_iomap(donor_inode)) {
+		ext4_msg(sb, KERN_ERR,
+			 "Online defrag not supported for inode with iomap buffered IO path");
+		return -EOPNOTSUPP;
+	}
+
 	if (donor_inode->i_mode & (S_ISUID|S_ISGID)) {
 		ext4_debug("ext4 move extent: suid or sgid is set to donor file [ino:orig %llu, donor %llu]\n",
 			   orig_inode->i_ino, donor_inode->i_ino);
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index 0236b6b9785a2..734d856a6d478 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -22,6 +22,7 @@
 #include <linux/bio.h>
 #include <linux/workqueue.h>
 #include <linux/kernel.h>
+#include <linux/iomap.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/sched/mm.h>
@@ -30,6 +31,8 @@
 #include "xattr.h"
 #include "acl.h"
 
+#include <trace/events/ext4.h>
+
 static struct kmem_cache *io_end_cachep;
 static struct kmem_cache *io_end_vec_cachep;
 
@@ -547,3 +550,201 @@ void ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *folio,
 		io_submit_add_bh(io, inode, folio, bh);
 	} while ((bh = bh->b_this_page) != head);
 }
+
+/*
+ * If the current writeback range starts beyond the zeroed EOF pending
+ * range that straddles i_disksize, wait for the zeroed data from
+ * ext4_block_zero_eof() to be written out first. Otherwise, extending
+ * i_disksize may expose stale data in the old EOF block.
+ */
+static void ext4_iomap_wb_disksize_pending_wait(struct inode *inode,
+						loff_t pos, size_t size)
+{
+	loff_t disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	loff_t pstart, plen;
+
+	/*
+	 * Overwrite I/Os and I/Os covering the EOF block do not need to
+	 * wait: the former do not advance i_disksize past the pending
+	 * boundary, and the latter are the pending I/O itself (cleared in
+	 * the bio completion path).
+	 */
+	if (pos < round_up(disksize, i_blocksize(inode)))
+		return;
+
+	plen = ext4_iomap_get_disksize_pending_range(inode, &pstart);
+	if (!plen || pos < pstart + plen)
+		return;
+
+	trace_ext4_iomap_wb_disksize_pending_wait(inode, pos, size);
+	ext4_iomap_wait_disksize_pending(inode);
+}
+
+static int ext4_iomap_wb_update_disksize(handle_t *handle, struct inode *inode,
+					 loff_t end, bool is_disksize_grow)
+{
+	loff_t new_disksize, i_size;
+	struct ext4_inode_info *ei = EXT4_I(inode);
+	int ret;
+
+	/*
+	 * Races with truncate are avoided by checking i_size under
+	 * i_data_sem.
+	 */
+	down_write(&ei->i_data_sem);
+	i_size = i_size_read(inode);
+
+	/*
+	 * EXT4_STATE_DISKSIZE_GROW_PENDING is cleared when the pending
+	 * I/O completes. However, another thread may have re-set the bit
+	 * between that point and here, meaning i_disksize has already
+	 * been advanced and a new EOF zeroing has been initiated. In that
+	 * case, do not advance i_disksize to i_size; leave it to the
+	 * next pending grow ioend.
+	 */
+	if (is_disksize_grow &&
+	    ext4_test_inode_state(inode, EXT4_STATE_DISKSIZE_GROW_PENDING))
+		is_disksize_grow = false;
+
+	/*
+	 * Update i_disksize to i_size when EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO
+	 * completes. This is safe because we never directly allocate written
+	 * blocks during buffered writes.
+	 *
+	 * This ensures that i_disksize is correctly advanced during
+	 * truncate-up or append fallocate on a block-unaligned file,
+	 * preventing it from remaining stale. The tradeoff is that zeroed
+	 * data may be exposed after crash recovery if dirty data in this
+	 * range is not yet on disk, but stale data will never be exposed.
+	 * This is because the extent is only converted to written state
+	 * after the data has been persisted.
+	 */
+	new_disksize = is_disksize_grow ? i_size : min(end, i_size);
+	if (new_disksize > ei->i_disksize) {
+		trace_ext4_iomap_wb_update_disksize(inode, end, i_size,
+				ei->i_disksize, new_disksize, is_disksize_grow);
+		WRITE_ONCE(ei->i_disksize, new_disksize);
+	}
+	up_write(&ei->i_data_sem);
+	ret = ext4_mark_inode_dirty(handle, inode);
+	if (ret)
+		EXT4_ERROR_INODE_ERR(inode, -ret, "Failed to mark inode dirty");
+
+	return ret;
+}
+
+static void ext4_iomap_finish_ioend(struct iomap_ioend *ioend)
+{
+	struct inode *inode = ioend->io_inode;
+	struct super_block *sb = inode->i_sb;
+	loff_t pos = ioend->io_offset;
+	size_t size = ioend->io_size;
+	loff_t end = pos + size;
+	unsigned long io_mode = (unsigned long)ioend->io_private;
+	bool is_disksize_grow = (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO);
+	handle_t *handle;
+	int credits;
+	int ret, err;
+
+	ret = blk_status_to_errno(ioend->io_bio.bi_status);
+	if (is_disksize_grow)
+		trace_ext4_iomap_wb_disksize_pending_complete(ioend->io_inode,
+				ioend->io_offset, ioend->io_size, ret);
+	if (unlikely(ret)) {
+		if (test_opt(sb, DATA_ERR_ABORT) && !ext4_emergency_state(sb))
+			jbd2_journal_abort(EXT4_SB(sb)->s_journal, ret);
+		goto out;
+	}
+
+	if (!(ioend->io_flags & IOMAP_IOEND_UNWRITTEN) &&
+	    end <= READ_ONCE(EXT4_I(inode)->i_disksize) && !is_disksize_grow)
+		goto out;
+
+	/* Wait for disksize-pending zeroed data to be written out. */
+	ext4_iomap_wb_disksize_pending_wait(inode, pos, size);
+
+	/*
+	 * We may need to convert one extent, update the i_disksize and
+	 * dirty the inode.
+	 */
+	credits = ext4_chunk_trans_blocks(inode,
+			EXT4_MAX_BLOCKS(size, pos, inode->i_blkbits));
+	handle = ext4_journal_start(inode, EXT4_HT_EXT_CONVERT, credits);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_err;
+	}
+
+	/* Update on-disk size after I/O is completed. */
+	if (end > READ_ONCE(EXT4_I(inode)->i_disksize) || is_disksize_grow) {
+		ret = ext4_iomap_wb_update_disksize(handle, inode, end,
+						    is_disksize_grow);
+		if (ret)
+			goto out_journal;
+	}
+
+	if (ioend->io_flags & IOMAP_IOEND_UNWRITTEN)
+		ret = ext4_convert_unwritten_extents(handle, inode, pos,
+						     size, NULL);
+
+out_journal:
+	err = ext4_journal_stop(handle);
+	if (!ret)
+		ret = err;
+out_err:
+	if (ret < 0 && !ext4_emergency_state(sb)) {
+		ext4_msg(sb, KERN_EMERG,
+			 "failed to convert unwritten extents to written extents or update inode size -- potential data loss! (inode %llu, error %d)",
+			 inode->i_ino, ret);
+	}
+out:
+	iomap_finish_ioends(ioend, ret);
+}
+
+/*
+ * Work on buffered iomap completed IO, to convert unwritten extents to
+ * mapped extents
+ */
+void ext4_iomap_end_io(struct work_struct *work)
+{
+	struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
+						  i_rsv_conversion_work);
+	struct iomap_ioend *ioend;
+	struct list_head ioend_list;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ei->i_completed_io_lock, flags);
+	list_replace_init(&ei->i_rsv_conversion_list, &ioend_list);
+	spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
+
+	iomap_sort_ioends(&ioend_list);
+	while (!list_empty(&ioend_list)) {
+		ioend = list_entry(ioend_list.next, struct iomap_ioend, io_list);
+		list_del_init(&ioend->io_list);
+		iomap_ioend_try_merge(ioend, &ioend_list);
+		ext4_iomap_finish_ioend(ioend);
+	}
+}
+
+void ext4_iomap_end_bio(struct bio *bio)
+{
+	struct iomap_ioend *ioend = iomap_ioend_from_bio(bio);
+	struct ext4_inode_info *ei = EXT4_I(ioend->io_inode);
+	unsigned long io_mode = (unsigned long)ioend->io_private;
+	unsigned long flags;
+
+	/*
+	 * This is a disksize-pending I/O: clear the disksize-pending
+	 * state set in ext4_block_zero_eof() and wake up all waiters
+	 * that will update the inode i_disksize.
+	 */
+	if (io_mode == EXT4_IOMAP_IOEND_DISKSIZE_GROW_IO)
+		ext4_iomap_clear_disksize_pending(ioend->io_inode);
+
+	spin_lock_irqsave(&ei->i_completed_io_lock, flags);
+	if (list_empty(&ei->i_rsv_conversion_list))
+		queue_work(EXT4_SB(ioend->io_inode->i_sb)->rsv_conversion_wq,
+			   &ei->i_rsv_conversion_work);
+	list_add_tail(&ioend->io_list, &ei->i_rsv_conversion_list);
+	spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
+}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index bca0dc87d0b7c..7db34ba0ea593 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -100,13 +100,21 @@ static const struct fs_parameter_spec ext4_param_specs[];
  * Lock ordering
  *
  * page fault path:
- * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
- *   -> page lock -> i_data_sem (rw)
+ * - buffer_head path:
+ *   mmap_lock -> sb_start_pagefault -> invalidate_lock (r) ->
+ *     transaction start -> folio lock -> i_data_sem (rw)
+ * - iomap path:
+ *   mmap_lock -> sb_start_pagefault -> invalidate_lock (r) ->
+ *     folio lock -> transaction start -> i_data_sem (rw)
  *
  * buffered write path:
- * sb_start_write -> i_mutex -> mmap_lock
- * sb_start_write -> i_mutex -> transaction start -> page lock ->
- *   i_data_sem (rw)
+ * sb_start_write -> i_rwsem (w) -> mmap_lock
+ * - buffer_head path:
+ *   sb_start_write -> i_rwsem (w) -> transaction start -> folio lock ->
+ *     i_data_sem (rw)
+ * - iomap path:
+ *   sb_start_write -> i_rwsem (w) -> transaction start -> i_data_sem (rw)
+ *   sb_start_write -> i_rwsem (w) -> folio lock (not under an active handle)
  *
  * truncate:
  * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
@@ -119,7 +127,10 @@ static const struct fs_parameter_spec ext4_param_specs[];
  * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
  *
  * writepages:
- * transaction start -> page lock(s) -> i_data_sem (rw)
+ * - buffer_head path:
+ *   transaction start -> folio lock(s) -> i_data_sem (rw)
+ * - iomap path:
+ *   folio lock -> transaction start -> i_data_sem (rw)
  */
 
 static const struct fs_context_operations ext4_context_ops = {
@@ -1486,6 +1497,14 @@ static void ext4_destroy_inode(struct inode *inode)
 			 "Inode %llu (%p): i_reserved_data_blocks (%u) not cleared!",
 			 inode->i_ino, EXT4_I(inode),
 			 EXT4_I(inode)->i_reserved_data_blocks);
+
+	if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ERROR_FS) &&
+	    !ext4_emergency_state(inode->i_sb) &&
+	    WARN_ON_ONCE(ext4_test_inode_state(inode,
+				EXT4_STATE_DISKSIZE_GROW_PENDING)))
+		ext4_msg(inode->i_sb, KERN_ERR,
+			 "Inode %llu (%p): EXT4_STATE_DISKSIZE_GROW_PENDING not cleared!",
+			 inode->i_ino, EXT4_I(inode));
 }
 
 static void ext4_shutdown(struct super_block *sb)
@@ -1728,6 +1747,7 @@ enum {
 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
 	Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
 	Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
+	Opt_buffered_iomap, Opt_nobuffered_iomap,
 	Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
 #ifdef CONFIG_EXT4_DEBUG
 	Opt_fc_debug_max_replay, Opt_fc_debug_force
@@ -1866,6 +1886,8 @@ static const struct fs_parameter_spec ext4_param_specs[] = {
 	fsparam_flag	("no_prefetch_block_bitmaps",
 						Opt_no_prefetch_block_bitmaps),
 	fsparam_s32	("mb_optimize_scan",	Opt_mb_optimize_scan),
+	fsparam_flag	("buffered_iomap",	Opt_buffered_iomap),
+	fsparam_flag	("nobuffered_iomap",	Opt_nobuffered_iomap),
 	fsparam_string	("check",		Opt_removed),	/* mount option from ext2/3 */
 	fsparam_flag	("nocheck",		Opt_removed),	/* mount option from ext2/3 */
 	fsparam_flag	("reservation",		Opt_removed),	/* mount option from ext2/3 */
@@ -1959,6 +1981,10 @@ static const struct mount_opts {
 	{Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
 	{Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
 	 MOPT_SET},
+	{Opt_buffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,
+	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
+	{Opt_nobuffered_iomap, EXT4_MOUNT2_BUFFERED_IOMAP,
+	 MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY},
 #ifdef CONFIG_EXT4_DEBUG
 	{Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
 	 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 7028a28316fa3..92048da9dae43 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -3157,6 +3157,215 @@ TRACE_EVENT(ext4_move_extent_exit,
 		  __entry->ret)
 );
 
+DECLARE_EVENT_CLASS(ext4_set_iomap_class,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 loff_t offset, loff_t length, unsigned int flags),
+	TP_ARGS(inode, map, offset, length, flags),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(ext4_lblk_t, m_lblk)
+		__field(ext4_fsblk_t, pblk)
+		__field(unsigned int, m_len)
+		__field(unsigned int, m_flags)
+		__field(u64, m_seq)
+		__field(loff_t, offset)
+		__field(loff_t, length)
+		__field(unsigned int, iomap_flags)
+	),
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->m_lblk		= map->m_lblk;
+		__entry->pblk		= map->m_pblk;
+		__entry->m_len		= map->m_len;
+		__entry->m_flags	= map->m_flags;
+		__entry->m_seq		= map->m_seq;
+		__entry->offset		= offset;
+		__entry->length		= length;
+		__entry->iomap_flags	= flags;
+
+	),
+	TP_printk("dev %d:%d ino %llu m_lblk %u pblk %llu m_len %u m_flags %s m_seq %llu orig_off 0x%llx orig_len 0x%llx iomap_flags 0x%x",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->m_lblk, __entry->pblk, __entry->m_len,
+		  show_mflags(__entry->m_flags), __entry->m_seq,
+		  __entry->offset, __entry->length, __entry->iomap_flags)
+)
+
+#define DEFINE_SET_IOMAP_EVENT(name) \
+DEFINE_EVENT(ext4_set_iomap_class, name, \
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map, \
+		 loff_t offset, loff_t length, unsigned int flags), \
+	TP_ARGS(inode, map, offset, length, flags))
+
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_read_begin);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_buffered_write_begin);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_map_writeback_range);
+DEFINE_SET_IOMAP_EVENT(ext4_iomap_zero_begin);
+
+DECLARE_EVENT_CLASS(ext4_iomap_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, i_disksize)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	),
+	TP_printk("dev %d:%d ino %llu i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->i_disksize)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_mark_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_clear_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+DEFINE_EVENT(ext4_iomap_disksize_pending, ext4_iomap_wait_disksize_pending,
+	TP_PROTO(struct inode *inode),
+	TP_ARGS(inode)
+);
+
+/* disksize pending I/O tracepoints for iomap Buffered I/O path */
+DECLARE_EVENT_CLASS(ext4_iomap_wb_disksize_pending,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, io_offset)
+		__field(size_t, io_size)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->io_offset = io_offset;
+		__entry->io_size = io_size;
+		__entry->i_size = i_size_read(inode);
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+	),
+	TP_printk("dev %d:%d ino %llu io_offset %lld io_size %zu i_size %lld i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->io_offset, __entry->io_size,
+		  __entry->i_size, __entry->i_disksize)
+);
+
+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,
+	ext4_iomap_wb_disksize_pending_submit,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size)
+);
+
+DEFINE_EVENT(ext4_iomap_wb_disksize_pending,
+	ext4_iomap_wb_disksize_pending_wait,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size),
+	TP_ARGS(inode, io_offset, io_size)
+);
+
+TRACE_EVENT(ext4_iomap_wb_disksize_pending_complete,
+	TP_PROTO(struct inode *inode, loff_t io_offset, size_t io_size,
+		 int ret),
+	TP_ARGS(inode, io_offset, io_size, ret),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, io_offset)
+		__field(size_t, io_size)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(int, ret)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->io_offset = io_offset;
+		__entry->io_size = io_size;
+		__entry->i_size = i_size_read(inode);
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+		__entry->ret = ret;
+	),
+	TP_printk("dev %d:%d ino %llu io_offset %lld io_size %zu ret %d i_size %lld i_disksize %lld",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->io_offset, __entry->io_size,
+		  __entry->ret, __entry->i_size, __entry->i_disksize)
+);
+
+/* i_disksize update tracepoint */
+TRACE_EVENT(ext4_iomap_wb_update_disksize,
+	TP_PROTO(struct inode *inode, loff_t end, loff_t i_size,
+		 loff_t i_disksize, loff_t new_disksize,
+		 bool is_disksize_grow),
+	TP_ARGS(inode, end, i_size, i_disksize, new_disksize,
+		is_disksize_grow),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, end)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(loff_t, new_disksize)
+		__field(bool, is_disksize_grow)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->end = end;
+		__entry->i_size = i_size;
+		__entry->i_disksize = i_disksize;
+		__entry->new_disksize = new_disksize;
+		__entry->is_disksize_grow = is_disksize_grow;
+	),
+	TP_printk("dev %d:%d ino %llu end %lld i_size %lld i_disksize %lld new_disksize %lld is_disksize_grow %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino, __entry->end, __entry->i_size,
+		  __entry->i_disksize, __entry->new_disksize,
+		  __entry->is_disksize_grow)
+);
+
+/* Block zero EOF tracepoint */
+TRACE_EVENT(ext4_block_zero_eof,
+	TP_PROTO(struct inode *inode, loff_t from, loff_t length,
+		 bool did_zero, bool zero_written),
+	TP_ARGS(inode, from, length, did_zero, zero_written),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(u64, ino)
+		__field(loff_t, from)
+		__field(loff_t, length)
+		__field(loff_t, i_size)
+		__field(loff_t, i_disksize)
+		__field(bool, did_zero)
+		__field(bool, zero_written)
+	),
+	TP_fast_assign(
+		__entry->dev = inode->i_sb->s_dev;
+		__entry->ino = inode->i_ino;
+		__entry->from = from;
+		__entry->length = length;
+		__entry->i_size = inode->i_size;
+		__entry->i_disksize = READ_ONCE(EXT4_I(inode)->i_disksize);
+		__entry->did_zero = did_zero;
+		__entry->zero_written = zero_written;
+	),
+	TP_printk("dev %d:%d ino %llu zero EOF from %lld length %lld i_size %lld i_disksize %lld did_zero %d zero_written %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev), __entry->ino,
+		  __entry->from, __entry->length, __entry->i_size,
+		  __entry->i_disksize, __entry->did_zero, __entry->zero_written)
+);
+
 #endif /* _TRACE_EXT4_H */
 
 /* This part must be outside protection */

15/3 2026/10/09 13:31 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 40190
							output: 34
							thoughts: 1568
						

Thoughts:
**KMSAN Evaluation: Ext4 Buffered I/O Implementation**

Okay, let's break this down. My task is to determine if this ext4 buffered I/O patch warrants a dedicated KMSAN fuzzing session. I've got the rules etched in my mind: KMSAN targets uninitialized memory reads and kernel-to-user info leaks; specifically, I need to look for code that *only* KMSAN would catch. KASAN, LOCKDEP, and UB-sanitizers cover the usual suspects like OOB accesses and UAFs, so I can ignore those for this initial analysis.

I need to focus on these scenarios where KMSAN would be relevant:
1.  Kernel structures sent to user space (copy_to_user, etc.)
2.  Conditional logic based on potentially uninitialized variables
3.  Complex data structures with uninitialized fields.
4.  Bounds checks that can lead to reading uninitialized memory.

And the flip side: NeedsKMSAN = false if:
1.  The issue is KASAN, LOCKDEP or other sanitizers catch the bug
2.  Data structures are fully zeroed or initialized.
3.  No uninitialized memory usage or info-leaks.

Now, let's dissect the patch. It seems to be adding buffered iomap support to ext4, which is a fairly significant change. My initial scan points to modifications across a bunch of files: `ext4.h`, `ext4_jbd2.c`, `ext4_jbd2.h`, `extents.c`, `file.c`, `ialloc.c`, and `inode.c`.

My primary concern is the potential for uninitialized memory usage during the iomap implementation. I'm focusing on data structures and data transfers. I'm particularly interested in anything that interacts with user-space. I see a lot of function names like `ext4_iomap_buffered_write()`, `ext4_iomap_map_blocks()` and related structures.

My immediate focus is on how `ext4_map_blocks()` and specifically `struct ext4_map_blocks` are used. I need to know if it's being initialized correctly *before* potentially being used to access memory. I noticed a `struct extent_status es;` being used inside this, but that doesn't concern me.

I dig into `ext4_iomap_map_blocks()`. It takes a `struct ext4_map_blocks *map` as input. Ah, there is a `struct ext4_map_blocks map;` in the caller.

Let me now check callers of `ext4_iomap_map_blocks()`:
1.  `ext4_iomap_buffered_read_begin`
    -   I can see that a `struct ext4_map_blocks map;` is being declared and passed to `ext4_iomap_map_blocks`. *Is it initialized?* **That's my main question at the moment.** I need to understand what's in `struct ext4_map_blocks` and check where it's zeroed/initialized before being passed.

I need to investigate the definition of `struct ext4_map_blocks` and look at all its usages. I will grep for that in ext4 to see what other data structures are involved. Then I will determine whether it's zero-initialized or initialized before any reads. If it's *not* properly initialized, and if it's used in a context that could leak uninitialized values to user space or be used in conditional logic that depends on the contents, then *this is a strong candidate for KMSAN attention*. Otherwise, I'm leaning toward NeedsKMSAN=false for now.



16/3 2026/10/09 13:32 tool grepper
0m Args:
{
  "Expression": "struct ext4_map_blocks \\{",
  "PathPrefixes": [
    "fs/ext4/"
  ]
}

Results:
{
  "Output": "fs/ext4/ext4.h=210=struct ext4_allocation_request {\n--\nfs/ext4/ext4.h-256-\nfs/ext4/ext4.h:257:struct ext4_map_blocks {\nfs/ext4/ext4.h-258-\text4_fsblk_t m_pblk;\n"
}

17/3 2026/10/09 13:32 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 42709
							output: 37
							thoughts: 22
						

Thoughts:
Okay, let's dive into this. I'm going to pull up `fs/ext4/ext4.h` and focus around line 257. I need to refresh my memory on what's defined there, and how it relates to the broader ext4 structure. My intuition suggests it's likely something fundamental, perhaps related to inode structures, block group descriptors, or the superblock – the core building blocks of the filesystem. Given my expertise, I'll be looking for nuances; corner cases where performance might be impacted, or areas where potential vulnerabilities could exist. I'll be paying close attention to data types, bitfields, and any potential alignment issues that might affect efficiency or, worse, lead to unexpected behavior. I'm going to start mentally tracing how the data structures declared there are used within the broader context. I'll be asking myself questions about the implications of the definitions for the filesystem's on-disk format and its in-memory representation. I'm essentially building a mental map of this section of the code, knowing how easily one thing can ripple throughout the system. I'm keen to evaluate the balance between performance, flexibility, and safety.



18/3 2026/10/09 13:32 tool read-file
0m Args:
{
  "File": "fs/ext4/ext4.h",
  "FirstLine": 256,
  "LineCount": 25
}

Results:
{
  "Contents": " 256:\t\n 257:\tstruct ext4_map_blocks {\n 258:\t\text4_fsblk_t m_pblk;\n 259:\t\text4_lblk_t m_lblk;\n 260:\t\tunsigned int m_len;\n 261:\t\tunsigned int m_flags;\n 262:\t\tu64 m_seq;\n 263:\t};\n 264:\t\n 265:\t/*\n 266:\t * Block validity checking, system zone rbtree.\n 267:\t */\n 268:\tstruct ext4_system_blocks {\n 269:\t\tstruct rb_root root;\n 270:\t\tstruct rcu_head rcu;\n 271:\t};\n 272:\t\n 273:\t/*\n 274:\t * Flags for ext4_io_end-\u003eflags\n 275:\t */\n 276:\t#define EXT4_IO_END_UNWRITTEN\t0x0001\n 277:\t#define EXT4_IO_END_FAILED\t0x0002\n 278:\t\n 279:\t#define EXT4_IO_END_DEFER_COMPLETION (EXT4_IO_END_UNWRITTEN | EXT4_IO_END_FAILED)\n 280:\t\n"
}

19/3 2026/10/09 13:32 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 43375
							output: 36
							thoughts: 831
						

Thoughts:
**Analyzing the Ext4 Block Mapping Process**

Okay, let's break this down. I'm looking at how the `struct ext4_map_blocks map` is used within the ext4 filesystem code, specifically focusing on read and write operations. My initial thought is to understand how the logical block numbers are translated into physical block numbers and what that `struct` is doing with all of this.

First, I see that the `ext4_iomap_buffered_read_begin` function and other similar functions use this `struct ext4_map_blocks map`.  It seems like a crucial data structure for block mapping. Now, digging into the definition of this structure: `m_pblk`, `m_lblk`, `m_len`, `m_flags`, and `m_seq`. I need to figure out how these fields get populated and used.

I'm looking at `ext4_iomap_map_blocks`. From this I see `map->m_lblk` and `map->m_len` are explicitly initialized by calculating the first and last logical blocks based on the offset and length of the I/O operation. That makes sense, calculating the extents being looked at.

But then, the alarm bell! I see something is not being initialized! My attention is immediately drawn to the fields `map->m_flags`, `map->m_pblk` and `map->m_seq`. Where are these populated? They are not directly set in `ext4_iomap_map_blocks()`, and are not obvious from the context, so I need to go deeper into `ext4_map_blocks()` itself and the functions called within this function. It appears that `ext4_map_blocks()` is the core function.
I'm guessing this function is where the actual mapping takes place and I need to inspect the `fs/ext4/inode.c` file!



20/3 2026/10/09 13:32 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 770,
  "LineCount": 80
}

Results:
{
  "Contents": " 770:\t * It returns the error in case of allocation failure.\n 771:\t */\n 772:\tint ext4_map_blocks(handle_t *handle, struct inode *inode,\n 773:\t\t\t    struct ext4_map_blocks *map, int flags)\n 774:\t{\n 775:\t\tstruct extent_status es;\n 776:\t\tint retval;\n 777:\t\tint ret = 0;\n 778:\t\tbool internal_handle = false;\n 779:\t\tunsigned int orig_mlen;\n 780:\t#ifdef ES_AGGRESSIVE_TEST\n 781:\t\tstruct ext4_map_blocks orig_map;\n 782:\t\n 783:\t\tmemcpy(\u0026orig_map, map, sizeof(*map));\n 784:\t#endif\n 785:\t\n 786:\t\tmap-\u003em_flags = 0;\n 787:\t\text_debug(inode, \"flag 0x%x, max_blocks %u, logical block %lu\\n\",\n 788:\t\t\t  flags, map-\u003em_len, (unsigned long) map-\u003em_lblk);\n 789:\t\n 790:\t\t/*\n 791:\t\t * ext4_map_blocks returns an int, and m_len is an unsigned int\n 792:\t\t */\n 793:\t\tif (unlikely(map-\u003em_len \u003e INT_MAX))\n 794:\t\t\tmap-\u003em_len = INT_MAX;\n 795:\t\torig_mlen = map-\u003em_len;\n 796:\t\n 797:\t\t/* We can handle the block number less than EXT_MAX_BLOCKS */\n 798:\t\tif (unlikely(map-\u003em_lblk \u003e= EXT_MAX_BLOCKS))\n 799:\t\t\treturn -EFSCORRUPTED;\n 800:\t\n 801:\t\t/*\n 802:\t\t * Callers from the context of data submission are the only exceptions\n 803:\t\t * for regular files that do not hold the i_rwsem or invalidate_lock.\n 804:\t\t * However, caching unrelated ranges is not permitted.\n 805:\t\t */\n 806:\t\tif (flags \u0026 EXT4_GET_BLOCKS_IO_SUBMIT)\n 807:\t\t\tWARN_ON_ONCE(!(flags \u0026 EXT4_EX_NOCACHE));\n 808:\t\telse\n 809:\t\t\text4_check_map_extents_env(inode);\n 810:\t\n 811:\tcreate_retry:\n 812:\t\t/* Lookup extent status tree firstly */\n 813:\t\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n 814:\t\t\tif (ext4_es_is_written(\u0026es) || ext4_es_is_unwritten(\u0026es)) {\n 815:\t\t\t\tmap-\u003em_pblk = ext4_es_pblock(\u0026es) +\n 816:\t\t\t\t\t\tmap-\u003em_lblk - es.es_lblk;\n 817:\t\t\t\tmap-\u003em_flags |= ext4_es_is_written(\u0026es) ?\n 818:\t\t\t\t\t\tEXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;\n 819:\t\t\t\tretval = es.es_len - (map-\u003em_lblk - es.es_lblk);\n 820:\t\t\t\tif (retval \u003e map-\u003em_len)\n 821:\t\t\t\t\tretval = map-\u003em_len;\n 822:\t\t\t\tmap-\u003em_len = retval;\n 823:\t\t\t} else if (ext4_es_is_delayed(\u0026es) || ext4_es_is_hole(\u0026es)) {\n 824:\t\t\t\tmap-\u003em_pblk = 0;\n 825:\t\t\t\tmap-\u003em_flags |= ext4_es_is_delayed(\u0026es) ?\n 826:\t\t\t\t\t\tEXT4_MAP_DELAYED : 0;\n 827:\t\t\t\tretval = es.es_len - (map-\u003em_lblk - es.es_lblk);\n 828:\t\t\t\tif (retval \u003e map-\u003em_len)\n 829:\t\t\t\t\tretval = map-\u003em_len;\n 830:\t\t\t\tmap-\u003em_len = retval;\n 831:\t\t\t\tretval = 0;\n 832:\t\t\t} else {\n 833:\t\t\t\tBUG();\n 834:\t\t\t}\n 835:\t\n 836:\t\t\tif (flags \u0026 EXT4_GET_BLOCKS_CACHED_NOWAIT)\n 837:\t\t\t\treturn retval;\n 838:\t#ifdef ES_AGGRESSIVE_TEST\n 839:\t\t\text4_map_blocks_es_recheck(handle, inode, map,\n 840:\t\t\t\t\t\t   \u0026orig_map, flags);\n 841:\t#endif\n 842:\t\t\tif (!(flags \u0026 EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) ||\n 843:\t\t\t\t\torig_mlen == map-\u003em_len)\n 844:\t\t\t\tgoto found;\n 845:\t\n 846:\t\t\tmap-\u003em_len = orig_mlen;\n 847:\t\t}\n 848:\t\t/*\n 849:\t\t * In the query cache no-wait mode, nothing we can do more if we\n"
}

21/3 2026/10/09 13:32 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 46025
							output: 37
							thoughts: 51
						

Thoughts:
Okay, here's my take on those thoughts, framed as my own:

**Analyzing Potential Block Mapping Issues in ext4**

Right, let's zero in on line 786: `map->m_flags = 0;`. Hmm, setting those mapping flags to zero... I wonder what the implications are if `ext4_da_map_blocks()` gets called right after that? That's the critical question here, isn't it? We need to be absolutely sure we understand the state of things when we enter `ext4_da_map_blocks()`. It's possible that setting flags to zero isn't a problem, depending on where this code lies in the grand scheme of things, but we need to know for certain. Best way to do this is to check the function definition. Let's dig into `ext4_da_map_blocks()` now. I'll need to review its behavior and how it interacts with the `m_flags` field to understand if there could be a bug.



22/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 2045,
  "LineCount": 80
}

Results:
{
  "Contents": "2045:\t\t\t  (unsigned long) map-\u003em_lblk);\n2046:\t\n2047:\t\text4_check_map_extents_env(inode);\n2048:\t\n2049:\t\t/* Lookup extent status tree firstly */\n2050:\t\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n2051:\t\t\tmap-\u003em_len = min_t(unsigned int, map-\u003em_len,\n2052:\t\t\t\t\t   es.es_len - (map-\u003em_lblk - es.es_lblk));\n2053:\t\n2054:\t\t\tif (ext4_es_is_hole(\u0026es))\n2055:\t\t\t\tgoto add_delayed;\n2056:\t\n2057:\tfound:\n2058:\t\t\t/*\n2059:\t\t\t * Delayed extent could be allocated by fallocate.\n2060:\t\t\t * So we need to check it.\n2061:\t\t\t */\n2062:\t\t\tif (ext4_es_is_delayed(\u0026es)) {\n2063:\t\t\t\tmap-\u003em_flags |= EXT4_MAP_DELAYED;\n2064:\t\t\t\treturn 0;\n2065:\t\t\t}\n2066:\t\n2067:\t\t\tmap-\u003em_pblk = ext4_es_pblock(\u0026es) + map-\u003em_lblk - es.es_lblk;\n2068:\t\t\tif (ext4_es_is_written(\u0026es))\n2069:\t\t\t\tmap-\u003em_flags |= EXT4_MAP_MAPPED;\n2070:\t\t\telse if (ext4_es_is_unwritten(\u0026es))\n2071:\t\t\t\tmap-\u003em_flags |= EXT4_MAP_UNWRITTEN;\n2072:\t\t\telse\n2073:\t\t\t\tBUG();\n2074:\t\n2075:\t#ifdef ES_AGGRESSIVE_TEST\n2076:\t\t\text4_map_blocks_es_recheck(NULL, inode, map, \u0026orig_map, 0);\n2077:\t#endif\n2078:\t\t\treturn 0;\n2079:\t\t}\n2080:\t\n2081:\t\t/*\n2082:\t\t * Try to see if we can get the block without requesting a new\n2083:\t\t * file system block.\n2084:\t\t */\n2085:\t\tdown_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2086:\t\tif (ext4_has_inline_data(inode))\n2087:\t\t\tretval = 0;\n2088:\t\telse\n2089:\t\t\tretval = ext4_map_query_blocks(NULL, inode, map, 0);\n2090:\t\tup_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2091:\t\tif (retval)\n2092:\t\t\treturn retval \u003c 0 ? retval : 0;\n2093:\t\n2094:\tadd_delayed:\n2095:\t\tdown_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2096:\t\t/*\n2097:\t\t * Page fault path (ext4_page_mkwrite does not take i_rwsem)\n2098:\t\t * and fallocate path (no folio lock) can race. Make sure we\n2099:\t\t * lookup the extent status tree here again while i_data_sem\n2100:\t\t * is held in write mode, before inserting a new da entry in\n2101:\t\t * the extent status tree.\n2102:\t\t */\n2103:\t\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n2104:\t\t\tmap-\u003em_len = min_t(unsigned int, map-\u003em_len,\n2105:\t\t\t\t\t   es.es_len - (map-\u003em_lblk - es.es_lblk));\n2106:\t\n2107:\t\t\tif (!ext4_es_is_hole(\u0026es)) {\n2108:\t\t\t\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2109:\t\t\t\tgoto found;\n2110:\t\t\t}\n2111:\t\t} else if (!ext4_has_inline_data(inode)) {\n2112:\t\t\tretval = ext4_map_query_blocks(NULL, inode, map, 0);\n2113:\t\t\tif (retval) {\n2114:\t\t\t\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2115:\t\t\t\treturn retval \u003c 0 ? retval : 0;\n2116:\t\t\t}\n2117:\t\t}\n2118:\t\n2119:\t\tmap-\u003em_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;\n2120:\t\tretval = ext4_insert_delayed_blocks(inode, map-\u003em_lblk, map-\u003em_len);\n2121:\t\tif (!retval)\n2122:\t\t\tmap-\u003em_seq = READ_ONCE(EXT4_I(inode)-\u003ei_es_seq);\n2123:\t\tup_write(\u0026EXT4_I(inode)-\u003ei_data_sem);\n2124:\t\n"
}

23/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 47694
							output: 37
							thoughts: 166
						

Thoughts:
**Analyzing `ext4_da_map_blocks` Flag Management**

Okay, I need to get a handle on how these flags are being set within `ext4_da_map_blocks`. Line 2063 grabs my attention: `map->m_flags |= EXT4_MAP_DELAYED;` followed by line 2069, `map->m_flags |= EXT4_MAP_MAPPED;`. Then there is line 2119: `map->m_flags |= EXT4_MAP_DELAYED | EXT4_MAP_NEW;`. Right, the bitwise OR assignment `|=`. But, hold on a second! Is `ext4_da_map_blocks` *initializing* `map->m_flags` or is it *adding* to an existing bitmask? I need to clarify that.

Let me go back to the top of `ext4_da_map_blocks`, because I need to check how the struct `map` is actually initialized, starting from line 2045. It's best to look at what comes before that line to understand the initial state. Let's see...



24/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 2030,
  "LineCount": 25
}

Results:
{
  "Contents": "2030:\t * delalloc extent entry if it still couldn't find any extent. Pass out\n2031:\t * the mapped extent through @map and return 0 on success.\n2032:\t */\n2033:\tstatic int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)\n2034:\t{\n2035:\t\tstruct extent_status es;\n2036:\t\tint retval;\n2037:\t#ifdef ES_AGGRESSIVE_TEST\n2038:\t\tstruct ext4_map_blocks orig_map;\n2039:\t\n2040:\t\tmemcpy(\u0026orig_map, map, sizeof(*map));\n2041:\t#endif\n2042:\t\n2043:\t\tmap-\u003em_flags = 0;\n2044:\t\text_debug(inode, \"max_blocks %u, logical block %lu\\n\", map-\u003em_len,\n2045:\t\t\t  (unsigned long) map-\u003em_lblk);\n2046:\t\n2047:\t\text4_check_map_extents_env(inode);\n2048:\t\n2049:\t\t/* Lookup extent status tree firstly */\n2050:\t\tif (ext4_es_lookup_extent(inode, map-\u003em_lblk, NULL, \u0026es, \u0026map-\u003em_seq)) {\n2051:\t\t\tmap-\u003em_len = min_t(unsigned int, map-\u003em_len,\n2052:\t\t\t\t\t   es.es_len - (map-\u003em_lblk - es.es_lblk));\n2053:\t\n2054:\t\t\tif (ext4_es_is_hole(\u0026es))\n"
}

25/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 48591
							output: 36
							thoughts: 160
						

Thoughts:
**My Initial Thoughts on ext4_map_blocks**

Okay, so I'm looking at `ext4_map_blocks`, and I see a crucial initialization on line 2043: `map->m_flags = 0;`. This initialization also occurs in `ext4_da_map_blocks`. That’s good; we're starting with a clean slate for those flags. However, what about `map->m_seq`? Is that also being initialized or cleared somewhere, or is its state being managed correctly elsewhere? I need to keep that in mind.

Now, focusing on `ext4_map_blocks` again. I see a call to `ext4_es_lookup_extent` on line 813: `if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq))`. Hold on a minute... what happens if `ext4_es_lookup_extent` *fails*? What if it returns false, indicating the extent isn't found in the extent status tree? I need to understand the handling of that scenario. Let me quickly jump to lines 848 and onward of `ext4_map_blocks` to see what is done in that case. I need to be sure the control flow is handled correctly when the extent lookup fails.



26/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 848,
  "LineCount": 40
}

Results:
{
  "Contents": " 848:\t\t/*\n 849:\t\t * In the query cache no-wait mode, nothing we can do more if we\n 850:\t\t * cannot find extent in the cache.\n 851:\t\t */\n 852:\t\tif (flags \u0026 EXT4_GET_BLOCKS_CACHED_NOWAIT)\n 853:\t\t\treturn 0;\n 854:\t\n 855:\t\t/*\n 856:\t\t * Try to see if we can get the block without requesting a new\n 857:\t\t * file system block.\n 858:\t\t */\n 859:\t\tdown_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n 860:\t\tretval = ext4_map_query_blocks(handle, inode, map, flags);\n 861:\t\tup_read((\u0026EXT4_I(inode)-\u003ei_data_sem));\n 862:\t\tif (retval \u003c 0)\n 863:\t\t\tgoto out;\n 864:\t\n 865:\tfound:\n 866:\t\tif (retval \u003e 0 \u0026\u0026 map-\u003em_flags \u0026 EXT4_MAP_MAPPED) {\n 867:\t\t\tret = check_block_validity(inode, map);\n 868:\t\t\tif (ret != 0)\n 869:\t\t\t\tgoto out;\n 870:\t\t}\n 871:\t\n 872:\t\t/* If it is only a block(s) look up */\n 873:\t\tif ((flags \u0026 EXT4_GET_BLOCKS_CREATE) == 0)\n 874:\t\t\treturn retval;\n 875:\t\n 876:\t\t/*\n 877:\t\t * Returns if the blocks have already allocated\n 878:\t\t *\n 879:\t\t * Note that if blocks have been preallocated\n 880:\t\t * ext4_ext_map_blocks() returns with buffer head unmapped\n 881:\t\t */\n 882:\t\tif (retval \u003e 0) {\n 883:\t\t\t/*\n 884:\t\t\t * If we need to convert written extent to unwritten or\n 885:\t\t\t * convert unwritten extent to written, continue and do\n 886:\t\t\t * the actual work in ext4_ext_map_blocks().\n 887:\t\t\t */\n"
}

27/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 49675
							output: 34
							thoughts: 173
						

Thoughts:
**Analyzing Potential Data Loss in Ext4 Block Mapping**

Okay, let's dive into this Ext4 code. I need to understand this potential data access vulnerability. Line 873-874 immediately grabs my attention:

`if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)`
`    return retval;`

Right, the function returns if `flags` *doesn't* include `EXT4_GET_BLOCKS_CREATE`. My immediate thought is, like in `ext4_iomap_buffered_read_begin`, we *don't* pass `EXT4_GET_BLOCKS_CREATE` when calling `ext4_iomap_map_blocks`. We call it with a zero flag, resulting in a call to `ext4_map_query_blocks`.

Hold on, what happens if `ext4_es_lookup_extent` fails? Does `ext4_map_query_blocks` get called then? Is the `map->m_seq` initialized in that case? I need to be sure; if the extent lookup fails and `ext4_map_query_blocks` isn't setting it, then we could have an issue. Time to dig into the `ext4_map_query_blocks` function and confirm whether it properly initializes the mapping sequence. I need to verify that this potential gap in the mapping process is properly handled.



28/3 2026/10/09 13:33 tool grepper
0m Args:
{
  "Expression": "ext4_map_query_blocks",
  "PathPrefixes": [
    "fs/ext4/"
  ]
}

Results:
{
  "Output": "fs/ext4/ext4.h=210=struct ext4_allocation_request {\n--\nfs/ext4/ext4.h-243-/*\nfs/ext4/ext4.h:244: * This is for use in ext4_map_query_blocks() for a special case where we can\nfs/ext4/ext4.h-245- * have a physically and logically contiguous blocks split across two leaf\n--\nfs/ext4/ext4.h-247- * to know whether the returned extent is last in leaf. If yes, then lookup for\nfs/ext4/ext4.h:248: * next in leaf block in ext4_map_query_blocks_next_in_leaf().\nfs/ext4/ext4.h-249- * - This is never going to be added to any buffer head state.\n--\nfs/ext4/ext4.h=684=enum {\n--\nfs/ext4/ext4.h-752-/*\nfs/ext4/ext4.h:753: * ext4_map_query_blocks() uses this filter mask to filter the flags needed to\nfs/ext4/ext4.h-754- * pass while lookup/querying of on disk extent tree.\n--\nfs/ext4/ext4.h=3980=extern int ext4_map_blocks(handle_t *handle, struct inode *inode,\nfs/ext4/ext4.h-3981-\t\t\t   struct ext4_map_blocks *map, int flags);\nfs/ext4/ext4.h:3982:extern int ext4_map_query_blocks(handle_t *handle, struct inode *inode,\nfs/ext4/ext4.h-3983-\t\t\t\t  struct ext4_map_blocks *map, int flags);\n--\nfs/ext4/extents-test.c=353=static int check_buffer(char *buf, int c, int size)\n--\nfs/ext4/extents-test.c-367-/*\nfs/ext4/extents-test.c:368: * Simulate a map block call by first calling ext4_map_query_blocks() to\nfs/ext4/extents-test.c-369- * correctly populate map flags and pblk and then call the\n--\nfs/ext4/extents-test.c=374=static void ext4_map_create_blocks_helper(struct kunit *test,\n--\nfs/ext4/extents-test.c-380-\nfs/ext4/extents-test.c:381:\tretval = ext4_map_query_blocks(NULL, inode, map, flags);\nfs/ext4/extents-test.c-382-\tif (retval \u003c 0) {\nfs/ext4/extents-test.c-383-\t\tKUNIT_FAIL(test,\nfs/ext4/extents-test.c:384:\t\t\t   \"ext4_map_query_blocks() failed. Cannot proceed\\n\");\nfs/ext4/extents-test.c-385-\t\treturn;\n--\nfs/ext4/extents.c=6465=EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_issue_zeroout);\nfs/ext4/extents.c:6466:EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_map_query_blocks);\nfs/ext4/extents.c-6467-#endif\n--\nfs/ext4/inode.c=528=static void ext4_map_blocks_es_recheck(handle_t *handle,\n--\nfs/ext4/inode.c-569-\nfs/ext4/inode.c:570:static int ext4_map_query_blocks_next_in_leaf(handle_t *handle,\nfs/ext4/inode.c-571-\t\t\tstruct inode *inode, struct ext4_map_blocks *map,\n--\nfs/ext4/inode.c-624-\nfs/ext4/inode.c:625:int ext4_map_query_blocks(handle_t *handle, struct inode *inode,\nfs/ext4/inode.c-626-\t\t\t  struct ext4_map_blocks *map, int flags)\n--\nfs/ext4/inode.c-663-\t} else {\nfs/ext4/inode.c:664:\t\tretval = ext4_map_query_blocks_next_in_leaf(handle, inode, map,\nfs/ext4/inode.c-665-\t\t\t\t\t\t\t    orig_mlen);\n--\nfs/ext4/inode.c=772=int ext4_map_blocks(handle_t *handle, struct inode *inode,\n--\nfs/ext4/inode.c-859-\tdown_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\nfs/ext4/inode.c:860:\tretval = ext4_map_query_blocks(handle, inode, map, flags);\nfs/ext4/inode.c-861-\tup_read((\u0026EXT4_I(inode)-\u003ei_data_sem));\n--\nfs/ext4/inode.c=2033=static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)\n--\nfs/ext4/inode.c-2088-\telse\nfs/ext4/inode.c:2089:\t\tretval = ext4_map_query_blocks(NULL, inode, map, 0);\nfs/ext4/inode.c-2090-\tup_read(\u0026EXT4_I(inode)-\u003ei_data_sem);\n--\nfs/ext4/inode.c-2111-\t} else if (!ext4_has_inline_data(inode)) {\nfs/ext4/inode.c:2112:\t\tretval = ext4_map_query_blocks(NULL, inode, map, 0);\nfs/ext4/inode.c-2113-\t\tif (retval) {\n"
}

29/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 51689
							output: 36
							thoughts: 31
						

Thoughts:
Okay, here's my interpretation of that thought process, assuming I'm already deep in ext4 internals.

**Diving into ext4 Block Mapping**

Right, let's get down to brass tacks. I need to understand how ext4 actually figures out where a file's data is located on disk. That means I need to look at how block mapping is handled. I'm going to jump straight into the code, and I know exactly where to find the heart of this logic: `ext4_map_query_blocks`. I'm going to look at that in the `fs/ext4/inode.c` file, starting at line 625. I've got a specific question I want to answer and this is the best place to find it.



30/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 624,
  "LineCount": 45
}

Results:
{
  "Contents": " 624:\t\n 625:\tint ext4_map_query_blocks(handle_t *handle, struct inode *inode,\n 626:\t\t\t\t  struct ext4_map_blocks *map, int flags)\n 627:\t{\n 628:\t\tunsigned int status;\n 629:\t\tint retval;\n 630:\t\tunsigned int orig_mlen = map-\u003em_len;\n 631:\t\n 632:\t\tflags \u0026= EXT4_EX_QUERY_FILTER;\n 633:\t\tif (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))\n 634:\t\t\tretval = ext4_ext_map_blocks(handle, inode, map, flags);\n 635:\t\telse\n 636:\t\t\tretval = ext4_ind_map_blocks(handle, inode, map, flags);\n 637:\t\tif (retval \u003c 0)\n 638:\t\t\treturn retval;\n 639:\t\n 640:\t\t/* A hole? */\n 641:\t\tif (retval == 0)\n 642:\t\t\tgoto out;\n 643:\t\n 644:\t\tif (unlikely(retval != map-\u003em_len)) {\n 645:\t\t\text4_warning(inode-\u003ei_sb,\n 646:\t\t\t\t     \"ES len assertion failed for inode \"\n 647:\t\t\t\t     \"%llu: retval %d != map-\u003em_len %d\",\n 648:\t\t\t\t     inode-\u003ei_ino, retval, map-\u003em_len);\n 649:\t\t\tWARN_ON(1);\n 650:\t\t}\n 651:\t\n 652:\t\t/*\n 653:\t\t * No need to query next in leaf:\n 654:\t\t * - if returned extent is not last in leaf or\n 655:\t\t * - if the last in leaf is the full requested range\n 656:\t\t */\n 657:\t\tif (!(map-\u003em_flags \u0026 EXT4_MAP_QUERY_LAST_IN_LEAF) ||\n 658:\t\t\t\tmap-\u003em_len == orig_mlen) {\n 659:\t\t\tstatus = map-\u003em_flags \u0026 EXT4_MAP_UNWRITTEN ?\n 660:\t\t\t\t\tEXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;\n 661:\t\t\text4_es_cache_extent(inode, map-\u003em_lblk, map-\u003em_len,\n 662:\t\t\t\t\t     map-\u003em_pblk, status);\n 663:\t\t} else {\n 664:\t\t\tretval = ext4_map_query_blocks_next_in_leaf(handle, inode, map,\n 665:\t\t\t\t\t\t\t\t    orig_mlen);\n 666:\t\t}\n 667:\tout:\n 668:\t\tmap-\u003em_seq = READ_ONCE(EXT4_I(inode)-\u003ei_es_seq);\n"
}

31/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 52675
							output: 37
							thoughts: 177
						

Thoughts:
**Analyzing the ext4 Block Mapping Process**

Okay, so I'm digging into the block mapping within the ext4 filesystem, specifically in the context of `ext4_map_query_blocks()`. I've noticed a crucial data flow. The key is how the sequence number, `m_seq`, is handled. I see that `map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);` grabs the current sequence from the inode's extent status. Now, if `ext4_es_lookup_extent` finds a relevant extent, *it* is responsible for setting `&map->m_seq`. This is the core interaction.

I need to understand how that sequence number gets updated and utilized. The next logical step is to examine `ext4_set_iomap`. Let's refresh our memory of the function:
```c
static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
			   struct ext4_map_blocks *map, loff_t offset,
			   loff_t length, unsigned int flags)
```
I'm sure it's located in `fs/ext4/inode.c`, so I'll go check it out. I'm keen to understand how `ext4_set_iomap` interacts with the map structure and how it factors into the overall block allocation and mapping scheme. I need to see how the sequence number comes into play in this process.



32/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 3600,
  "LineCount": 55
}

Results:
{
  "Contents": "3600:\t\n3601:\tstatic bool ext4_iomap_valid(struct inode *inode, const struct iomap *iomap)\n3602:\t{\n3603:\t\treturn iomap-\u003evalidity_cookie == READ_ONCE(EXT4_I(inode)-\u003ei_es_seq);\n3604:\t}\n3605:\t\n3606:\tconst struct iomap_write_ops ext4_iomap_write_ops = {\n3607:\t\t.iomap_valid = ext4_iomap_valid,\n3608:\t};\n3609:\t\n3610:\tstatic void ext4_set_iomap(struct inode *inode, struct iomap *iomap,\n3611:\t\t\t\t   struct ext4_map_blocks *map, loff_t offset,\n3612:\t\t\t\t   loff_t length, unsigned int flags)\n3613:\t{\n3614:\t\tu8 blkbits = inode-\u003ei_blkbits;\n3615:\t\n3616:\t\t/*\n3617:\t\t * Writes that span EOF might trigger an I/O size update on completion,\n3618:\t\t * so consider them to be dirty for the purpose of O_DSYNC, even if\n3619:\t\t * there is no other metadata changes being made or are pending.\n3620:\t\t */\n3621:\t\tiomap-\u003eflags = 0;\n3622:\t\tif (ext4_inode_datasync_dirty(inode) ||\n3623:\t\t    offset + length \u003e i_size_read(inode))\n3624:\t\t\tiomap-\u003eflags |= IOMAP_F_DIRTY;\n3625:\t\n3626:\t\tif (map-\u003em_flags \u0026 EXT4_MAP_NEW)\n3627:\t\t\tiomap-\u003eflags |= IOMAP_F_NEW;\n3628:\t\n3629:\t\t/* HW-offload atomics are always used */\n3630:\t\tif (flags \u0026 IOMAP_ATOMIC)\n3631:\t\t\tiomap-\u003eflags |= IOMAP_F_ATOMIC_BIO;\n3632:\t\n3633:\t\tif (flags \u0026 IOMAP_DAX)\n3634:\t\t\tiomap-\u003edax_dev = EXT4_SB(inode-\u003ei_sb)-\u003es_daxdev;\n3635:\t\telse\n3636:\t\t\tiomap-\u003ebdev = inode-\u003ei_sb-\u003es_bdev;\n3637:\t\tiomap-\u003eoffset = EXT4_LBLK_TO_B(inode, map-\u003em_lblk);\n3638:\t\tiomap-\u003elength = EXT4_LBLK_TO_B(inode, map-\u003em_len);\n3639:\t\n3640:\t\tif ((map-\u003em_flags \u0026 EXT4_MAP_MAPPED) \u0026\u0026\n3641:\t\t    !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))\n3642:\t\t\tiomap-\u003eflags |= IOMAP_F_MERGED;\n3643:\t\n3644:\t\tiomap-\u003evalidity_cookie = map-\u003em_seq;\n3645:\t\n3646:\t\t/*\n3647:\t\t * Flags passed to ext4_map_blocks() for direct I/O writes can result\n3648:\t\t * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits\n3649:\t\t * set. In order for any allocated unwritten extents to be converted\n3650:\t\t * into written extents correctly within the -\u003eend_io() handler, we\n3651:\t\t * need to ensure that the iomap-\u003etype is set appropriately. Hence, the\n3652:\t\t * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has\n3653:\t\t * been set first.\n3654:\t\t */\n"
}

33/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 54242
							output: 37
							thoughts: 28
						

Thoughts:
Okay, here's my interpretation of that thought process, assuming I'm already quite familiar with the ext4 filesystem and the `ext4_set_iomap` function:

**Diving into `ext4_set_iomap` (lines 3655-3680)**

Alright, so the discussion has moved on to `ext4_set_iomap`, and I want to get a precise handle on what's happening in that particular segment, specifically lines 3655-3680. Given that this is a core function for manipulating I/O mappings within ext4, I immediately start mentally parsing that range. I'm focusing on those specific lines because, knowing ext4's architecture, I expect to find critical logic around I/O remapping, extent management, or possibly inode updates related to block allocation or deallocation within this scope. I need to refresh my understanding of how I/O is set. Is this related to direct I/O, buffered I/O, or something else? I will carefully consider the variables in play - what structures are being accessed, what locking mechanisms are being used, and what system calls might be involved. My primary goal here is to identify potential bottlenecks, subtle inefficiencies, or, alternatively, any elegant optimizations that are occurring in this crucial area of the filesystem code. I am probably looking at this through the lens of performance implications. The structure and variables will likely hint to me what part of the flow I am looking at.



34/3 2026/10/09 13:33 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 3655,
  "LineCount": 35
}

Results:
{
  "Contents": "3655:\t\tif (map-\u003em_flags \u0026 EXT4_MAP_UNWRITTEN) {\n3656:\t\t\tiomap-\u003etype = IOMAP_UNWRITTEN;\n3657:\t\t\tiomap-\u003eaddr = (u64) map-\u003em_pblk \u003c\u003c blkbits;\n3658:\t\t\tif (flags \u0026 IOMAP_DAX)\n3659:\t\t\t\tiomap-\u003eaddr += EXT4_SB(inode-\u003ei_sb)-\u003es_dax_part_off;\n3660:\t\t} else if (map-\u003em_flags \u0026 EXT4_MAP_MAPPED) {\n3661:\t\t\tiomap-\u003etype = IOMAP_MAPPED;\n3662:\t\t\tiomap-\u003eaddr = (u64) map-\u003em_pblk \u003c\u003c blkbits;\n3663:\t\t\tif (flags \u0026 IOMAP_DAX)\n3664:\t\t\t\tiomap-\u003eaddr += EXT4_SB(inode-\u003ei_sb)-\u003es_dax_part_off;\n3665:\t\t} else if (map-\u003em_flags \u0026 EXT4_MAP_DELAYED) {\n3666:\t\t\tiomap-\u003etype = IOMAP_DELALLOC;\n3667:\t\t\tiomap-\u003eaddr = IOMAP_NULL_ADDR;\n3668:\t\t} else {\n3669:\t\t\tiomap-\u003etype = IOMAP_HOLE;\n3670:\t\t\tiomap-\u003eaddr = IOMAP_NULL_ADDR;\n3671:\t\t}\n3672:\t}\n3673:\t\n3674:\tstatic int ext4_map_blocks_atomic_write_slow(handle_t *handle,\n3675:\t\t\t\tstruct inode *inode, struct ext4_map_blocks *map)\n3676:\t{\n3677:\t\text4_lblk_t m_lblk = map-\u003em_lblk;\n3678:\t\tunsigned int m_len = map-\u003em_len;\n3679:\t\tunsigned int mapped_len = 0, m_flags = 0;\n3680:\t\text4_fsblk_t next_pblk = 0;\n3681:\t\tbool check_next_pblk = false;\n3682:\t\tint ret = 0;\n3683:\t\n3684:\t\tWARN_ON_ONCE(!ext4_has_feature_bigalloc(inode-\u003ei_sb));\n3685:\t\n3686:\t\t/*\n3687:\t\t * This is a slow path in case of mixed mapping. We use\n3688:\t\t * EXT4_GET_BLOCKS_CREATE_ZERO flag here to make sure we get a single\n3689:\t\t * contiguous mapped mapping. This will ensure any unwritten or hole\n"
}

35/3 2026/10/09 13:33 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 55331
							output: 23
							thoughts: 705
						

Thoughts:
**Analyzing `ext4_iomap_zero_begin`**

Okay, let's break down this `ext4_iomap_zero_begin` function. The goal here is to handle the start of a zeroing operation within the ext4 filesystem, specifically using I/O mapping. First, I check for any emergency state on the superblock, which is standard practice for maintaining file system integrity. Next, I do a quick sanity check to make sure the `IOMAP_ZERO` flag is actually set, because otherwise, this function shouldn't be called.

Then, the critical part: I'm calling `ext4_iomap_map_blocks`. This is where the magic happens; this maps logical blocks to physical blocks on the disk. This is a common operation in file systems that handles fragmentation and mapping data to physical blocks and is also responsible for the main mapping and extent lookups.

Now, things get interesting. I see a check for `EXT4_MAP_UNWRITTEN`. This means I'm dealing with unwritten extents, which is an important concept in modern file systems that allows for efficient allocation.

Within the unwritten extent handling, I find `iomap_fill_dirty_folios`. *That's* the key. I need to understand what this does. The comment mentions dirty folios. This sounds like an optimization to zero out the unwritten mappings in the page cache when we have dirty folios, which provides an alternative path to extent conversion and cleaning the pagecache. I bet this is crucial to efficiently handle the zeroing, especially when dealing with unwritten extents. This prevents a flush iomap from having to trigger the extent conversion.

There's also a recheck of the mapping after `iomap_fill_dirty_folios`. This is clever. It's handling a potential race condition where a concurrent writeback might clean the folio and convert the unwritten extent to a written extent. So, if the extent is converted, I'll go back and remap.

Finally, I'm setting the `iomap` struct with the information from the mapping and any relevant flags.

So, *next* I need to investigate `iomap_fill_dirty_folios`. It seems like a vital piece of the puzzle to understand the zeroing and extent conversion process.



36/3 2026/10/09 13:34 tool grepper
0m Args:
{
  "Expression": "iomap_fill_dirty_folios"
}

Results:
{
  "Output": "fs/ext4/inode.c=4205=static int ext4_iomap_zero_begin(struct inode *inode,\n--\nfs/ext4/inode.c-4237-\nfs/ext4/inode.c:4238:\t\tcount = iomap_fill_dirty_folios(iter, \u0026start, end,\nfs/ext4/inode.c-4239-\t\t\t\t\t\t\u0026iomap_flags);\n--\nfs/ext4/inode.c-4246-\t\t * Recheck the mapping after a folio lock round in\nfs/ext4/inode.c:4247:\t\t * iomap_fill_dirty_folios().\nfs/ext4/inode.c-4248-\t\t */\n--\nfs/iomap/buffered-io.c=1648=static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero,\n--\nfs/iomap/buffered-io.c-1703-/**\nfs/iomap/buffered-io.c:1704: * iomap_fill_dirty_folios - fill a folio batch with dirty folios\nfs/iomap/buffered-io.c-1705- * @iter: Iteration structure\n--\nfs/iomap/buffered-io.c=1715=unsigned int\nfs/iomap/buffered-io.c:1716:iomap_fill_dirty_folios(\nfs/iomap/buffered-io.c-1717-\tstruct iomap_iter\t*iter,\n--\nfs/iomap/buffered-io.c-1736-}\nfs/iomap/buffered-io.c:1737:EXPORT_SYMBOL_GPL(iomap_fill_dirty_folios);\nfs/iomap/buffered-io.c-1738-\n--\nfs/xfs/xfs_iomap.c=1783=xfs_buffered_write_iomap_begin(\n--\nfs/xfs/xfs_iomap.c-1908-\t\tend = XFS_FSB_TO_B(mp, imap.br_startoff + imap.br_blockcount);\nfs/xfs/xfs_iomap.c:1909:\t\tfbatch_count = iomap_fill_dirty_folios(iter, \u0026start, end,\nfs/xfs/xfs_iomap.c-1910-\t\t\t\t\t\t       \u0026iomap_flags);\n--\nfs/xfs/xfs_iomap.c-1954-\t\t\t       min(count, XFS_FSB_TO_B(mp, imap.br_blockcount));\nfs/xfs/xfs_iomap.c:1955:\t\t\tiomap_fill_dirty_folios(iter, \u0026foffset, fend,\nfs/xfs/xfs_iomap.c-1956-\t\t\t\t\t\t\u0026iomap_flags);\n--\ninclude/linux/iomap.h=26=struct vm_fault;\n--\ninclude/linux/iomap.h-111- * IOMAP_F_FOLIO_BATCH indicates that the folio batch mechanism is active\ninclude/linux/iomap.h:112: * for this operation, set by iomap_fill_dirty_folios().\ninclude/linux/iomap.h-113- *\n--\ninclude/linux/iomap.h=447=int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,\n--\ninclude/linux/iomap.h-449-\t\tconst struct iomap_write_ops *write_ops);\ninclude/linux/iomap.h:450:unsigned int iomap_fill_dirty_folios(struct iomap_iter *iter, loff_t *start,\ninclude/linux/iomap.h-451-\t\tloff_t end, unsigned int *iomap_flags);\n"
}

37/3 2026/10/09 13:34 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 57559
							output: 37
							thoughts: 504
						

Thoughts:
**Analyzing Potential Uninitialized Memory in ext4 Trace Events**

Okay, let's dive in. I'm taking a look at the trace events defined within `include/trace/events/ext4.h` to see if there are any obvious areas where uninitialized memory might be an issue. My immediate focus is on the data these events are capturing.

Looking at the event definitions, I see:

*   `ext4_set_iomap_class`: This one takes an `inode` pointer, a `map` struct (likely containing block mapping information), offset, length, and flags. No immediate red flags here; the `map` struct contents warrant further inspection, but the arguments themselves don't scream "uninitialized".
*   `ext4_iomap_disksize_pending`: This focuses on just the `inode`. Safe as is.
*   `ext4_iomap_wb_disksize_pending`, `ext4_iomap_wb_disksize_pending_complete`: Both use `inode`, offset, and size. Seems safe.
*   `ext4_iomap_wb_update_disksize`: Takes an `inode`, end, size, disksize, new\_disksize and a boolean. Seems safe.
*   `ext4_block_zero_eof`: This looks interesting. Takes `inode`, from, length, `did_zero`, and `zero_written`. Now, `did_zero` and `zero_written` are *booleans*, and that catches my attention. Are these flags accurately reflecting the zeroing process? Could a false positive or negative here be significant? Time to dig deeper.

Hang on... I'm noticing `ext4_block_zero_range` call `ext4_block_zero_eof`! Looking at `ext4_block_zero_range` there's conditional logic in that function. Specifically, let's examine:

```c
	} else if (ext4_inode_buffered_iomap(inode)) {
		return ext4_block_iomap_zero_range(inode, from, length,
						   did_zero, zero_written);
	}
```

Wait, `ext4_block_zero_eof` is also directly used here! I need to trace this back to the `inode.c` file! Specifically let's look at lines 5020-5060 in `fs/ext4/inode.c`, I'll read through it in detail. Understanding how those booleans are populated and used within `ext4_block_zero_eof` is key here. That logic, and how those booleans are set, will give me a clearer picture of whether there is an opportunity to read potentially uninitialized data.



38/3 2026/10/09 13:34 tool read-file
0m Args:
{
  "File": "fs/ext4/inode.c",
  "FirstLine": 5020,
  "LineCount": 50
}

Results:
{
  "Contents": "5020:\t\t * ext4_page_mkwrite() converting inline data to an extent without\n5021:\t\t * holding i_rwsem is safe, as that path zeroes the full block before\n5022:\t\t * copying in the inline data.\n5023:\t\t */\n5024:\t\tif (ext4_has_inline_data(inode))\n5025:\t\t\treturn 0;\n5026:\t\t/* If we are processing an encrypted inode during orphan list handling */\n5027:\t\tif (IS_ENCRYPTED(inode) \u0026\u0026 !fscrypt_has_encryption_key(inode))\n5028:\t\t\treturn 0;\n5029:\t\n5030:\t\tif (length \u003e blocksize - offset)\n5031:\t\t\tlength = blocksize - offset;\n5032:\t\n5033:\t\terr = ext4_block_zero_range(inode, from, length,\n5034:\t\t\t\t\t    \u0026did_zero, \u0026zero_written);\n5035:\t\tif (err)\n5036:\t\t\treturn err;\n5037:\t\t/*\n5038:\t\t * It's necessary to order zeroed data before update i_disksize when\n5039:\t\t * truncating up or performing an append write, because there might be\n5040:\t\t * exposing stale on-disk data which may caused by concurrent post-EOF\n5041:\t\t * mmap write during folio writeback.\n5042:\t\t */\n5043:\t\tif (did_zero \u0026\u0026 zero_written \u0026\u0026 !IS_DAX(inode)) {\n5044:\t\t\tif (ext4_should_order_data(inode)) {\n5045:\t\t\t\thandle_t *handle;\n5046:\t\n5047:\t\t\t\thandle = ext4_journal_start(inode, EXT4_HT_MISC, 1);\n5048:\t\t\t\tif (IS_ERR(handle))\n5049:\t\t\t\t\treturn PTR_ERR(handle);\n5050:\t\n5051:\t\t\t\terr = ext4_jbd2_inode_add_write(handle, inode, from,\n5052:\t\t\t\t\t\t\t\tlength);\n5053:\t\t\t\text4_journal_stop(handle);\n5054:\t\t\t} else if (ext4_inode_buffered_iomap(inode))\n5055:\t\t\t\terr = ext4_iomap_mark_disksize_pending(inode, from);\n5056:\t\t\tif (err)\n5057:\t\t\t\treturn err;\n5058:\t\t}\n5059:\t\n5060:\t\ttrace_ext4_block_zero_eof(inode, from, length, did_zero, zero_written);\n5061:\t\treturn 0;\n5062:\t}\n5063:\t\n5064:\t/*\n5065:\t * Zero out the unaligned head and tail of the [lstart, lstart+length)\n5066:\t * range.\n5067:\t *\n5068:\t * On return, @partial_zeroed records which edges actually got\n5069:\t * partial-zeroed.  Set EXT4_PARTIAL_ZERO_START/EXT4_PARTIAL_ZERO_END if\n"
}

39/3 2026/10/09 13:34 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 59522
							output: 34
							thoughts: 228
						

Thoughts:
**Analyzing the ext4 Zeroing Logic**

Okay, let's dive into this `ext4_block_iomap_zero_range` function. I need to understand how it handles zeroing ranges within an ext4 filesystem. I can see the core of the operation seems to delegate to `iomap_zero_range`. The code snippet shows this is the main part of the function:

```c
	ret = iomap_zero_range(inode, from, length, did_zero,
			       &ext4_iomap_zero_ops, &ext4_iomap_write_ops,
			       NULL);
	if (ret)
		return ret;
```
It's passing `inode`, the start `from`, and length to zero, along with `did_zero`, `ext4_iomap_zero_ops`, `ext4_iomap_write_ops`, and `NULL`. The `did_zero` and `zero_written` variables are updated depending on the result from `iomap_zero_range`.

Now, I'm getting a hunch. I should double check to see if `ext4_zero_partial_blocks` also calls this function, `ext4_block_zero_range`. Let me check out `ext4_zero_partial_blocks` and confirm my suspicion.



40/3 2026/10/09 13:34 tool grepper
0m Args:
{
  "Expression": "ext4_block_zero_range",
  "PathPrefixes": [
    "fs/ext4/"
  ]
}

Results:
{
  "Output": "fs/ext4/inode.c=4805=static int ext4_block_iomap_zero_range(struct inode *inode, loff_t from,\n--\nfs/ext4/inode.c-4852- */\nfs/ext4/inode.c:4853:static int ext4_block_zero_range(struct inode *inode,\nfs/ext4/inode.c-4854-\t\t\t\t loff_t from, loff_t length, bool *did_zero,\n--\nfs/ext4/inode.c=5006=int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)\n--\nfs/ext4/inode.c-5032-\nfs/ext4/inode.c:5033:\terr = ext4_block_zero_range(inode, from, length,\nfs/ext4/inode.c-5034-\t\t\t\t    \u0026did_zero, \u0026zero_written);\n--\nfs/ext4/inode.c=5076=int ext4_zero_partial_blocks(struct inode *inode, loff_t lstart, loff_t length,\n--\nfs/ext4/inode.c-5094-\t    (partial_start || (partial_end != sb-\u003es_blocksize - 1))) {\nfs/ext4/inode.c:5095:\t\terr = ext4_block_zero_range(inode, lstart, length, \u0026did_zero,\nfs/ext4/inode.c-5096-\t\t\t\t\t    NULL);\n--\nfs/ext4/inode.c-5103-\tif (partial_start) {\nfs/ext4/inode.c:5104:\t\terr = ext4_block_zero_range(inode, lstart, sb-\u003es_blocksize,\nfs/ext4/inode.c-5105-\t\t\t\t\t    \u0026did_zero, NULL);\n--\nfs/ext4/inode.c-5113-\t\tdid_zero = false;\nfs/ext4/inode.c:5114:\t\terr = ext4_block_zero_range(inode, byte_end - partial_end,\nfs/ext4/inode.c-5115-\t\t\t\t\t    partial_end + 1, \u0026did_zero, NULL);\n"
}

41/3 2026/10/09 13:34 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 60662
							output: 38
							thoughts: 1464
						

Thoughts:
**Analyzing Potential Uninitialized Memory Issues in an ext4 Patch**

Okay, so I'm looking at this ext4 patch and immediately my mind jumps to potential uninitialized memory exposures, especially given my background. I see `zero_written` is passed as `NULL` in `ext4_zero_partial_blocks` and then checked for non-null in `ext4_block_iomap_zero_range`, which is safe. But wait, what about userspace interactions, that’s where things get interesting, is there any data being copied to or from user space?

I need to dive into the code more methodically. Let's see... looking at `file.c`, I see `ext4_buffered_write_iter` calls `ext4_iomap_buffered_write`, which then calls `iomap_file_buffered_write`. Aha! That's definitely a user-space write path, using the `iov_iter *from`, which puts data from user space into the kernel. What about reads? Looking at `ext4_set_aops`, I find that `ext4_iomap_read_folio` and `.readahead` is involved.

This brings up another question! Can uninitialized data be read by user space? My experience says this is a classic filesystem vulnerability. Usually, the issue arises when unwritten extents or holes are read from disk without being zeroed, leading to information leakage. But... wait, does KMSAN even track disk blocks? Nope, it doesn't. KMSAN is there to catch kernel uninitialized memory reads, but disk blocks are marked as initialized by the block layer.

So, the focus shifts to internal kernel structures and buffers. I need to methodically check all new variables and buffers introduced by this patch to ensure that they are initialized. Let's start by looking at each modified file:

1.  **`ext4.h`:** This file seems to be mostly about comments. Added constants, `EXT4_MOUNT2_BUFFERED_IOMAP`, etc. It looks like everything is about state and integer manipulations, that's not worrying.

2.  **`ext4_jbd2.c` and `ext4_jbd2.h`:** This is just logic, nothing to worry about.

3.  **`extents.c`:** Now we're getting somewhere. I see `ext4_ext_truncate`, `ext4_zero_range`, and `ext4_do_fallocate`, which could be interesting. `ext4_convert_unwritten_extents` caught my eye. There's a `struct ext4_map_blocks map;` there. But was it added by this patch? I need to check the diff.

    Okay, the diff confirms that `map` was already there! And it seems that map is assigned to and used in ext4_convert_unwritten_extents. The new thing is `bool internal_handle = !handle;`. Nothing seems uninitialized.

Overall, so far, I am not seeing any obvious uninitialized memory risks in this patch. My analysis reveals that the core concern of uninitialized data leaking to userspace, while valid in filesystem development, is mitigated by the way the block layer and KMSAN handle disk blocks. I'll need to continue reviewing, but at this point, the patch seems safe from this perspective.



42/3 2026/10/09 13:34 tool read-file
0m Args:
{
  "File": "fs/ext4/extents.c",
  "FirstLine": 5155,
  "LineCount": 60
}

Results:
{
  "Contents": "5155:\t\t\t\t\t   ext4_lblk_t *converted)\n5156:\t{\n5157:\t\text4_lblk_t max_blocks, conv_blocks = 0;\n5158:\t\tint ret = 0, ret2 = 0, ret3 = 0;\n5159:\t\tstruct ext4_map_blocks map;\n5160:\t\tunsigned int blkbits = inode-\u003ei_blkbits;\n5161:\t\tunsigned int credits;\n5162:\t\tbool internal_handle = !handle;\n5163:\t\n5164:\t\tmap.m_lblk = offset \u003e\u003e blkbits;\n5165:\t\tmap.m_len = max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);\n5166:\t\n5167:\t\t/* Credits to convert one extent in this range to written state. */\n5168:\t\tcredits = ext4_meta_trans_blocks(inode, max_blocks, 1, 0);\n5169:\t\n5170:\t\tif (internal_handle) {\n5171:\t\t\thandle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);\n5172:\t\t\tif (IS_ERR(handle)) {\n5173:\t\t\t\tret = PTR_ERR(handle);\n5174:\t\t\t\tgoto out;\n5175:\t\t\t}\n5176:\t\t}\n5177:\t\n5178:\t\twhile (max_blocks) {\n5179:\t\t\t/*\n5180:\t\t\t * The caller cannot know how many extents the range covers,\n5181:\t\t\t * so make sure the transaction can take one more extent,\n5182:\t\t\t * extending or restarting it once it is full.  The reserved\n5183:\t\t\t * handle of the buffer_head writeback path is safe as it has\n5184:\t\t\t * enough credits for the conversion, so it is not restarted\n5185:\t\t\t * here.\n5186:\t\t\t */\n5187:\t\t\tret = ext4_journal_ensure_credits(handle, credits, 0);\n5188:\t\t\tif (ret \u003c 0)\n5189:\t\t\t\tbreak;\n5190:\t\n5191:\t\t\t/*\n5192:\t\t\t * Do not cache any unrelated extents, as it does not hold the\n5193:\t\t\t * i_rwsem or invalidate_lock, which could corrupt the extent\n5194:\t\t\t * status tree.\n5195:\t\t\t */\n5196:\t\t\tret = ext4_map_blocks(handle, inode, \u0026map,\n5197:\t\t\t\t\t      EXT4_GET_BLOCKS_IO_CONVERT_EXT |\n5198:\t\t\t\t\t      EXT4_EX_NOCACHE);\n5199:\t\t\tif (ret \u003c= 0) {\n5200:\t\t\t\text4_warning(inode-\u003ei_sb,\n5201:\t\t\t\t\t     \"inode #%llu: block %u: len %u: ext4_map_blocks returned %d\",\n5202:\t\t\t\t\t     inode-\u003ei_ino, map.m_lblk, map.m_len, ret);\n5203:\t\t\t\tif (unlikely(ret == 0))\n5204:\t\t\t\t\tret = -EINVAL;\n5205:\t\t\t} else {\n5206:\t\t\t\tconv_blocks += map.m_len;\n5207:\t\t\t}\n5208:\t\n5209:\t\t\tret2 = ext4_mark_inode_dirty(handle, inode);\n5210:\t\t\tret = ret \u003c 0 ? ret : ret2;\n5211:\t\t\tif (ret)\n5212:\t\t\t\tbreak;\n5213:\t\n5214:\t\t\tmap.m_lblk += map.m_len;\n"
}

43/3 2026/10/09 13:34 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 63861
							output: 24
							thoughts: 1288
						

Thoughts:
**Analyzing the Ext4 Patch for Potential Issues**

Okay, let's keep going through this patch. First, I see calls to `iomap_file_buffered_write` and `ext4_enable_buffered_iomap`, suggesting this is related to buffered I/O, which is good to see. The `ext4_iomap_clear_disksize_pending`, `ext4_iomap_wait_disksize_pending`, and `ext4_iomap_get_disksize_pending_range` functions are interesting, especially the last one. I need to be careful with potential uninitialized variable reads. I noted that `ext4_iomap_get_disksize_pending_range` initializes `*start = disksize;` only if certain conditions are met, so I need to check the callers to make sure `*start` (which I see is called `pstart` in the callers) is only *used* if it's been initialized. After reviewing the callers, I'm confident that `pstart` is only used if `plen` is not zero, and `plen != 0` implies that `*start` is initialized, so I can confirm there is no uninitialized read here.

Next, I turned my attention to the changes in `ext4_map_blocks`. The critical variable here is `orig_mlen`, which now has a more explicit initialization. It's assigned a value before use in all cases, so that's fine. Also, I confirm `internal_handle` is initialized to `false`.

Finally, the `ext4_iomap_writepages` function. I noted a potential issue with `struct iomap_writepage_ctx wpc`. I want to verify if the struct is fully initialized. I know in C, unspecified fields in a designated initializer are zero-initialized. I need to check the definition of `struct iomap_writepage_ctx` in `include/linux/iomap.h` to see which members are left uninitialized in the initialization and make sure those values are acceptable, but so far, I'm not seeing any red flags.



44/3 2026/10/09 13:34 tool grepper
0m Args:
{
  "Expression": "struct iomap_writepage_ctx \\{"
}

Results:
{
  "Output": "include/linux/iomap.h=520=struct iomap_writeback_ops {\n--\ninclude/linux/iomap.h-550-\ninclude/linux/iomap.h:551:struct iomap_writepage_ctx {\ninclude/linux/iomap.h-552-\tstruct iomap\t\tiomap;\n"
}

45/3 2026/10/09 13:34 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 65743
							output: 37
							thoughts: 22
						

Thoughts:
Here's my take on the thought process, tailored for an expert audience:

**Diving into the `iomap.h` Details**

Okay, time to dig into the kernel's I/O mapping internals. Specifically, I need to refresh my memory, or perhaps troubleshoot a specific issue, related to the `iomap` subsystem. Let's start with `include/linux/iomap.h`, and let's go directly to around line 551. I'm guessing that area probably deals with [*Specific concept related to the expert's field, e.g., direct I/O, extent mapping, or a particular I/O optimization strategy*]. Line 551 will likely hold some crucial macro definition, data structure, or function declaration that I need to understand, or potentially debug. Let's see what's in there. I bet it is related to [another specific concept or function]. Time to get to work.



46/3 2026/10/09 13:35 tool read-file
0m Args:
{
  "File": "include/linux/iomap.h",
  "FirstLine": 550,
  "LineCount": 25
}

Results:
{
  "Contents": " 550:\t\n 551:\tstruct iomap_writepage_ctx {\n 552:\t\tstruct iomap\t\tiomap;\n 553:\t\tstruct inode\t\t*inode;\n 554:\t\tstruct writeback_control *wbc;\n 555:\t\tconst struct iomap_writeback_ops *ops;\n 556:\t\tu32\t\t\tnr_folios;\t/* folios added to the ioend */\n 557:\t\tvoid\t\t\t*wb_ctx;\t/* pending writeback context */\n 558:\t};\n 559:\t\n 560:\tstruct iomap_ioend *iomap_init_ioend(struct inode *inode, struct bio *bio,\n 561:\t\t\tloff_t file_offset, u16 ioend_flags);\n 562:\tstruct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,\n 563:\t\t\tunsigned int max_len, bool is_append);\n 564:\tvoid iomap_finish_ioends(struct iomap_ioend *ioend, int error);\n 565:\tvoid iomap_ioend_try_merge(struct iomap_ioend *ioend,\n 566:\t\t\tstruct list_head *more_ioends);\n 567:\tvoid iomap_sort_ioends(struct list_head *ioend_list);\n 568:\tssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,\n 569:\t\t\tloff_t pos, loff_t end_pos, unsigned int dirty_len);\n 570:\tint iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error);\n 571:\t\n 572:\tvoid iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,\n 573:\t\t\tint error);\n 574:\tvoid iomap_finish_folio_write(struct inode *inode, struct folio *folio,\n"
}

47/3 2026/10/09 13:35 llm kmsan-evaluator
0m Model:
gemini-3.8-flash

Args:
null

Results:
null

Tokens:
							input: 66564
							output: 432
							thoughts: 3028
						

Thoughts:
**KMSAN Needs Assessment for Ext4 iomap Buffered I/O Patch**

My assessment of this Ext4 patch, which implements iomap buffered I/O, is that it does not introduce any risks that require KMSAN. The code changes focus on integrating the iomap infrastructure with the Ext4 filesystem, including buffered I/O, zeroed EOF handling, disksize tracking, and unwritten extent conversions.

My review has revealed the following:

1.  **No User-Space Data Exposure:** The patch does not introduce any pathways where uninitialized kernel structures or padded structures are copied to user space (e.g., via `copy_to_user`, `put_user`, netlink, ioctl). This rules out a significant class of information leak risks.

2.  **Thorough Initialization:** All local stack variables and structures, such as `struct iomap_writepage_ctx`, `struct ext4_map_blocks`, pointers, and scalar variables, are either:
    *   Designated-initialized by the compiler (e.g., using `.field = value`) and thus zeroed as per C standards.
    *   Initialized by the appropriate functions (e.g., `ext4_map_blocks`, `ext4_da_map_blocks`) before any field is read.
    *   Output pointer arguments are either always initialized before use, or the read of the output is strictly guarded, so uninitialized data cannot be read.

3.  **Risk Analysis and Bug Detection:** The primary risks associated with this patch involve:
    *   **Locking issues:** Potential deadlocks and incorrect lock ordering in the intricate interactions between folio locks, transaction handles, `i_rwsem`, and `i_data_sem`. These issues are detectable by LOCKDEP.
    *   **Race Conditions:** Potential race conditions in updates of the `EXT4_STATE_DISKSIZE_GROW_PENDING` and other inode state, or incorrect error handling.
    *   **Filesystem Corruption:** Potential corruption in the extent tree, or incorrect error handling, detectable via assertions, debugging, and testing.
    *   **Memory Errors:** Use-after-free conditions in `iomap_ioend` or `bio` handling, out-of-bounds accesses in extent tree manipulation. These are detectable by KASAN.

Crucially, **none** of these risks are related to the use of uninitialized memory in a way that KMSAN would detect. The focus is on locking correctness, data integrity, and potential memory errors.

Therefore, given the nature of the changes, the initialization strategies, and the kinds of bugs that are likely to arise, the use of KMSAN is not warranted. The patch does not introduce the types of risks that KMSAN is designed to detect.



48/3 2026/10/09 13:35 tool set-results
0m Args:
{
  "KMSANReasoning": "The patch series implements support for the iomap buffered I/O path in ext4 (enabled via the 'buffered_iomap' mount option), introducing iomap address space operations (ext4_iomap_aops), extent mapping helpers, writeback completion and disksize pending synchronization routines, and extent conversion handling.\n\nAnalysis regarding KMSAN vs. KASAN/LOCKDEP:\n1. No uninitialized memory or info-leak risk:\n   - The changes do not expose any kernel data structures, buffers, or padded structs to user space (no copy_to_user, put_user, netlink skb generation, or ioctl output structs).\n   - All stack variables (e.g., `struct ext4_map_blocks map`, `struct iomap_writepage_ctx wpc`) are properly initialized: `wpc` uses designated initialization zeroing all unspecified fields; `ext4_map_blocks` and `ext4_da_map_blocks` explicitly zero `map-\u003em_flags` and assign `map-\u003em_seq` / `map-\u003em_pblk` before use; helper functions like `ext4_iomap_get_disksize_pending_range()` initialize the out-parameter whenever a non-zero length is returned, and callers strictly check the return value before accessing the out-parameter.\n2. KASAN and LOCKDEP coverage:\n   - The primary risks of this patch are complex lock ordering changes (between folio locks, journal transaction starts, i_data_sem, and i_rwsem) which are scrutinized by LOCKDEP, and potential bio/ioend lifetime or concurrency bugs (use-after-free or out-of-bounds access) which are covered by standard KASAN.\n\nBecause the changes do not introduce or expose uninitialized memory access or user-space information leaks, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

Results:
{
  "KMSANReasoning": "The patch series implements support for the iomap buffered I/O path in ext4 (enabled via the 'buffered_iomap' mount option), introducing iomap address space operations (ext4_iomap_aops), extent mapping helpers, writeback completion and disksize pending synchronization routines, and extent conversion handling.\n\nAnalysis regarding KMSAN vs. KASAN/LOCKDEP:\n1. No uninitialized memory or info-leak risk:\n   - The changes do not expose any kernel data structures, buffers, or padded structs to user space (no copy_to_user, put_user, netlink skb generation, or ioctl output structs).\n   - All stack variables (e.g., `struct ext4_map_blocks map`, `struct iomap_writepage_ctx wpc`) are properly initialized: `wpc` uses designated initialization zeroing all unspecified fields; `ext4_map_blocks` and `ext4_da_map_blocks` explicitly zero `map-\u003em_flags` and assign `map-\u003em_seq` / `map-\u003em_pblk` before use; helper functions like `ext4_iomap_get_disksize_pending_range()` initialize the out-parameter whenever a non-zero length is returned, and callers strictly check the return value before accessing the out-parameter.\n2. KASAN and LOCKDEP coverage:\n   - The primary risks of this patch are complex lock ordering changes (between folio locks, journal transaction starts, i_data_sem, and i_rwsem) which are scrutinized by LOCKDEP, and potential bio/ioend lifetime or concurrency bugs (use-after-free or out-of-bounds access) which are covered by standard KASAN.\n\nBecause the changes do not introduce or expose uninitialized memory access or user-space information leaks, a dedicated KMSAN fuzzing session is not warranted.",
  "NeedsKMSAN": false
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)