ntfs: replace put_folio zeroing with iomap tail handling
Namjae Jeon
maintainerPATCH 1/4 ntfs: replace put_folio zeroing with iomap tail handling
The NTFS put_folio callbacks clear uninitialized data around buffered writes and zero ranges. Move this work to iomap block preparation and use the default folio release path.
Set IOMAP_F_ZERO_TAIL only for mapped writes and zero ranges starting at or beyond initialized_size. Split mapped ranges at initialized_size and use iomap_truncate_page() to clear the boundary block's uninitialized tail when a mapping includes initialized bytes in that block. This preserves valid data when a short copy retries with a smaller length inside a cached iomap. Apply this to buffered write mappings and the read mappings reused by iomap_zero_range().
Record initialized_size in the validity cookie and revalidate it after locking each folio. Skip bytes initialized since the tail range was selected to preserve data written through a concurrent shared mapping.
--- fs/ntfs/iomap.c | 133 ++++++++++++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 62 deletions(-)
fs/ntfs/iomap.c +71 -62
@@ -12,73 +12,47 @@#include "ntfs.h" #include "iomap.h" -static void ntfs_iomap_put_folio_non_resident (struct inode *inode,loff_t pos, -unsigned int len,struct folio *folio) +static bool ntfs_iomap_valid_initialized_size (struct inode *inode, +const struct iomap *iomap) { -struct ntfs_inode *ni =NTFS_I (inode); -unsigned long sector_size =1UL << inode->i_blkbits; -loff_t start_down, end_up, init; - - start_down =round_down (pos, sector_size); - end_up = (pos + len -1 ) | (sector_size -1 ); - init = ni->initialized_size; - -if (init >= start_down && init <= end_up) { -if (init < pos) { -loff_t offset =offset_in_folio (folio, pos + len); - -if (offset ==0 ) - offset =folio_size (folio); -folio_zero_segments (folio, -offset_in_folio (folio, init), -offset_in_folio (folio, pos), - offset, -folio_size (folio)); - - }else { -loff_t offset =max_t (loff_t, pos + len, init); - - offset =offset_in_folio (folio, offset); -if (offset ==0 ) - offset =folio_size (folio); -folio_zero_segment (folio, - offset, -folio_size (folio)); - } - }else if (init <= pos) { -loff_t offset =0 , offset2 =offset_in_folio (folio, pos + len); - -if ((init >>folio_shift (folio)) == (pos >>folio_shift (folio))) - offset =offset_in_folio (folio, init); -if (offset2 ==0 ) - offset2 =folio_size (folio); -folio_zero_segments (folio, - offset, -offset_in_folio (folio, pos), - offset2, -folio_size (folio)); - } -folio_unlock (folio); -folio_put (folio); +return iomap->type ==IOMAP_INLINE || + iomap->validity_cookie ==READ_ONCE (NTFS_I (inode)->initialized_size); } +const struct iomap_write_ops ntfs_iomap_folio_ops = { + .iomap_valid = ntfs_iomap_valid_initialized_size, +}; + +static const struct iomap_ops ntfs_zero_tail_iomap_ops; +/* -* iomap_zero_range is called for an area beyond the initialized size, -* garbage values can be read, so zeroing out is needed. +* Only set IOMAP_F_ZERO_TAIL at or beyond initialized_size: short-copy +* retries can end before the end of a cached iomap. For initialized +* mappings, split at initialized_size and zero only the boundary block's +* uninitialized tail. */ -static void ntfs_iomap_put_folio (struct inode *inode,loff_t pos, -unsigned int len,struct folio *folio) +static int ntfs_iomap_prepare_tail (struct inode *inode,struct iomap *iomap) { -if (NInoNonResident (NTFS_I (inode))) -return ntfs_iomap_put_folio_non_resident (inode, pos, - len, folio); -folio_unlock (folio); -folio_put (folio); -} +loff_t init =READ_ONCE (NTFS_I (inode)->initialized_size); +loff_t end; -const struct iomap_write_ops ntfs_iomap_folio_ops = { - .put_folio = ntfs_iomap_put_folio, -}; + iomap->validity_cookie = init; +if (iomap->type !=IOMAP_MAPPED ) +return 0 ; + +if (iomap->offset >= init) { + iomap->flags |=IOMAP_F_ZERO_TAIL ; +return 0 ; + } + + iomap->length =min_t (u64, iomap->length, init - iomap->offset); + end = iomap->offset + iomap->length; +if (round_up (end,i_blocksize (inode)) <= init) +return 0 ; + +return iomap_truncate_page (inode, init,NULL , &ntfs_zero_tail_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +}static int ntfs_read_iomap_begin_resident (struct inode *inode,loff_t offset,loff_t length,unsigned int flags,struct iomap *iomap,bool keep_mrec_lock)
@@ -256,6 +230,14 @@ static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset iomap->length = length;up_write (&ni->runlist.lock); +if (flags &IOMAP_ZERO ) { +int err; + + err =ntfs_iomap_prepare_tail (inode, iomap); +if (err) +return err; + } + if (!(flags & IOMAP_ZERO) && iomap->type == IOMAP_MAPPED && iomap->offset < ni->initialized_size &&
@@ -332,7 +314,6 @@ static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *iomap) }static const struct iomap_write_ops ntfs_zero_iomap_folio_ops = { - .put_folio = ntfs_iomap_put_folio, .iomap_valid = ntfs_iomap_valid, };
@@ -364,6 +345,34 @@ const struct iomap_ops ntfs_seek_iomap_ops = { .iomap_next = ntfs_seek_iomap_next, }; +static int ntfs_zero_tail_iomap_begin (struct inode *inode,loff_t offset, +loff_t length,unsigned int flags,struct iomap *iomap, +struct iomap *srcmap) +{ +loff_t init =READ_ONCE (NTFS_I (inode)->initialized_size); +int err; + +/* Skip bytes initialized after the tail range was selected. */ +if (offset < init) { + iomap->type =IOMAP_HOLE ; + iomap->addr =IOMAP_NULL_ADDR ; + iomap->offset = offset; + iomap->length =min (length, init - offset); +return 0 ; + } + + err =ntfs_seek_iomap_begin (inode, offset, length, flags, iomap, srcmap); + iomap->validity_cookie = init; +return err; +} + +static DEFINE_IOMAP_ITER_NEXT(ntfs_zero_tail_iomap_next, + ntfs_zero_tail_iomap_begin); + +static const struct iomap_ops ntfs_zero_tail_iomap_ops = { + .iomap_next = ntfs_zero_tail_iomap_next, +}; +int ntfs_dio_zero_range (struct inode *inode,loff_t offset,loff_t length) {if ((offset | length) & (SECTOR_SIZE -1 ))
@@ -573,7 +582,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ iomap->length = length; } -return 0 ; +return ntfs_iomap_prepare_tail (inode, iomap); }#define NTFS_IOMAP_FLAGS_BEGIN BIT(1)
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 2/4 ntfs: sync attribute inode size after resize
Internal attribute truncation bypasses truncate_setsize(), so successful resizes must update the attribute inode's i_size. Restrict the update to attribute inodes to keep KEEP_SIZE fallocate from changing a file's i_size.
--- fs/ntfs/attrib.c | 2 ++ 1 file changed, 2 insertions(+)
fs/ntfs/attrib.c +2 -0
@@ -5187,6 +5187,8 @@ int ntfs_attr_truncate_i_locked(struct ntfs_inode *ni, const s64 newsize, ni, newsize, locked_ni, false); }else err =ntfs_resident_attr_resize (ni, newsize,0 , holes); +if (!err &&NInoAttr (ni)) +i_size_write (VFS_I (ni), newsize);ntfs_debug ("Return status %d\n" , err);return err; }
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 3/4 ntfs: clear stale pagecache on initialized-size extension
In reply to Namjae Jeoniomap_zero_range() skips holes, so EOF-polluted cache data can survive gap zeroing. Discard the uninitialized cache range before advancing initialized_size so later reads cannot expose stale bytes.
Perform the cleanup in ntfs_extend_initialized_size() so both file writes and shared writable mmap preparation use it. Hold the invalidate lock while reading initialized_size, clearing the cache, zeroing the gap, and updating the attribute. Release the lock on every exit path.
--- fs/ntfs/inode.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-)
fs/ntfs/inode.c +27 -14
@@ -8,6 +8,7 @@#include <linux/writeback.h> #include <linux/seq_file.h> +#include <linux/mm.h> #include "lcnalloc.h" #include "time.h"
@@ -2484,38 +2485,50 @@ int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, const loff_t new_size) {struct ntfs_inode *ni =NTFS_I (vi); -loff_t old_init_size; +loff_t old_init_size, clear_size;unsigned long flags; -int err; +int err =0 ; + +if (!NInoNonResident (ni)) +return -EINVAL ; +filemap_invalidate_lock (vi->i_mapping);read_lock_irqsave (&ni->size_lock, flags); old_init_size = ni->initialized_size;read_unlock_irqrestore (&ni->size_lock, flags); -if (!NInoNonResident (ni)) -return -EINVAL ;if (old_init_size >= new_size) -return 0 ; + goto out; err =ntfs_attr_map_whole_runlist (ni);if (err) -return err; + goto out; -if (!NInoCompressed (ni) && old_init_size < offset) { - err =iomap_zero_range (vi, old_init_size, - offset - old_init_size, -NULL , &ntfs_seek_iomap_ops, - &ntfs_iomap_folio_ops,NULL ); -if (err) -return err; +if (!NInoCompressed (ni)) { +/* +* Zeroing skips holes, so discard cached uninitialized bytes +* before making them visible to writes or shared mappings. +*/ + clear_size =max_t (loff_t,i_size_read (vi), new_size); +truncate_pagecache_range (vi, old_init_size, clear_size -1 ); + +if (old_init_size < offset) { + err =iomap_zero_range (vi, old_init_size, + offset - old_init_size, +NULL , &ntfs_seek_iomap_ops, + &ntfs_iomap_folio_ops,NULL ); +if (err) + goto out; + } } -mutex_lock (&ni->mrec_lock); err =ntfs_attr_set_initialized_size (ni, new_size);mutex_unlock (&ni->mrec_lock);if (err)truncate_setsize (vi, old_init_size); +out: +filemap_invalidate_unlock (vi->i_mapping);return err; }
Signature · 1 line
2.25.1
Namjae Jeon
maintainerPATCH 4/4 ntfs: propagate EA truncate errors
In reply to Namjae JeonPropagate errors from truncating an EA stream so callers do not report a successful xattr update after a failed resize.
--- fs/ntfs/ea.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ntfs/ea.c +1 -1
@@ -37,7 +37,7 @@ static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 eastruct ntfs_inode *ea_ni =NTFS_I (ea_vi);if (need_truncate && ea_ni->data_size > ea_off + ea_size) -ntfs_attr_truncate (ea_ni, ea_off + ea_size); + err =ntfs_attr_truncate (ea_ni, ea_off + ea_size);mark_mft_record_dirty (ni); }
Signature · 1 line
2.25.1
Hyunchul Lee
maintainerRe: [PATCH 4/4] ntfs: propagate EA truncate errors
남재 책임님, AI 돌린 리뷰 결과입니다.
그리고 ntfs-next의 d91900254dad ntfs: restart the zone search when the allocation hint fails 패치를 적용하면
generic/127의 문제가 수정됩니다. 기존 클러스터 할당에는 is_contig = true여도 두 runs으로 분리되어 할당되는 경우가 있었는데,
이 경우에 첫 runs만 fallocate에서 제로 아웃을 해서 문제가 발생했었습니다.
판정: Changes requested. 심각도 High. 정확한 트리거는
fs/ntfs/ea.c:33-40 뒤의 fs/ntfs/ea.c:392-400 이며, 영향은 영속적인
EA data/metadata 불일치와 가능한 파일시스템 손상이다. 신뢰도는 9/10이다. 관련된 이전 Lore 리뷰 코멘트는 없다. 최소 수정 방향은 모든 축소 가능한 replacement/update에서 기존 $EA 바이트와 기존 struct ea_information 을 보존하고, prefix write, truncate 또는 information write가 실패하면 둘 다 복구하는 것이다. rollback 오류를 조용히 버려서는 안 된다. 같은 on-disk invariant를 제공한다면 atomic staging도 대안이다.
2026년 10월 7일 (수) 오후 10:19, Namjae Jeon <[email protected]>님이 작성:
Quoted 26 lines
Propagate errors from truncating an EA stream so callers do not report a successful xattr update after a failed resize.
Fixes fc053f05ca28 ("ntfs: add reparse and ea operations")Signed-off-by Namjae Jeon <[email protected]>--- fs/ntfs/ea.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/ntfs/ea.c +1
-2@@ -37,7 +37,7 @@ static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 eastruct ntfs_inode *ea_ni =NTFS_I (ea_vi);if (need_truncate && ea_ni->data_size > ea_off + ea_size) -ntfs_attr_truncate (ea_ni, ea_off + ea_size); + err =ntfs_attr_truncate (ea_ni, ea_off + ea_size);mark_mft_record_dirty (ni); } --2.25.1
Signature · 2 lines
Thanks, Hyunchul