Skip to contentSkip to conversation

exfat: validate cached iomaps during buffered writes and writeback

PATCH Superseded Chi Zhiling 2 participants 3 messages Jul 1 – Jul 2

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 fs/exfat/cache.c |  7 ++++---
 fs/exfat/file.c  |  4 ++--
 fs/exfat/iomap.c | 15 +++++++++++++--
 fs/exfat/iomap.h |  1 +
 4 files changed, 20 insertions(+), 7 deletions(-)
fs/exfat/cache.c +4 -3
index 7c8b4182f5de..71631bab182c 100644
@@ -228,9 +228,10 @@ static void __exfat_cache_inval_inode(struct inode *inode)
 		exfat_cache_free(cache);
 	}
 	/* Update. The copy of caches before this id is discarded. */
-	ei->cache_valid_id++;
-	if (ei->cache_valid_id == EXFAT_CACHE_VALID)
-		ei->cache_valid_id++;
+	if (ei->cache_valid_id + 1 == EXFAT_CACHE_VALID)
+		ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
+	else
+		ei->cache_valid_id = ei->cache_valid_id + 1;
 }
 
 void exfat_cache_inval_inode(struct inode *inode)
fs/exfat/file.c +2 -2
index 5fc13378d35f..4258398ca641 100644
@@ -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
index 1aac38e63fe6..efced320a5ef 100644
@@ -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;
@@ -155,6 +156,15 @@ const struct iomap_ops exfat_iomap_ops = {
 	.iomap_begin = exfat_iomap_begin,
 };
 
+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_write_iomap_end - Update the state after write
  *
@@ -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
index fd8a913f7794..39c93f8cd790 100644
@@ -9,6 +9,7 @@
 extern const struct iomap_dio_ops exfat_write_dio_ops;
 extern const struct iomap_ops exfat_iomap_ops;
 extern const struct iomap_ops exfat_write_iomap_ops;
+extern const struct iomap_write_ops exfat_iomap_write_ops;
 extern const struct iomap_writeback_ops exfat_writeback_ops;
 extern const struct iomap_read_ops exfat_iomap_bio_read_ops;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines
fs/exfat/cache.c +0 -0
index 7c8b4182f5de..71631bab182c 100644

fs/exfat/cache.c static void __exfat_cache_inval_inode(struct inode *inode) · line 234

Quoted 10 lines
fs/exfat/cache.c +4 -3
@@ -228,9 +228,10 @@ static void __exfat_cache_inval_inode(struct inode *inode)
                exfat_cache_free(cache);
        }
        /* Update. The copy of caches before this id is discarded. */
-       ei->cache_valid_id++;
-       if (ei->cache_valid_id == EXFAT_CACHE_VALID)
-               ei->cache_valid_id++;
+       if (ei->cache_valid_id + 1 == EXFAT_CACHE_VALID)
+               ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
+       else
+               ei->cache_valid_id = ei->cache_valid_id + 1;

This looks equivalent to the original one. What does this rewrite change?

fs/exfat/cache.c static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length · line 136

Quoted 5 lines
fs/exfat/cache.c +0 -1
@@ -133,7 +134,7 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length
                }
        }
 
-       iomap->flags |= IOMAP_F_MERGED;

Is dropping the IOMAP_F_MERGED flag intentional? If yes, Why ?

fs/exfat/cache.c static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length · line 137

Quoted 1 line
fs/exfat/cache.c +1 -0
+       iomap->validity_cookie = ei->cache_valid_id;

Thanks.

#2 lore raw
Quoted 8 lines · Namjae Jeon
Quoted 8 lines
fs/exfat/cache.c +0 -0
index 7c8b4182f5de..71631bab182c 100644
@@ -228,9 +228,10 @@ static void __exfat_cache_inval_inode(struct inode *inode)
                 exfat_cache_free(cache);
         }
         /* Update. The copy of caches before this id is discarded. */
Quoted 8 lines

fs/exfat/cache.c static void __exfat_cache_inval_inode(struct inode *inode) · line 234

Quoted 7 lines
fs/exfat/cache.c +4 -3
-       ei->cache_valid_id++;
-       if (ei->cache_valid_id == EXFAT_CACHE_VALID)
-               ei->cache_valid_id++;
+       if (ei->cache_valid_id + 1 == EXFAT_CACHE_VALID)
+               ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
+       else
+               ei->cache_valid_id = ei->cache_valid_id + 1;

This looks equivalent to the original one. What does this rewrite change?

This changes the number of writes, but it doesn't make much sense. I'll revert this change in the next version.

Quoted 7 lines

fs/exfat/cache.c static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length · line 136

Quoted 6 lines
fs/exfat/cache.c +0 -1
@@ -133,7 +134,7 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length
                 }
         }
 
-       iomap->flags |= IOMAP_F_MERGED;

Is dropping the IOMAP_F_MERGED flag intentional? If yes, Why ?

Because IOMAP_F_MERGED is set twice in __exfat_iomap_begin(), this patch also drops the redundant assignment.

Thanks,

Quoted 3 lines

fs/exfat/cache.c static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length · line 137

Quoted 1 line
fs/exfat/cache.c +1 -0
+       iomap->validity_cookie = ei->cache_valid_id;

Thanks.