exfat: validate cached iomaps during buffered writes and writeback
Subsystems (2)
PATCH v3 1/1 exfat: validate cached iomaps during buffered writes and writeback
From: Chi Zhiling <[email protected]>
Writeback does not hold i_rwsem and can race with truncate or other operations that change the file's block mapping. This may leave cached iomaps stale, causing writeback to write data to blocks that no longer belong to the file.
Track iomap validity with exfat's cache_valid_id and implement iomap_valid() so cached iomaps are revalidated after the folio is locked. If the mapping has changed, a new iomap is obtained before writeback continues.
This matches the iomap validity model used by XFS and prevents data corruption from stale cached iomaps.
IOMAP_F_MERGED is set twice in __exfat_iomap_begin(), this patch also drops the redundant setting.
--- fs/exfat/file.c | 4 ++-- fs/exfat/iomap.c | 15 +++++++++++++-- fs/exfat/iomap.h | 1 + 3 files changed, 16 insertions(+), 4 deletions(-)
fs/exfat/file.c +2 -2
@@ -772,8 +772,8 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)if (iocb->ki_flags &IOCB_DIRECT ) ret =exfat_dio_write_iter (iocb, iter);else - ret =iomap_file_buffered_write (iocb, iter, - &exfat_write_iomap_ops,NULL ,NULL ); + ret =iomap_file_buffered_write (iocb, iter, &exfat_write_iomap_ops, + &exfat_iomap_write_ops,NULL );if (ret <0 ) goto unlock;
fs/exfat/iomap.c +13 -2
@@ -56,6 +56,7 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length iomap->addr =IOMAP_NULL_ADDR ; iomap->offset = offset; iomap->length = length; + iomap->validity_cookie = ei->cache_valid_id;return 0 ; }
@@ -133,7 +134,7 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length } } - iomap->flags |=IOMAP_F_MERGED ; + iomap->validity_cookie = ei->cache_valid_id; out:mutex_unlock (&sbi->s_lock);return err;
@@ -191,6 +192,15 @@ const struct iomap_ops exfat_write_iomap_ops = { .iomap_end = exfat_write_iomap_end, }; +static bool exfat_iomap_valid (struct inode *inode,const struct iomap *iomap) +{ +return EXFAT_I (inode)->cache_valid_id == iomap->validity_cookie; +} + +const struct iomap_write_ops exfat_iomap_write_ops = { + .iomap_valid = exfat_iomap_valid, +}; +/* * exfat_writeback_range - Map folio during writeback *
@@ -201,7 +211,8 @@ static ssize_t exfat_writeback_range(struct iomap_writepage_ctx *wpc, struct folio *folio, u64 offset, unsigned int len, u64 end_pos) { if (offset < wpc->iomap.offset || - offset >= wpc->iomap.offset + wpc->iomap.length) { + offset >= wpc->iomap.offset + wpc->iomap.length || + !exfat_iomap_valid(wpc->inode, &wpc->iomap)) { int error; error = __exfat_iomap_begin(wpc->inode, offset, len,
fs/exfat/iomap.h +1 -0
@@ -11,6 +11,7 @@ extern const struct iomap_ops exfat_iomap_ops;extern const struct iomap_ops exfat_write_iomap_ops;extern const struct iomap_writeback_ops exfat_writeback_ops;extern const struct iomap_read_ops exfat_iomap_bio_read_ops; +extern const struct iomap_write_ops exfat_iomap_write_ops;int exfat_iomap_swap_activate (struct swap_info_struct *sis,struct file *file,sector_t *span);
Signature · 1 line
2.43.0
Namjae Jeon
maintainerQuoted 7 lines · Chi Zhiling
From: Chi Zhiling <[email protected]>
Writeback does not hold
i_rwsemand can race with truncate or other operations that change the file's block mapping. This may leave cached iomaps stale, causing writeback to write data to blocks that no longer belong to the file.
Do you have an actual reproducer or an observed corruption for this ? Thanks.
Quoted 10 lines · Namjae Jeon
Quoted 7 lines · Chi Zhiling
From: Chi Zhiling <[email protected]>
Writeback does not hold
i_rwsemand can race with truncate or other operations that change the file's block mapping. This may leave cached iomaps stale, causing writeback to write data to blocks that no longer belong to the file.Do you have an actual reproducer or an observed corruption for this ? Thanks.
I haven't actually observed any data corruption, and I haven't been able to reproduce the issue.
I just checked the code again, and it seems this issue cannot actually occur. The reason is that no operation other than truncate can modify the block mapping, and truncate waits for page reclaim to complete before releasing it.
Sorry for taking up your time, and thank you very much for your help :)
Namjae Jeon
maintainerQuoted 16 lines · Chi Zhiling
Quoted 10 lines · Namjae Jeon
Quoted 7 lines · Chi Zhiling
From: Chi Zhiling <[email protected]>
Writeback does not hold
i_rwsemand can race with truncate or other operations that change the file's block mapping. This may leave cached iomaps stale, causing writeback to write data to blocks that no longer belong to the file.Do you have an actual reproducer or an observed corruption for this ? Thanks.
I haven't actually observed any data corruption, and I haven't been able to reproduce the issue.
Quoted 6 lines
I just checked the code again, and it seems this issue cannot actually occur. The reason is that no operation other than truncate can modify the block mapping, and truncate waits for page reclaim to complete before releasing it.
Sorry for taking up your time, and thank you very much for your help :)
No problem at all, Thank you for checking again and confirming that there is no issue.