Skip to contentSkip to conversation

exfat: convert to iomap

PATCH Superseded Namjae Jeon 5 participants 32 messages Mar 26 – Apr 7

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

cover exfat: convert to iomap

This patch series converts the exfat filesystem to the iomap framework for buffered I/O, direct I/O, and llseek (SEEK_HOLE/SEEK_DATA) support.

Namjae Jeon (5):
exfat: add iomap support
exfat: add iomap direct I/O support
exfat: add iomap buffered I/O support
exfat: add support for multi-cluster allocation
exfat: add support for SEEK_HOLE and SEEK_DATA in llseek

 fs/exfat/Makefile   |   2 +-
 fs/exfat/dir.c      |   2 +-
 fs/exfat/exfat_fs.h |  17 ++-
 fs/exfat/fatent.c   |  26 ++--
 fs/exfat/file.c     | 243 ++++++++++++++++++++++---------
 fs/exfat/inode.c    | 341 +++++---------------------------------------
 fs/exfat/iomap.c    | 305 +++++++++++++++++++++++++++++++++++++++
 fs/exfat/iomap.h    |  16 +++
 fs/exfat/namei.c    |   2 +-
 fs/exfat/super.c    |   1 +
 10 files changed, 565 insertions(+), 390 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH 1/5 exfat: add iomap support

Add iomap support to the exfat filesystem. This patch introduces the necessary iomap infrastructure by adding a new iomap.c file and related iomap operations. The main change is converting exfat_extend_valid_size() to use iomap_zero_range() instead of the legacy write_begin/write_end path. To support this, exfat_map_cluster() is extended to return whether a new cluster was allocated via a balloc flag, and a new helper function exfat_cluster_to_phys() is added. Also, data_start_bytes is added to struct exfat_sb_info for easier conversion from cluster number to physical byte offset.

---
 fs/exfat/Makefile   |   2 +-
 fs/exfat/exfat_fs.h |  12 ++
 fs/exfat/file.c     |  54 +++-----
 fs/exfat/inode.c    |   9 +-
 fs/exfat/iomap.c    | 305 ++++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/iomap.h    |  16 +++
 fs/exfat/super.c    |   1 +
 7 files changed, 361 insertions(+), 38 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
fs/exfat/Makefile +1 -1
index ed51926a4971..e06bf85870ae 100644
@@ -5,4 +5,4 @@
 obj-$(CONFIG_EXFAT_FS) += exfat.o
 
 exfat-y	:= inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \
-	   file.o balloc.o
+	   file.o balloc.o iomap.o
fs/exfat/exfat_fs.h +12 -0
index 9fed9fb33cae..860f2e438b63 100644
@@ -259,6 +259,7 @@ struct exfat_sb_info {
 	unsigned long long FAT1_start_sector; /* FAT1 start sector */
 	unsigned long long FAT2_start_sector; /* FAT2 start sector */
 	unsigned long long data_start_sector; /* data area start sector */
+	unsigned long long data_start_bytes;
 	unsigned int num_FAT_sectors; /* num of FAT sectors */
 	unsigned int root_dir; /* root dir cluster */
 	unsigned int dentries_per_clu; /* num of dentries per cluster */
@@ -432,6 +433,13 @@ static inline loff_t exfat_ondisk_size(const struct inode *inode)
 	return ((loff_t)inode->i_blocks) << 9;
 }
 
+static inline loff_t exfat_cluster_to_phys(struct exfat_sb_info *sbi,
+		unsigned int clus)
+{
+	return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bits) +
+		sbi->data_start_bytes;
+}
+
 /* super.c */
 int exfat_set_volume_dirty(struct super_block *sb);
 int exfat_clear_volume_dirty(struct super_block *sb);
@@ -480,6 +488,7 @@ long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
 				unsigned long arg);
 int exfat_force_shutdown(struct super_block *sb, u32 flags);
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync);
 
 /* namei.c */
 extern const struct dentry_operations exfat_dentry_ops;
@@ -543,6 +552,9 @@ int __exfat_write_inode(struct inode *inode, int sync);
 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
 void exfat_evict_inode(struct inode *inode);
 int exfat_block_truncate_page(struct inode *inode, loff_t from);
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc);
 
 /* exfat/nls.c */
 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
fs/exfat/file.c +20 -34

87 lines not on this page · raw message

fs/exfat/inode.c +6 -3
index beb9ea7cca9f..cc54cce65a31 100644
@@ -123,8 +123,9 @@ void exfat_sync_inode(struct inode *inode)
  * Output: errcode, cluster number
  * *clu = (~0), if it's unable to allocate a new cluster
  */
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-		unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc)
 {
 	int ret;
 	unsigned int last_clu;
@@ -235,6 +236,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 			}
 		}
 		*count = 1;
+		*balloc = true;
 	}
 
 	/* hint information */
@@ -258,6 +260,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	sector_t phys = 0;
 	sector_t valid_blks;
 	loff_t i_size;
+	bool balloc;
 
 	mutex_lock(&sbi->s_lock);
 	i_size = i_size_read(inode);
@@ -268,7 +271,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	/* Is this block already allocated? */
 	count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
 	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
-			&cluster, &count, create);
+			&cluster, &count, create, &balloc);
 	if (err) {
 		if (err != -ENOSPC)
 			exfat_fs_error_ratelimit(sb,
fs/exfat/iomap.c new file +305 -0

305 lines not on this page · raw message

fs/exfat/iomap.h new file +16 -0
new file mode 100644
index 000000000000..4abe0dc452ee
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2026 Namjae Jeon <[email protected]>
+ */
+
+#ifndef _LINUX_EXFAT_IOMAP_H
+#define _LINUX_EXFAT_IOMAP_H
+
+extern const struct iomap_write_ops exfat_iomap_folio_ops;
+extern const struct iomap_ops exfat_read_iomap_ops;
+extern const struct iomap_ops exfat_write_iomap_ops;
+extern const struct iomap_dio_ops exfat_write_dio_ops;
+extern const struct iomap_writeback_ops exfat_writeback_ops;
+extern const struct iomap_ops exfat_mkwrite_iomap_ops;
+
+#endif /* _LINUX_EXFAT_IOMAP_H */
fs/exfat/super.c +1 -0
index 83396fd265cd..b69c4b0a926b 100644
@@ -499,6 +499,7 @@ static int exfat_read_boot_sector(struct super_block *sb)
 	if (p_boot->num_fats == 2)
 		sbi->FAT2_start_sector += sbi->num_FAT_sectors;
 	sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
+	sbi->data_start_bytes = sbi->data_start_sector << p_boot->sect_size_bits;
 	sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
 	/* because the cluster index starts with 2 */
 	sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Hi, Namjae

Could you CC my personal email "[email protected]" when sending v2? since my work email can’t receive messages immediately.

Quoted 123 lines · Namjae Jeon

Add iomap support to the exfat filesystem. This patch introduces the necessary iomap infrastructure by adding a new iomap.c file and related iomap operations. The main change is converting exfat_extend_valid_size() to use iomap_zero_range() instead of the legacy write_begin/write_end path. To support this, exfat_map_cluster() is extended to return whether a new cluster was allocated via a balloc flag, and a new helper function exfat_cluster_to_phys() is added. Also, data_start_bytes is added to struct exfat_sb_info for easier conversion from cluster number to physical byte offset.

---
  fs/exfat/Makefile   |   2 +-
  fs/exfat/exfat_fs.h |  12 ++
  fs/exfat/file.c     |  54 +++-----
  fs/exfat/inode.c    |   9 +-
  fs/exfat/iomap.c    | 305 ++++++++++++++++++++++++++++++++++++++++++++
  fs/exfat/iomap.h    |  16 +++
  fs/exfat/super.c    |   1 +
  7 files changed, 361 insertions(+), 38 deletions(-)

create mode 100644 fs/exfat/iomap.c
create mode 100644 fs/exfat/iomap.h

fs/exfat/Makefile +1 -1
index ed51926a4971..e06bf85870ae 100644
@@ -5,4 +5,4 @@
  obj-$(CONFIG_EXFAT_FS) += exfat.o
 
  exfat-y	:= inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \
-	   file.o balloc.o
+	   file.o balloc.o iomap.o
fs/exfat/exfat_fs.h +12 -0
index 9fed9fb33cae..860f2e438b63 100644
@@ -259,6 +259,7 @@ struct exfat_sb_info {
  	unsigned long long FAT1_start_sector; /* FAT1 start sector */
  	unsigned long long FAT2_start_sector; /* FAT2 start sector */
  	unsigned long long data_start_sector; /* data area start sector */
+	unsigned long long data_start_bytes;
  	unsigned int num_FAT_sectors; /* num of FAT sectors */
  	unsigned int root_dir; /* root dir cluster */
  	unsigned int dentries_per_clu; /* num of dentries per cluster */
@@ -432,6 +433,13 @@ static inline loff_t exfat_ondisk_size(const struct inode *inode)
  	return ((loff_t)inode->i_blocks) << 9;
  }
 
+static inline loff_t exfat_cluster_to_phys(struct exfat_sb_info *sbi,
+		unsigned int clus)
+{
+	return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bits) +
+		sbi->data_start_bytes;
+}
+
  /* super.c */
  int exfat_set_volume_dirty(struct super_block *sb);
  int exfat_clear_volume_dirty(struct super_block *sb);
@@ -480,6 +488,7 @@ long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
  				unsigned long arg);
  int exfat_force_shutdown(struct super_block *sb, u32 flags);
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync);
 
  /* namei.c */
  extern const struct dentry_operations exfat_dentry_ops;
@@ -543,6 +552,9 @@ int __exfat_write_inode(struct inode *inode, int sync);
  int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
  void exfat_evict_inode(struct inode *inode);
  int exfat_block_truncate_page(struct inode *inode, loff_t from);
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc);
 
  /* exfat/nls.c */
  unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
fs/exfat/file.c +4 -22
index 2daf0dbabb24..756846b774c4 100644
@@ -14,9 +14,11 @@
  #include <linux/writeback.h>
  #include <linux/filelock.h>
  #include <linux/falloc.h>
+#include <linux/iomap.h>
 
  #include "exfat_raw.h"
  #include "exfat_fs.h"
+#include "iomap.h"
 
  static int exfat_cont_expand(struct inode *inode, loff_t size)
  {
@@ -628,44 +630,28 @@ int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
  	return blkdev_issue_flush(inode->i_sb->s_bdev);
  }
 
-static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size)
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)
  {
-	int err;
-	loff_t pos;
  	struct exfat_inode_info *ei = EXFAT_I(inode);
-	struct address_space *mapping = inode->i_mapping;
-	const struct address_space_operations *ops = mapping->a_ops;
-
-	pos = ei->valid_size;
-	while (pos < new_valid_size) {
-		u32 len;
-		struct folio *folio;
-		unsigned long off;
-
-		len = PAGE_SIZE - (pos & (PAGE_SIZE - 1));
-		if (pos + len > new_valid_size)
-			len = new_valid_size - pos;
-
-		err = ops->write_begin(NULL, mapping, pos, len, &folio, NULL);
-		if (err)
-			goto out;
-
-		off = offset_in_folio(folio, pos);
-		folio_zero_new_buffers(folio, off, off + len);
+	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 642

Quoted 10 lines
fs/exfat/file.c +5 -4
+	loff_t old_valid_size;
+	int ret = 0;
 
-		err = ops->write_end(NULL, mapping, pos, len, len, folio, NULL);
-		if (err < 0)
-			goto out;
-		pos += len;
+	mutex_lock(&sbi->s_lock);
+	old_valid_size = ei->valid_size;
+	mutex_unlock(&sbi->s_lock);

I’ve recently been refactoring around this lock :)

Quoted 215 lines
fs/exfat/file.c +11 -8
-		balance_dirty_pages_ratelimited(mapping);
-		cond_resched();
+	if (old_valid_size < off) {
+		ret = iomap_zero_range(inode, old_valid_size,
+				off - old_valid_size, NULL,
+				&exfat_write_iomap_ops, &exfat_iomap_folio_ops,
+				NULL);
+		if (!ret && bsync)
+			ret = filemap_write_and_wait_range(inode->i_mapping,
+					old_valid_size, off - 1);
  	}
 
-	return 0;
-
-out:
-	return err;
+	return ret;
  }
 
  static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
@@ -702,7 +688,7 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
  	}
 
  	if (pos > valid_size) {
-		ret = exfat_extend_valid_size(inode, pos);
+		ret = exfat_extend_valid_size(inode, pos, false);
  		if (ret < 0 && ret != -ENOSPC) {
  			exfat_err(inode->i_sb,
  				"write: fail to zero from %llu to %llu(%zd)",
@@ -760,7 +746,7 @@ static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
  	new_valid_size = min(new_valid_size, i_size_read(inode));
 
  	if (ei->valid_size < new_valid_size) {
-		err = exfat_extend_valid_size(inode, new_valid_size);
+		err = exfat_extend_valid_size(inode, new_valid_size, false);
  		if (err < 0) {
  			inode_unlock(inode);
  			return vmf_fs_error(err);
fs/exfat/inode.c +6 -3
index beb9ea7cca9f..cc54cce65a31 100644
@@ -123,8 +123,9 @@ void exfat_sync_inode(struct inode *inode)
   * Output: errcode, cluster number
   * *clu = (~0), if it's unable to allocate a new cluster
   */
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-		unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc)
  {
  	int ret;
  	unsigned int last_clu;
@@ -235,6 +236,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
  			}
  		}
  		*count = 1;
+		*balloc = true;
  	}
 
  	/* hint information */
@@ -258,6 +260,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
  	sector_t phys = 0;
  	sector_t valid_blks;
  	loff_t i_size;
+	bool balloc;
 
  	mutex_lock(&sbi->s_lock);
  	i_size = i_size_read(inode);
@@ -268,7 +271,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
  	/* Is this block already allocated? */
  	count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
  	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
-			&cluster, &count, create);
+			&cluster, &count, create, &balloc);
  	if (err) {
  		if (err != -ENOSPC)
  			exfat_fs_error_ratelimit(sb,
fs/exfat/iomap.c new file +129 -0
new file mode 100644
index 000000000000..e4135a13454f
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * iomap callack functions
+ *
+ * Copyright (C) 2026 Namjae Jeon <[email protected]>
+ */
+
+#include <linux/iomap.h>
+#include <linux/pagemap.h>
+
+#include "exfat_raw.h"
+#include "exfat_fs.h"
+#include "iomap.h"
+
+/*
+ * exfat_iomap_put_folio - Put folio after iomap operation
+ *
+ * Called when iomap is finished with a folio zero-fills portions of
+ * the folio beyond ->valid_size to prevent exposing uninitialized data.
+ */
+static void exfat_iomap_put_folio(struct inode *inode, loff_t pos,
+		unsigned int len, struct folio *folio)
+{
+	struct exfat_inode_info *ei = EXFAT_I(inode);
+	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
+	unsigned long sector_size = 1UL << inode->i_blkbits;
+	loff_t start_down, end_up, init;
+
+	mutex_lock(&sbi->s_lock);
+	start_down = round_down(pos, sector_size);
+	end_up = (pos + len - 1) | (sector_size - 1);
+	init = ei->valid_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);
+	mutex_unlock(&sbi->s_lock);
+}
+
+const struct iomap_write_ops exfat_iomap_folio_ops = {
+	.put_folio = exfat_iomap_put_folio,
+};
+
+/*
+ * exfat_file_write_dio_end_io - Direct I/O write completion handler
+ *
+ * Updates i_size if the write extended the file. Called from the dio layer
+ * after I/O completion.
+ */
+static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
+		int error, unsigned int flags)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+
+	if (error)
+		return error;
+
+	if (size && i_size_read(inode) < iocb->ki_pos + size) {
+		i_size_write(inode, iocb->ki_pos + size);
+		mark_inode_dirty(inode);
+	}
+
+	return 0;
+}
+
+const struct iomap_dio_ops exfat_write_dio_ops = {
+	.end_io		= exfat_file_write_dio_end_io,
+};
+
+/*
+ * exfat_read_iomap_begin - Begin mapping for reads
+ *
+ * Maps file range to disk location for read operations (read folio,
+ * readahead, direct I/O read, etc.).
+ *
+ * Returns IOMAP_MAPPED for areas within ->valid_size, and IOMAP_UNWRITTEN
+ * for allocated but uninitialized regions beyond ->valid_size.
+ */
+static int exfat_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
+		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
+{
+	struct super_block *sb = inode->i_sb;
+	struct exfat_sb_info *sbi = EXFAT_SB(sb);
+	struct exfat_inode_info *ei = EXFAT_I(inode);
+	unsigned int cluster, num_clusters = EXFAT_B_TO_CLU_ROUND_UP(length, sbi);
+	loff_t cluster_offset, cluster_length;
+	int err = 0;
+	bool balloc = false;
+
+	mutex_lock(&sbi->s_lock);
+	iomap->bdev = inode->i_sb->s_bdev;
+	iomap->offset = offset;
+
+	err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),

fs/exfat/iomap.c line 139

Quoted 10 lines
fs/exfat/iomap.c new file +10 -0
+			&cluster, &num_clusters, false, &balloc);
+	if (err)
+		goto out;
+
+	cluster_offset = EXFAT_CLU_OFFSET(offset, sbi);
+	cluster_length = EXFAT_CLU_TO_B(num_clusters, sbi);
+	if (length > cluster_length - cluster_offset)
+		iomap->length = cluster_length - cluster_offset;
+	else
+		iomap->length = length;

Using min here would be clearer

fs/exfat/iomap.c line 151

Quoted 12 lines
fs/exfat/iomap.c new file +12 -0
+
+	iomap->addr = exfat_cluster_to_phys(sbi, cluster) + cluster_offset;
+	if (offset >= ei->valid_size)
+		iomap->type = IOMAP_UNWRITTEN;
+	else
+		iomap->type = IOMAP_MAPPED;
+
+	if (!(flags & IOMAP_ZERO) && iomap->type == IOMAP_MAPPED &&
+	    iomap->offset < ei->valid_size &&
+	    iomap->offset + iomap->length > ei->valid_size) {
+		iomap->length = round_up(ei->valid_size, 1 << inode->i_blkbits) -
+			iomap->offset;

Aligning the end of the extent to the block size seems like a good option, since iomap can’t handle unaligned cases.

However, If valid_size isn’t block-aligned, then the extent will cover valid_size, and we should clear the region beyond valid_size, right?

But it seems that reads don’t do this, and they don’t call exfat_iomap_put_folio. Have I missed something?

Thanks,

Quoted 188 lines
fs/exfat/iomap.c new file +154 -0
+	}
+
+	iomap->flags |= IOMAP_F_MERGED;
+out:
+	mutex_unlock(&sbi->s_lock);
+	return err;
+}
+
+const struct iomap_ops exfat_read_iomap_ops = {
+	.iomap_begin = exfat_read_iomap_begin,
+};
+
+/*
+ * __exfat_write_iomap_begin - mapping logic for writes
+ *
+ * Maps the requested range and allocates clusters if needed.
+ */
+static int __exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, struct iomap *iomap)
+{
+	struct super_block *sb = inode->i_sb;
+	struct exfat_sb_info *sbi = EXFAT_SB(sb);
+	unsigned int cluster, num_clusters;
+	loff_t cluster_offset, cluster_length;
+	int err;
+	bool balloc = false;
+
+	num_clusters = max(EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) -
+		EXFAT_B_TO_CLU_ROUND_UP(offset, sbi), 1);
+	mutex_lock(&sbi->s_lock);
+	err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),
+			&cluster, &num_clusters, true, &balloc);
+	if (err)
+		goto out;
+
+	iomap->bdev = inode->i_sb->s_bdev;
+	iomap->offset = offset;
+
+	cluster_offset = EXFAT_CLU_OFFSET(offset, sbi);
+	cluster_length = EXFAT_CLU_TO_B(num_clusters, sbi);
+	if (length > cluster_length - cluster_offset)
+		iomap->length = cluster_length - cluster_offset;
+	else
+		iomap->length = length;
+	iomap->addr = exfat_cluster_to_phys(sbi, cluster) + cluster_offset;
+	iomap->type = IOMAP_MAPPED;
+	if (balloc)
+		iomap->flags = IOMAP_F_NEW;
+out:
+	mutex_unlock(&sbi->s_lock);
+	return err;
+}
+
+/*
+ * exfat_write_iomap_begin - Mapping for write operations
+ *
+ * Extends ->valid_size if the write starts beyond current initialized size.
+ * Then performs actual block mapping (possibly allocating clusters).
+ */
+static int exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
+{
+	int ret;
+
+	if (EXFAT_I(inode)->valid_size < offset) {
+		ret = exfat_extend_valid_size(inode, offset,
+				flags & IOMAP_DIRECT ? true : false);
+		if (ret)
+			return ret;
+	}
+
+	ret = __exfat_write_iomap_begin(inode, offset, length, iomap);
+
+	if (!(flags & IOMAP_DIRECT) && !ret &&
+	    i_size_read(inode) < iomap->offset + iomap->length) {
+		i_size_write(inode, iomap->offset + iomap->length);
+		mark_inode_dirty(inode);
+	}
+
+	return ret;
+}
+
+/*
+ * exfat_write_iomap_end - Update the state after write
+ *
+ * Extends ->valid_size to cover the newly written range.
+ * Marks the inode dirty if metadata was changed.
+ */
+static int exfat_write_iomap_end(struct inode *inode, loff_t pos, loff_t length,
+		ssize_t written, unsigned int flags, struct iomap *iomap)
+{
+	if (written) {
+		struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
+		struct exfat_inode_info *ei = EXFAT_I(inode);
+		bool dirtied = false;
+		loff_t end = pos + written;
+
+		mutex_lock(&sbi->s_lock);
+		if (ei->valid_size < end) {
+			ei->valid_size = end;
+			dirtied = true;
+		}
+		mutex_unlock(&sbi->s_lock);
+		if (dirtied)
+			mark_inode_dirty(inode);
+	}
+
+	return written;
+}
+
+const struct iomap_ops exfat_write_iomap_ops = {
+	.iomap_begin	= exfat_write_iomap_begin,
+	.iomap_end	= exfat_write_iomap_end,
+};
+
+static int exfat_mkwrite_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
+{
+	return __exfat_write_iomap_begin(inode, offset, length, iomap);
+}
+
+const struct iomap_ops exfat_mkwrite_iomap_ops = {
+	.iomap_begin	= exfat_mkwrite_iomap_begin,
+	.iomap_end	= exfat_write_iomap_end,
+};
+
+/*
+ * exfat_writeback_range - Map folio during writeback
+ *
+ * Called for each folio during writeback. If the folio falls outside the
+ * current iomap, remaps by calling read_iomap_begin.
+ */
+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) {
+		int error;
+
+		error = exfat_read_iomap_begin(wpc->inode, offset, len,
+				0, &wpc->iomap, NULL);
+		if (error)
+			return error;
+	}
+
+	return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
+}
+
+const struct iomap_writeback_ops exfat_writeback_ops = {
+	.writeback_range	= exfat_writeback_range,
+	.writeback_submit	= iomap_ioend_writeback_submit,
+};
fs/exfat/iomap.h new file +16 -0
new file mode 100644
index 000000000000..4abe0dc452ee
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2026 Namjae Jeon <[email protected]>
+ */
+
+#ifndef _LINUX_EXFAT_IOMAP_H
+#define _LINUX_EXFAT_IOMAP_H
+
+extern const struct iomap_write_ops exfat_iomap_folio_ops;
+extern const struct iomap_ops exfat_read_iomap_ops;
+extern const struct iomap_ops exfat_write_iomap_ops;
+extern const struct iomap_dio_ops exfat_write_dio_ops;
+extern const struct iomap_writeback_ops exfat_writeback_ops;
+extern const struct iomap_ops exfat_mkwrite_iomap_ops;
+
+#endif /* _LINUX_EXFAT_IOMAP_H */
fs/exfat/super.c +1 -0
index 83396fd265cd..b69c4b0a926b 100644
@@ -499,6 +499,7 @@ static int exfat_read_boot_sector(struct super_block *sb)
  	if (p_boot->num_fats == 2)
  		sbi->FAT2_start_sector += sbi->num_FAT_sectors;
  	sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
+	sbi->data_start_bytes = sbi->data_start_sector << p_boot->sect_size_bits;
  	sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
  	/* because the cluster index starts with 2 */
  	sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
author maintainer
#3 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Quoted 2 lines · Chi Zhiling

Hi, Namjae

Hi Chi,

Quoted 3 lines

Could you CC my personal email "[email protected]" when sending v2? since my work email can’t receive messages immediately.

Sure.

Quoted 5 lines

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 642

Quoted 3 lines
fs/exfat/file.c +3 -0
+     mutex_lock(&sbi->s_lock);
+     old_valid_size = ei->valid_size;
+     mutex_unlock(&sbi->s_lock);

I’ve recently been refactoring around this lock :)

Okay.

Quoted 8 lines

fs/exfat/iomap.c line 139

Quoted 6 lines
fs/exfat/iomap.c new file +6 -0
+     cluster_offset = EXFAT_CLU_OFFSET(offset, sbi);
+     cluster_length = EXFAT_CLU_TO_B(num_clusters, sbi);
+     if (length > cluster_length - cluster_offset)
+             iomap->length = cluster_length - cluster_offset;
+     else
+             iomap->length = length;

Using min here would be clearer

Okay.

Quoted 17 lines

fs/exfat/iomap.c line 151

Quoted 13 lines
fs/exfat/iomap.c new file +12 -0
+
+     iomap->addr = exfat_cluster_to_phys(sbi, cluster) + cluster_offset;
+     if (offset >= ei->valid_size)
+             iomap->type = IOMAP_UNWRITTEN;
+     else
+             iomap->type = IOMAP_MAPPED;
+
+     if (!(flags & IOMAP_ZERO) && iomap->type == IOMAP_MAPPED &&
+         iomap->offset < ei->valid_size &&
+         iomap->offset + iomap->length > ei->valid_size) {
+             iomap->length = round_up(ei->valid_size, 1 << inode->i_blkbits) -
+                     iomap->offset;

Aligning the end of the extent to the block size seems like a good option, since iomap can’t handle unaligned cases.

Quoted 6 lines

However, If valid_size isn’t block-aligned, then the extent will cover valid_size, and we should clear the region beyond valid_size, right?

But it seems that reads don’t do this, and they don’t call exfat_iomap_put_folio.
Have I missed something?

You seem right, I will check it more.
Thanks for your review:)

#4 lore raw

Re: [PATCH 1/5] exfat: add iomap support

In reply to Namjae Jeon

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 633

Quoted 3 lines
fs/exfat/file.c +1 -1
-static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size)
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)

This looks like at least partially unrelated refactoring. Can you split this out into a separate well-documented patch, or maybe even two where one has the bulk reformatting changes, and one adds the new bsync option? Also it might help to document the bsync option. Often it might be more readable to add a flags parameter with a named flag to make things easier to follow.

fs/exfat/inode.c void exfat_sync_inode(struct inode *inode) · line 128

Quoted 5 lines
fs/exfat/inode.c +3 -2
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-		unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc)

Similarly, this would be a good prep patch.

fs/exfat/iomap.c line 6

Quoted 12 lines
fs/exfat/iomap.c new file +6 -0
new file mode 100644
index 000000000000..e4135a13454f
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * iomap callack functions
+ *
+ * Copyright (C) 2026 Namjae Jeon <[email protected]>
+ */

Also normally new infrastructure would get added with the users. I.e. the bits you're using in the first iomap conversion would go with that and so on. But that's not a strict rule.

fs/exfat/iomap.c line 22

Quoted 8 lines
fs/exfat/iomap.c new file +8 -0
+/*
+ * exfat_iomap_put_folio - Put folio after iomap operation
+ *
+ * Called when iomap is finished with a folio zero-fills portions of
+ * the folio beyond ->valid_size to prevent exposing uninitialized data.
+ */
+static void exfat_iomap_put_folio(struct inode *inode, loff_t pos,
+		unsigned int len, struct folio *folio)

Can you explain the logic here? Shouldn't the iomap buffered I/O code do all the needed zeroing for you based on the map type? If not how could we enhance the core iomap code so that we don't need this in the file system, which feels like a bit of break of abstraction barriers?

Quoted 11 lines
fs/exfat/iomap.c new file +11 -0
+/*
+ * exfat_file_write_dio_end_io - Direct I/O write completion handler
+ *
+ * Updates i_size if the write extended the file. Called from the dio layer
+ * after I/O completion.
+ */
+static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
+		int error, unsigned int flags)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+

fs/exfat/iomap.c line 99

Quoted 10 lines
fs/exfat/iomap.c new file +10 -0
+	if (error)
+		return error;
+
+	if (size && i_size_read(inode) < iocb->ki_pos + size) {
+		i_size_write(inode, iocb->ki_pos + size);
+		mark_inode_dirty(inode);
+	}
+
+	return 0;
+}

I think in the long run we should just do this as the default in the core iomap dio code when no end_io routine is provided for writes. But I can refactor this later to not hold you up.

fs/exfat/iomap.c line 116

Quoted 3 lines
fs/exfat/iomap.c new file +3 -0
+static int exfat_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
+		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
+{

The read and write routines look very similar, and chance we could extra most of the logic into a common helper? Also the rounding of the length using the start in the write version looks like it would be the right thing for the read side anyway.

fs/exfat/iomap.c line 273

Quoted 6 lines
fs/exfat/iomap.c new file +6 -0
+static int exfat_mkwrite_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
+{
+	return __exfat_write_iomap_begin(inode, offset, length, iomap);
+}

The special mkwrite handling looks a bit odd to me. I'll return to that later in the series, but this is a good example where keeping all the related code together would help the reviewer.

fs/exfat/iomap.c line 294

Quoted 6 lines
fs/exfat/iomap.c new file +6 -0
+	if (offset < wpc->iomap.offset ||
+	    offset >= wpc->iomap.offset + wpc->iomap.length) {
+		int error;
+
+		error = exfat_read_iomap_begin(wpc->inode, offset, len,
+				0, &wpc->iomap, NULL);

The read confused me a bit here, but I guess by the time we do writeback everything is allocated in exfat. This would be another good candidate to directly call the low-level helper suggested above.

fs/exfat/super.c static int exfat_read_boot_sector(struct super_block *sb) · line 502

Quoted 9 lines
fs/exfat/super.c +1 -0
index 83396fd265cd..b69c4b0a926b 100644
@@ -499,6 +499,7 @@ static int exfat_read_boot_sector(struct super_block *sb)
 	if (p_boot->num_fats == 2)
 		sbi->FAT2_start_sector += sbi->num_FAT_sectors;
 	sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
+	sbi->data_start_bytes = sbi->data_start_sector << p_boot->sect_size_bits;

Is this related to iomap?

author maintainer
#5 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Quoted 11 lines · Christoph Hellwig

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 633

Quoted 4 lines
fs/exfat/file.c +1 -1
-static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size)
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)

This looks like at least partially unrelated refactoring. Can you split this out into a separate well-documented patch, or maybe even two where one has the bulk reformatting changes, and one adds the new bsync option? Also it might help to document the bsync option. Often it might be more readable to add a flags parameter with a named flag to make things easier to follow.

Okay. I'll split it out into a separate patch and add a flags parameter with documentation.

Quoted 8 lines

fs/exfat/inode.c void exfat_sync_inode(struct inode *inode) · line 128

Quoted 6 lines
fs/exfat/inode.c +3 -2
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-             unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+             unsigned int *clu, unsigned int *count, int create,
+             bool *balloc)

Similarly, this would be a good prep patch.

Okay.

Quoted 17 lines

fs/exfat/iomap.c line 6

Quoted 13 lines
fs/exfat/iomap.c new file +6 -0
new file mode 100644
index 000000000000..e4135a13454f
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * iomap callack functions
+ *
+ * Copyright (C) 2026 Namjae Jeon <[email protected]>
+ */

Also normally new infrastructure would get added with the users. I.e. the bits you're using in the first iomap conversion would go with that and so on. But that's not a strict rule.

I'll try to split the introduction of the iomap infrastructure so that only the parts actually used in each conversion step are added together with the users.

Quoted 15 lines

fs/exfat/iomap.c line 22

Quoted 9 lines
fs/exfat/iomap.c new file +8 -0
+/*
+ * exfat_iomap_put_folio - Put folio after iomap operation
+ *
+ * Called when iomap is finished with a folio zero-fills portions of
+ * the folio beyond ->valid_size to prevent exposing uninitialized data.
+ */
+static void exfat_iomap_put_folio(struct inode *inode, loff_t pos,
+             unsigned int len, struct folio *folio)

Can you explain the logic here? Shouldn't the iomap buffered I/O code do all the needed zeroing for you based on the map type? If not how could we enhance the core iomap code so that we don't need this in the file system, which feels like a bit of break of abstraction barriers?

The reason we have exfat_iomap_put_folio() is because of exFAT's VDL(->valid_size). When we map a range beyond ->valid_size, if we set the map type to IOMAP_UNWRITTEN, iomap_zero_range() does not perform zeroing.
However, if we set it to IOMAP_MAPPED, then iomap_zero_range() treats the area beyond ->valid_size as valid data and could expose uninitialized garbage data from disk. So it explicitly zero out the portion beyond ->valid_size in exfat_iomap_put_folio().
I agree that it would be better if core iomap could handle this case by itself. I'll check whether we can improve the core iomap layer to avoid filesystem-specific put_folio handling for ->valid_size.

Quoted 18 lines
Quoted 18 lines
fs/exfat/iomap.c new file +17 -0
+/*
+ * exfat_file_write_dio_end_io - Direct I/O write completion handler
+ *
+ * Updates i_size if the write extended the file. Called from the dio layer
+ * after I/O completion.
+ */
+static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
+             int error, unsigned int flags)
+{
+     struct inode *inode = file_inode(iocb->ki_filp);
+
+     if (error)
+             return error;
+
+     if (size && i_size_read(inode) < iocb->ki_pos + size) {
+             i_size_write(inode, iocb->ki_pos + size);
+             mark_inode_dirty(inode);
Quoted 8 lines

fs/exfat/iomap.c line 99

Quoted 4 lines
fs/exfat/iomap.c new file +4 -0
+     }
+
+     return 0;
+}

I think in the long run we should just do this as the default in the core iomap dio code when no end_io routine is provided for writes. But I can refactor this later to not hold you up.

Okay, Thanks!

Quoted 9 lines

fs/exfat/iomap.c line 116

Quoted 4 lines
fs/exfat/iomap.c new file +3 -0
+static int exfat_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
+             unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
+{

The read and write routines look very similar, and chance we could extra most of the logic into a common helper? Also the rounding of the length using the start in the write version looks like it would be the right thing for the read side anyway.

Okay. I'll factor out the common logic into a helper.

Quoted 11 lines

fs/exfat/iomap.c line 273

Quoted 7 lines
fs/exfat/iomap.c new file +6 -0
+static int exfat_mkwrite_iomap_begin(struct inode *inode, loff_t offset,
+             loff_t length, unsigned int flags, struct iomap *iomap,
+             struct iomap *srcmap)
+{
+     return __exfat_write_iomap_begin(inode, offset, length, iomap);
+}

The special mkwrite handling looks a bit odd to me. I'll return to that later in the series, but this is a good example where keeping all the related code together would help the reviewer.

Okay. I will fix it.

Quoted 11 lines

fs/exfat/iomap.c line 294

Quoted 7 lines
fs/exfat/iomap.c new file +6 -0
+     if (offset < wpc->iomap.offset ||
+         offset >= wpc->iomap.offset + wpc->iomap.length) {
+             int error;
+
+             error = exfat_read_iomap_begin(wpc->inode, offset, len,
+                             0, &wpc->iomap, NULL);

The read confused me a bit here, but I guess by the time we do writeback everything is allocated in exfat. This would be another good candidate to directly call the low-level helper suggested above.

Agreed. As you suggested, I'll change it to directly call the low-level common helper.

Quoted 12 lines

fs/exfat/super.c static int exfat_read_boot_sector(struct super_block *sb) · line 502

Quoted 10 lines
fs/exfat/super.c +1 -0
index 83396fd265cd..b69c4b0a926b 100644
@@ -499,6 +499,7 @@ static int exfat_read_boot_sector(struct super_block *sb)
      if (p_boot->num_fats == 2)
              sbi->FAT2_start_sector += sbi->num_FAT_sectors;
      sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
+     sbi->data_start_bytes = sbi->data_start_sector << p_boot->sect_size_bits;

Is this related to iomap?

Yes, I'll split it out into a separate prep patch together with the other related changes, so it will be clearer.
Thanks for your review!

Quoted 1 line
#6 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Quoted 5 lines · Namjae Jeon
Quoted 5 lines

Can you explain the logic here? Shouldn't the iomap buffered I/O code do all the needed zeroing for you based on the map type? If not how could we enhance the core iomap code so that we don't need this in the file system, which feels like a bit of break of abstraction barriers?

Quoted 11 lines

The reason we have exfat_iomap_put_folio() is because of exFAT's VDL(->valid_size). When we map a range beyond ->valid_size, if we set the map type to IOMAP_UNWRITTEN, iomap_zero_range() does not perform zeroing.
However, if we set it to IOMAP_MAPPED, then iomap_zero_range() treats the area beyond ->valid_size as valid data and could expose uninitialized garbage data from disk. So it explicitly zero out the portion beyond ->valid_size in exfat_iomap_put_folio().
I agree that it would be better if core iomap could handle this case by itself. I'll check whether we can improve the core iomap layer to avoid filesystem-specific put_folio handling for ->valid_size.

I suspect just adding a new IOMAP_ type for this should be fine, as the above description sounds coherent enough to add it to documentation. I just can't really think of a good name.

author maintainer
#7 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Quoted 19 lines · Christoph Hellwig
Quoted 5 lines · Namjae Jeon
Quoted 5 lines

Can you explain the logic here? Shouldn't the iomap buffered I/O code do all the needed zeroing for you based on the map type? If not how could we enhance the core iomap code so that we don't need this in the file system, which feels like a bit of break of abstraction barriers?

Quoted 11 lines

The reason we have exfat_iomap_put_folio() is because of exFAT's VDL(->valid_size). When we map a range beyond ->valid_size, if we set the map type to IOMAP_UNWRITTEN, iomap_zero_range() does not perform zeroing.
However, if we set it to IOMAP_MAPPED, then iomap_zero_range() treats the area beyond ->valid_size as valid data and could expose uninitialized garbage data from disk. So it explicitly zero out the portion beyond ->valid_size in exfat_iomap_put_folio().
I agree that it would be better if core iomap could handle this case by itself. I'll check whether we can improve the core iomap layer to avoid filesystem-specific put_folio handling for ->valid_size.

Quoted 3 lines

I suspect just adding a new IOMAP_ type for this should be fine, as the above description sounds coherent enough to add it to documentation. I just can't really think of a good name.

Let me check it:)
Thanks for the feedback.

#8 lore raw

Re: [PATCH 1/5] exfat: add iomap support

In reply to Namjae Jeon
Quoted 58 lines · Namjae Jeon
Quoted 11 lines · Christoph Hellwig

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 633

Quoted 4 lines
fs/exfat/file.c +1 -1
-static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size)
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)

This looks like at least partially unrelated refactoring. Can you split this out into a separate well-documented patch, or maybe even two where one has the bulk reformatting changes, and one adds the new bsync option? Also it might help to document the bsync option. Often it might be more readable to add a flags parameter with a named flag to make things easier to follow.

Okay. I'll split it out into a separate patch and add a flags parameter with documentation.

Quoted 8 lines

fs/exfat/inode.c void exfat_sync_inode(struct inode *inode) · line 128

Quoted 6 lines
fs/exfat/inode.c +3 -2
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-             unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+             unsigned int *clu, unsigned int *count, int create,
+             bool *balloc)

Similarly, this would be a good prep patch.

Okay.

Quoted 17 lines

fs/exfat/iomap.c line 6

Quoted 13 lines
fs/exfat/iomap.c new file +6 -0
new file mode 100644
index 000000000000..e4135a13454f
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * iomap callack functions
+ *
+ * Copyright (C) 2026 Namjae Jeon <[email protected]>
+ */

Also normally new infrastructure would get added with the users. I.e. the bits you're using in the first iomap conversion would go with that and so on. But that's not a strict rule.

I'll try to split the introduction of the iomap infrastructure so that only the parts actually used in each conversion step are added together with the users.

Quoted 15 lines

fs/exfat/iomap.c line 22

Quoted 9 lines
fs/exfat/iomap.c new file +8 -0
+/*
+ * exfat_iomap_put_folio - Put folio after iomap operation
+ *
+ * Called when iomap is finished with a folio zero-fills portions of
+ * the folio beyond ->valid_size to prevent exposing uninitialized data.
+ */
+static void exfat_iomap_put_folio(struct inode *inode, loff_t pos,
+             unsigned int len, struct folio *folio)

Can you explain the logic here? Shouldn't the iomap buffered I/O code do all the needed zeroing for you based on the map type? If not how could we enhance the core iomap code so that we don't need this in the file system, which feels like a bit of break of abstraction barriers?

Quoted 10 lines

The reason we have exfat_iomap_put_folio() is because of exFAT's VDL(->valid_size). When we map a range beyond ->valid_size, if we set the map type to IOMAP_UNWRITTEN, iomap_zero_range() does not perform zeroing.> However, if we set it to IOMAP_MAPPED, then iomap_zero_range() treats the area beyond ->valid_size as valid data and could expose uninitialized garbage data from disk. So it explicitly zero out the portion beyond ->valid_size in exfat_iomap_put_folio().
I agree that it would be better if core iomap could handle this case by itself. I'll check whether we can improve the core iomap layer to avoid filesystem-specific put_folio handling for ->valid_size.

Perhaps all we need is to use a new iomap_ops in exfat_extend_valid_size that treats regions beyond ->valid_size as mapped.

And call exfat_extend_valid_size in advance to extend valid_size so that it is aligned to the block size before reads and writes.

Just for what it's worth

Thanks,

#9 lore raw

Re: [PATCH 1/5] exfat: add iomap support

In reply to Namjae Jeon
Quoted 5 lines
fs/exfat/iomap.c new file +5 -0
+static int __exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+               loff_t length, struct iomap *iomap)
+{
+       struct super_block *sb = inode->i_sb;
+       struct exfat_sb_info *sbi = EXFAT_SB(sb);

fs/exfat/iomap.c line 183

Quoted 10 lines
fs/exfat/iomap.c new file +10 -0
+       unsigned int cluster, num_clusters;
+       loff_t cluster_offset, cluster_length;
+       int err;
+       bool balloc = false;
+
+       num_clusters = max(EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) -
+               EXFAT_B_TO_CLU_ROUND_UP(offset, sbi), 1);
+       mutex_lock(&sbi->s_lock);
+       err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),
+                       &cluster, &num_clusters, true, &balloc);

num_clusters is the number of clusters which the region [offset, offset+length] is distributed.

num_clusters should be set as follows in both __exfat_write_iomap_begin and exfat_read_iomap_begin.

  num_clusters = EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) - EXFAT_B_TO_CLU(offset, sbi);

And num_to_be_allocated in exfat_map_cluster() should be set as follows.

     num_to_be_allocated = 0;
     if (clu_offset + *count > num_clusters)
             num_to_be_allocated = clu_offset + *count - num_clusters;
author maintainer
#10 lore raw

Re: [PATCH 1/5] exfat: add iomap support

Quoted 23 lines · On Wed, Apr 1, 2026 at 11:24 AM [email protected] <[email protected]>…
Quoted 6 lines
fs/exfat/iomap.c new file +5 -0
+static int __exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+               loff_t length, struct iomap *iomap)
+{
+       struct super_block *sb = inode->i_sb;
+       struct exfat_sb_info *sbi = EXFAT_SB(sb);

fs/exfat/iomap.c line 183

Quoted 10 lines
fs/exfat/iomap.c new file +10 -0
+       unsigned int cluster, num_clusters;
+       loff_t cluster_offset, cluster_length;
+       int err;
+       bool balloc = false;
+
+       num_clusters = max(EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) -
+               EXFAT_B_TO_CLU_ROUND_UP(offset, sbi), 1);
+       mutex_lock(&sbi->s_lock);
+       err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),
+                       &cluster, &num_clusters, true, &balloc);

num_clusters is the number of clusters which the region [offset, offset+length] is distributed.

num_clusters should be set as follows in both __exfat_write_iomap_begin and exfat_read_iomap_begin.

Quoted 7 lines
  num_clusters = EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) - EXFAT_B_TO_CLU(offset, sbi);

And num_to_be_allocated in exfat_map_cluster() should be set as follows.

     num_to_be_allocated = 0;
     if (clu_offset + *count > num_clusters)
             num_to_be_allocated = clu_offset + *count - num_clusters;

Okay, I will update it on the next version.
Thanks for the review:)

#11 lore raw

Re: [PATCH 1/5] exfat: add iomap support

In reply to Namjae Jeon
Quoted 442 lines · Namjae Jeon

Add iomap support to the exfat filesystem. This patch introduces the necessary iomap infrastructure by adding a new iomap.c file and related iomap operations. The main change is converting exfat_extend_valid_size() to use iomap_zero_range() instead of the legacy write_begin/write_end path. To support this, exfat_map_cluster() is extended to return whether a new cluster was allocated via a balloc flag, and a new helper function exfat_cluster_to_phys() is added. Also, data_start_bytes is added to struct exfat_sb_info for easier conversion from cluster number to physical byte offset.

---
 fs/exfat/Makefile   |   2 +-
 fs/exfat/exfat_fs.h |  12 ++
 fs/exfat/file.c     |  54 +++-----
 fs/exfat/inode.c    |   9 +-
 fs/exfat/iomap.c    | 305 ++++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/iomap.h    |  16 +++
 fs/exfat/super.c    |   1 +
 7 files changed, 361 insertions(+), 38 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
fs/exfat/Makefile +1 -1
index ed51926a4971..e06bf85870ae 100644
@@ -5,4 +5,4 @@
 obj-$(CONFIG_EXFAT_FS) += exfat.o
 
 exfat-y	:= inode.o namei.o dir.o super.o fatent.o cache.o nls.o misc.o \
-	   file.o balloc.o
+	   file.o balloc.o iomap.o
fs/exfat/exfat_fs.h +12 -0
index 9fed9fb33cae..860f2e438b63 100644
@@ -259,6 +259,7 @@ struct exfat_sb_info {
 	unsigned long long FAT1_start_sector; /* FAT1 start sector */
 	unsigned long long FAT2_start_sector; /* FAT2 start sector */
 	unsigned long long data_start_sector; /* data area start sector */
+	unsigned long long data_start_bytes;
 	unsigned int num_FAT_sectors; /* num of FAT sectors */
 	unsigned int root_dir; /* root dir cluster */
 	unsigned int dentries_per_clu; /* num of dentries per cluster */
@@ -432,6 +433,13 @@ static inline loff_t exfat_ondisk_size(const struct inode *inode)
 	return ((loff_t)inode->i_blocks) << 9;
 }
 
+static inline loff_t exfat_cluster_to_phys(struct exfat_sb_info *sbi,
+		unsigned int clus)
+{
+	return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bits) +
+		sbi->data_start_bytes;
+}
+
 /* super.c */
 int exfat_set_volume_dirty(struct super_block *sb);
 int exfat_clear_volume_dirty(struct super_block *sb);
@@ -480,6 +488,7 @@ long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
 				unsigned long arg);
 int exfat_force_shutdown(struct super_block *sb, u32 flags);
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync);
 
 /* namei.c */
 extern const struct dentry_operations exfat_dentry_ops;
@@ -543,6 +552,9 @@ int __exfat_write_inode(struct inode *inode, int sync);
 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
 void exfat_evict_inode(struct inode *inode);
 int exfat_block_truncate_page(struct inode *inode, loff_t from);
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc);
 
 /* exfat/nls.c */
 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
fs/exfat/file.c +20 -34
index 2daf0dbabb24..756846b774c4 100644
@@ -14,9 +14,11 @@
 #include <linux/writeback.h>
 #include <linux/filelock.h>
 #include <linux/falloc.h>
+#include <linux/iomap.h>
 
 #include "exfat_raw.h"
 #include "exfat_fs.h"
+#include "iomap.h"
 
 static int exfat_cont_expand(struct inode *inode, loff_t size)
 {
@@ -628,44 +630,28 @@ int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 	return blkdev_issue_flush(inode->i_sb->s_bdev);
 }
 
-static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size)
+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)
 {
-	int err;
-	loff_t pos;
 	struct exfat_inode_info *ei = EXFAT_I(inode);
-	struct address_space *mapping = inode->i_mapping;
-	const struct address_space_operations *ops = mapping->a_ops;
-
-	pos = ei->valid_size;
-	while (pos < new_valid_size) {
-		u32 len;
-		struct folio *folio;
-		unsigned long off;
-
-		len = PAGE_SIZE - (pos & (PAGE_SIZE - 1));
-		if (pos + len > new_valid_size)
-			len = new_valid_size - pos;
-
-		err = ops->write_begin(NULL, mapping, pos, len, &folio, NULL);
-		if (err)
-			goto out;
-
-		off = offset_in_folio(folio, pos);
-		folio_zero_new_buffers(folio, off, off + len);
+	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
+	loff_t old_valid_size;
+	int ret = 0;
 
-		err = ops->write_end(NULL, mapping, pos, len, len, folio, NULL);
-		if (err < 0)
-			goto out;
-		pos += len;
+	mutex_lock(&sbi->s_lock);
+	old_valid_size = ei->valid_size;
+	mutex_unlock(&sbi->s_lock);
 
-		balance_dirty_pages_ratelimited(mapping);
-		cond_resched();
+	if (old_valid_size < off) {
+		ret = iomap_zero_range(inode, old_valid_size,
+				off - old_valid_size, NULL,
+				&exfat_write_iomap_ops, &exfat_iomap_folio_ops,
+				NULL);
+		if (!ret && bsync)
+			ret = filemap_write_and_wait_range(inode->i_mapping,
+					old_valid_size, off - 1);
 	}
 
-	return 0;
-
-out:
-	return err;
+	return ret;
 }
 
 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
@@ -702,7 +688,7 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 	}
 
 	if (pos > valid_size) {
-		ret = exfat_extend_valid_size(inode, pos);
+		ret = exfat_extend_valid_size(inode, pos, false);
 		if (ret < 0 && ret != -ENOSPC) {
 			exfat_err(inode->i_sb,
 				"write: fail to zero from %llu to %llu(%zd)",
@@ -760,7 +746,7 @@ static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
 	new_valid_size = min(new_valid_size, i_size_read(inode));
 
 	if (ei->valid_size < new_valid_size) {
-		err = exfat_extend_valid_size(inode, new_valid_size);
+		err = exfat_extend_valid_size(inode, new_valid_size, false);
 		if (err < 0) {
 			inode_unlock(inode);
 			return vmf_fs_error(err);
fs/exfat/inode.c +6 -3
index beb9ea7cca9f..cc54cce65a31 100644
@@ -123,8 +123,9 @@ void exfat_sync_inode(struct inode *inode)
  * Output: errcode, cluster number
  * *clu = (~0), if it's unable to allocate a new cluster
  */
-static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
-		unsigned int *clu, unsigned int *count, int create)
+int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc)
 {
 	int ret;
 	unsigned int last_clu;
@@ -235,6 +236,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 			}
 		}
 		*count = 1;
+		*balloc = true;
 	}
 
 	/* hint information */
@@ -258,6 +260,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	sector_t phys = 0;
 	sector_t valid_blks;
 	loff_t i_size;
+	bool balloc;
 
 	mutex_lock(&sbi->s_lock);
 	i_size = i_size_read(inode);
@@ -268,7 +271,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	/* Is this block already allocated? */
 	count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
 	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
-			&cluster, &count, create);
+			&cluster, &count, create, &balloc);
 	if (err) {
 		if (err != -ENOSPC)
 			exfat_fs_error_ratelimit(sb,
fs/exfat/iomap.c new file +223 -0
new file mode 100644
index 000000000000..e4135a13454f
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * iomap callack functions
+ *
+ * Copyright (C) 2026 Namjae Jeon <[email protected]>
+ */
+
+#include <linux/iomap.h>
+#include <linux/pagemap.h>
+
+#include "exfat_raw.h"
+#include "exfat_fs.h"
+#include "iomap.h"
+
+/*
+ * exfat_iomap_put_folio - Put folio after iomap operation
+ *
+ * Called when iomap is finished with a folio zero-fills portions of
+ * the folio beyond ->valid_size to prevent exposing uninitialized data.
+ */
+static void exfat_iomap_put_folio(struct inode *inode, loff_t pos,
+		unsigned int len, struct folio *folio)
+{
+	struct exfat_inode_info *ei = EXFAT_I(inode);
+	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
+	unsigned long sector_size = 1UL << inode->i_blkbits;
+	loff_t start_down, end_up, init;
+
+	mutex_lock(&sbi->s_lock);
+	start_down = round_down(pos, sector_size);
+	end_up = (pos + len - 1) | (sector_size - 1);
+	init = ei->valid_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);
+	mutex_unlock(&sbi->s_lock);
+}
+
+const struct iomap_write_ops exfat_iomap_folio_ops = {
+	.put_folio = exfat_iomap_put_folio,
+};
+
+/*
+ * exfat_file_write_dio_end_io - Direct I/O write completion handler
+ *
+ * Updates i_size if the write extended the file. Called from the dio layer
+ * after I/O completion.
+ */
+static int exfat_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
+		int error, unsigned int flags)
+{
+	struct inode *inode = file_inode(iocb->ki_filp);
+
+	if (error)
+		return error;
+
+	if (size && i_size_read(inode) < iocb->ki_pos + size) {
+		i_size_write(inode, iocb->ki_pos + size);
+		mark_inode_dirty(inode);
+	}
+
+	return 0;
+}
+
+const struct iomap_dio_ops exfat_write_dio_ops = {
+	.end_io		= exfat_file_write_dio_end_io,
+};
+
+/*
+ * exfat_read_iomap_begin - Begin mapping for reads
+ *
+ * Maps file range to disk location for read operations (read folio,
+ * readahead, direct I/O read, etc.).
+ *
+ * Returns IOMAP_MAPPED for areas within ->valid_size, and IOMAP_UNWRITTEN
+ * for allocated but uninitialized regions beyond ->valid_size.
+ */
+static int exfat_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
+		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
+{
+	struct super_block *sb = inode->i_sb;
+	struct exfat_sb_info *sbi = EXFAT_SB(sb);
+	struct exfat_inode_info *ei = EXFAT_I(inode);
+	unsigned int cluster, num_clusters = EXFAT_B_TO_CLU_ROUND_UP(length, sbi);
+	loff_t cluster_offset, cluster_length;
+	int err = 0;
+	bool balloc = false;
+
+	mutex_lock(&sbi->s_lock);
+	iomap->bdev = inode->i_sb->s_bdev;
+	iomap->offset = offset;
+
+	err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),
+			&cluster, &num_clusters, false, &balloc);
+	if (err)
+		goto out;
+
+	cluster_offset = EXFAT_CLU_OFFSET(offset, sbi);
+	cluster_length = EXFAT_CLU_TO_B(num_clusters, sbi);
+	if (length > cluster_length - cluster_offset)
+		iomap->length = cluster_length - cluster_offset;
+	else
+		iomap->length = length;
+
+	iomap->addr = exfat_cluster_to_phys(sbi, cluster) + cluster_offset;
+	if (offset >= ei->valid_size)
+		iomap->type = IOMAP_UNWRITTEN;
+	else
+		iomap->type = IOMAP_MAPPED;
+
+	if (!(flags & IOMAP_ZERO) && iomap->type == IOMAP_MAPPED &&
+	    iomap->offset < ei->valid_size &&
+	    iomap->offset + iomap->length > ei->valid_size) {
+		iomap->length = round_up(ei->valid_size, 1 << inode->i_blkbits) -
+			iomap->offset;
+	}
+
+	iomap->flags |= IOMAP_F_MERGED;
+out:
+	mutex_unlock(&sbi->s_lock);
+	return err;
+}
+
+const struct iomap_ops exfat_read_iomap_ops = {
+	.iomap_begin = exfat_read_iomap_begin,
+};
+
+/*
+ * __exfat_write_iomap_begin - mapping logic for writes
+ *
+ * Maps the requested range and allocates clusters if needed.
+ */
+static int __exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, struct iomap *iomap)
+{
+	struct super_block *sb = inode->i_sb;
+	struct exfat_sb_info *sbi = EXFAT_SB(sb);
+	unsigned int cluster, num_clusters;
+	loff_t cluster_offset, cluster_length;
+	int err;
+	bool balloc = false;
+
+	num_clusters = max(EXFAT_B_TO_CLU_ROUND_UP(offset + length, sbi) -
+		EXFAT_B_TO_CLU_ROUND_UP(offset, sbi), 1);
+	mutex_lock(&sbi->s_lock);
+	err = exfat_map_cluster(inode, EXFAT_B_TO_CLU(offset, sbi),
+			&cluster, &num_clusters, true, &balloc);
+	if (err)
+		goto out;
+
+	iomap->bdev = inode->i_sb->s_bdev;
+	iomap->offset = offset;
+
+	cluster_offset = EXFAT_CLU_OFFSET(offset, sbi);
+	cluster_length = EXFAT_CLU_TO_B(num_clusters, sbi);
+	if (length > cluster_length - cluster_offset)
+		iomap->length = cluster_length - cluster_offset;
+	else
+		iomap->length = length;
+	iomap->addr = exfat_cluster_to_phys(sbi, cluster) + cluster_offset;
+	iomap->type = IOMAP_MAPPED;
+	if (balloc)
+		iomap->flags = IOMAP_F_NEW;
+out:
+	mutex_unlock(&sbi->s_lock);
+	return err;
+}
+
+/*
+ * exfat_write_iomap_begin - Mapping for write operations
+ *
+ * Extends ->valid_size if the write starts beyond current initialized size.
+ * Then performs actual block mapping (possibly allocating clusters).
+ */
+static int exfat_write_iomap_begin(struct inode *inode, loff_t offset,
+		loff_t length, unsigned int flags, struct iomap *iomap,
+		struct iomap *srcmap)
+{
+	int ret;
+
+	if (EXFAT_I(inode)->valid_size < offset) {
+		ret = exfat_extend_valid_size(inode, offset,
+				flags & IOMAP_DIRECT ? true : false);
+		if (ret)
+			return ret;
+	}
+

fs/exfat/iomap.c line 233

Quoted 10 lines
fs/exfat/iomap.c new file +10 -0
+	ret = __exfat_write_iomap_begin(inode, offset, length, iomap);
+
+	if (!(flags & IOMAP_DIRECT) && !ret &&
+	    i_size_read(inode) < iomap->offset + iomap->length) {
+		i_size_write(inode, iomap->offset + iomap->length);
+		mark_inode_dirty(inode);
+	}
+
+	return ret;
+}

Might be a stupid question but, why should this be handled differently from mkwrite? Also, dirtying the inode here seems redundant. If this is for the device going away due to power/link failure, I wonder if the inode would get the chance to be synced between begin() and end() for this to be effective. Sure, the new clusters are allocated for the mapping, but before end() is called, what's the point in updating the isize if there's no data written to the new clusters, yet?

I don't think it matters that much anyways since exFAT is not journalled. I'm only pointing this out because mark_inode_dirty() is not a cheap operation.

#12 lore raw

Re: [PATCH 1/5] exfat: add iomap support

In reply to Namjae Jeon
Quoted 1 line · Namjae Jeon

+int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync);

Since exFAT is fully embracing bool from now on, would you consider refactoring the entire exFAT codebase to using bool where fit to cut down on stack usage and for better readibility?

int and unsigned char being used as bool in the code copied and pasted from vfat has been bugging me for a while.

author maintainer
#13 lore raw

PATCH 2/5 exfat: add iomap direct I/O support

In reply to Namjae Jeon

Add iomap-based direct I/O support to the exfat filesystem. This replaces the previous exfat_direct_IO() implementation that used blockdev_direct_IO() with the modern iomap_dio_rw() interface.

---
 fs/exfat/file.c  | 30 ++++++++++++++++++++++++++++--
 fs/exfat/inode.c | 45 +--------------------------------------------
 2 files changed, 29 insertions(+), 46 deletions(-)
fs/exfat/file.c +28 -2
index 756846b774c4..2a9263b4433b 100644
@@ -698,7 +698,13 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 			goto unlock;
 	}
 
-	ret = __generic_file_write_iter(iocb, iter);
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		ret = iomap_dio_rw(iocb, iter, &exfat_write_iomap_ops,
+				&exfat_write_dio_ops, 0, NULL, 0);
+		if (ret == -ENOTBLK)
+			ret = 0;
+	} else
+		ret = __generic_file_write_iter(iocb, iter);
 	if (ret < 0)
 		goto unlock;
 
@@ -725,11 +731,31 @@ static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
 static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 {
 	struct inode *inode = file_inode(iocb->ki_filp);
+	ssize_t ret;
 
 	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
 		return -EIO;
 
-	return generic_file_read_iter(iocb, iter);
+	inode_lock_shared(inode);
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		size_t count = iov_iter_count(iter);
+
+		if ((iocb->ki_pos | count) & (inode->i_sb->s_blocksize - 1)) {
+			ret = -EINVAL;
+			goto inode_unlock;
+		}
+
+		file_accessed(iocb->ki_filp);
+		ret = iomap_dio_rw(iocb, iter, &exfat_read_iomap_ops, NULL, 0,
+				NULL, 0);
+	} else {
+		ret = generic_file_read_iter(iocb, iter);
+	}
+
+inode_unlock:
+	inode_unlock_shared(inode);
+
+	return ret;
 }
 
 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
fs/exfat/inode.c +1 -44

57 lines not on this page · raw message

Signature · 1 line
2.25.1
#14 lore raw

Re: [PATCH 2/5] exfat: add iomap direct I/O support

fs/exfat/file.c static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) · line 705

Quoted 5 lines · Namjae Jeon
fs/exfat/file.c +5 -0
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		ret = iomap_dio_rw(iocb, iter, &exfat_write_iomap_ops,
+				&exfat_write_dio_ops, 0, NULL, 0);
+		if (ret == -ENOTBLK)
+			ret = 0;

This seems to miss a fallback to __generic_file_write_iter?

fs/exfat/file.c static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) · line 746

Quoted 8 lines
fs/exfat/file.c +8 -0
+	inode_lock_shared(inode);
+	if (iocb->ki_flags & IOCB_DIRECT) {
+		size_t count = iov_iter_count(iter);
+
+		if ((iocb->ki_pos | count) & (inode->i_sb->s_blocksize - 1)) {
+			ret = -EINVAL;
+			goto inode_unlock;
+		}

iomap_dio_bio_iter() should already take care of the alignment check.

fs/exfat/inode.c static const struct address_space_operations exfat_aops = { · line 512

Quoted 2 lines
fs/exfat/inode.c +1 -1
-	.direct_IO	= exfat_direct_IO,
+	.direct_IO	= noop_direct_IO,

This should not be needed, setting FMODE_CAN_ODIRECT in ->open should be all that is needed.

author maintainer
#15 lore raw

Re: [PATCH 2/5] exfat: add iomap direct I/O support

Quoted 9 lines · Christoph Hellwig

fs/exfat/file.c static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) · line 705

Quoted 5 lines · Namjae Jeon
fs/exfat/file.c +5 -0
+     if (iocb->ki_flags & IOCB_DIRECT) {
+             ret = iomap_dio_rw(iocb, iter, &exfat_write_iomap_ops,
+                             &exfat_write_dio_ops, 0, NULL, 0);
+             if (ret == -ENOTBLK)
+                     ret = 0;

This seems to miss a fallback to __generic_file_write_iter?

There is a follow-up patch in the series that adds the fallback to buffered write. I'll reorganize the patches so that this change and the fallback are combined together in a clearer way.

Quoted 11 lines

fs/exfat/file.c static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) · line 746

Quoted 9 lines
fs/exfat/file.c +8 -0
+     inode_lock_shared(inode);
+     if (iocb->ki_flags & IOCB_DIRECT) {
+             size_t count = iov_iter_count(iter);
+
+             if ((iocb->ki_pos | count) & (inode->i_sb->s_blocksize - 1)) {
+                     ret = -EINVAL;
+                     goto inode_unlock;
+             }

iomap_dio_bio_iter() should already take care of the alignment check.

Right, I will remove it.

Quoted 6 lines

fs/exfat/inode.c static const struct address_space_operations exfat_aops = { · line 512

Quoted 3 lines
fs/exfat/inode.c +1 -1
-     .direct_IO      = exfat_direct_IO,
+     .direct_IO      = noop_direct_IO,

This should not be needed, setting FMODE_CAN_ODIRECT in ->open should be all that is needed.

Okay. I will remove it.
Thanks!

Quoted 1 line
author maintainer
#16 lore raw

PATCH 3/5 exfat: add iomap buffered I/O support

In reply to Namjae Jeon

Add full buffered I/O support using the iomap framework to the exfat filesystem. This replaces the old exfat_get_block(), exfat_write_begin(), exfat_write_end(), and exfat_block_truncate_page() functions with their iomap equivalents. Buffered writes now use iomap_file_buffered_write(), read uses iomap_bio_read_folio() and iomap_bio_readahead(), and writeback is handled through iomap_writepages().

---
 fs/exfat/exfat_fs.h |   3 +-
 fs/exfat/file.c     | 160 +++++++++++++++++++--------
 fs/exfat/inode.c    | 261 ++++----------------------------------------
 3 files changed, 142 insertions(+), 282 deletions(-)
fs/exfat/exfat_fs.h +1 -2
index 860f2e438b63..54da001a8f55 100644
@@ -87,7 +87,7 @@ enum {
 /*
  * helpers for cluster size to byte conversion.
  */
-#define EXFAT_CLU_TO_B(b, sbi)		((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)		((loff_t)(b) << (sbi)->cluster_size_bits)
 #define EXFAT_B_TO_CLU(b, sbi)		((b) >> (sbi)->cluster_size_bits)
 #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi)	\
 	(((b - 1) >> (sbi)->cluster_size_bits) + 1)
@@ -551,7 +551,6 @@ struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
 int __exfat_write_inode(struct inode *inode, int sync);
 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
 void exfat_evict_inode(struct inode *inode);
-int exfat_block_truncate_page(struct inode *inode, loff_t from);
 int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		unsigned int *clu, unsigned int *count, int create,
 		bool *balloc);
fs/exfat/file.c +117 -43

240 lines not on this page · raw message

fs/exfat/inode.c +24 -237

311 lines not on this page · raw message

Signature · 1 line
2.25.1
#17 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

fs/exfat/exfat_fs.h enum { · line 90

Quoted 2 lines
fs/exfat/exfat_fs.h +1 -1
-#define EXFAT_CLU_TO_B(b, sbi)		((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)		((loff_t)(b) << (sbi)->cluster_size_bits)

This should be a prep patch.

Quoted 4 lines
fs/exfat/file.c +2 -0
 	if ((attr->ia_valid & ATTR_SIZE) &&
 	    attr->ia_size > i_size_read(inode)) {
+		loff_t old_size = i_size_read(inode);
+

fs/exfat/file.c int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, · line 351

Quoted 10 lines
fs/exfat/file.c +9 -0
 		error = exfat_cont_expand(inode, attr->ia_size);
+		if (!error && attr->ia_size > old_size &&
+		    old_size % PAGE_SIZE != 0) {
+			loff_t len = min_t(loff_t,
+					round_up(old_size, PAGE_SIZE) - old_size,
+					attr->ia_size - old_size);
+			error = iomap_zero_range(inode, old_size, len,
+					NULL, &exfat_read_iomap_ops,
+					&exfat_iomap_folio_ops, NULL);
+		}

Why is this needed for iomap? Shouldn't explicit zeroing on size change be a prep patch?

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 642

Quoted 11 lines
fs/exfat/file.c +4 -1
 	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
 		return -EIO;
 
-	err = __generic_file_fsync(filp, start, end, datasync);
+	err = file_write_and_wait_range(filp, start, end);
 	if (err)
 		return err;
 
+	if (!datasync)
+		err = __exfat_write_inode(inode, 1);
+	write_inode_now(inode, !datasync);

This seems to have lost the i_state check to see if a sync is needed before __exfat_write_inode. Also doing write_inode_now plus the explicit inode sync seems duplicate. Last but not least I don't understand how this is related to iomap?

Quoted 27 lines
fs/exfat/file.c +27 -0
+	ssize_t ret;
+
+	ret = iomap_dio_rw(iocb, from, &exfat_write_iomap_ops,
+			&exfat_write_dio_ops, 0, NULL, 0);
+	if (ret == -ENOTBLK)
+		ret = 0;
+	else if (ret < 0)
+		goto out;
+
+	if (iov_iter_count(from)) {
+		loff_t offset, end;
+		ssize_t written;
+		int ret2;
+
+		offset = iocb->ki_pos;
+		iocb->ki_flags &= ~IOCB_DIRECT;
+		written = iomap_file_buffered_write(iocb, from,
+				&exfat_write_iomap_ops, &exfat_iomap_folio_ops,
+				NULL);
+		if (written < 0) {
+			ret = written;
+			goto out;
+		}
+
+		ret += written;
+		end = iocb->ki_pos + written - 1;
+		ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,

fs/exfat/file.c int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync) · line 714

Quoted 10 lines
fs/exfat/file.c +10 -0
+				offset, end);
+		if (ret2) {
+			ret = -EIO;
+			goto out;
+		}
+		if (!ret2)
+			invalidate_mapping_pages(iocb->ki_filp->f_mapping,
+					offset >> PAGE_SHIFT,
+					end >> PAGE_SHIFT);
+	}

Eventually we should factor this code int oa common helper. Not needed for this submission, though.

Quoted 5 lines

{

fs/exfat/file.c +0 -3
-	int err;
 	struct inode *inode = file_inode(vmf->vma->vm_file);
-	struct exfat_inode_info *ei = EXFAT_I(inode);
-	loff_t new_valid_size;

fs/exfat/file.c static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) · line 775

Quoted 10 lines
fs/exfat/file.c +1 -5
+	vm_fault_t ret;
 
 	if (!inode_trylock(inode))
 		return VM_FAULT_RETRY;
 
-	new_valid_size = ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT;
-	new_valid_size = min(new_valid_size, i_size_read(inode));
-
-	if (ei->valid_size < new_valid_size) {
-		err = exfat_extend_valid_size(inode, new_valid_size, false);

Why is this moving out? I think the zeroing changes in this patch are really something that should be split out and explained in the commit log for better understanding and bisectability.

fs/exfat/file.c static ssize_t exfat_splice_read(struct file *in, loff_t *ppos, · line 876

Quoted 6 lines
fs/exfat/file.c +6 -0
+static int exfat_file_open(struct inode *inode, struct file *filp)
+{
+	int err;
+
+	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
+		return -EIO;

This doesn't look iomap-related.

fs/exfat/file.c static ssize_t exfat_splice_read(struct file *in, loff_t *ppos, · line 882

Quoted 6 lines
fs/exfat/file.c +6 -0
+
+	err = generic_file_open(inode, filp);
+	if (err)
+		return err;
+
+	filp->f_mode |= FMODE_CAN_ODIRECT;

This should go into the previous patch.

author maintainer
#18 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 5 lines · Christoph Hellwig

fs/exfat/exfat_fs.h enum { · line 90

Quoted 3 lines
fs/exfat/exfat_fs.h +1 -1
-#define EXFAT_CLU_TO_B(b, sbi)               ((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)               ((loff_t)(b) << (sbi)->cluster_size_bits)

This should be a prep patch.

Okay.

Quoted 16 lines
Quoted 5 lines
      if ((attr->ia_valid & ATTR_SIZE) &&
          attr->ia_size > i_size_read(inode)) {
fs/exfat/file.c +2 -0
+             loff_t old_size = i_size_read(inode);
+

fs/exfat/file.c int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, · line 351

Quoted 10 lines
fs/exfat/file.c +9 -0
              error = exfat_cont_expand(inode, attr->ia_size);
+             if (!error && attr->ia_size > old_size &&
+                 old_size % PAGE_SIZE != 0) {
+                     loff_t len = min_t(loff_t,
+                                     round_up(old_size, PAGE_SIZE) - old_size,
+                                     attr->ia_size - old_size);
+                     error = iomap_zero_range(inode, old_size, len,
+                                     NULL, &exfat_read_iomap_ops,
+                                     &exfat_iomap_folio_ops, NULL);
+             }
Quoted 2 lines

Why is this needed for iomap? Shouldn't explicit zeroing on size change be a prep patch?

Okay, I will change it to a prep patch.

Quoted 9 lines
Quoted 9 lines
      if (unlikely(exfat_forced_shutdown(inode->i_sb)))
              return -EIO;
fs/exfat/file.c +1 -1
-     err = __generic_file_fsync(filp, start, end, datasync);
+     err = file_write_and_wait_range(filp, start, end);
      if (err)
              return err;
Quoted 8 lines

fs/exfat/file.c int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) · line 642

Quoted 3 lines
fs/exfat/file.c +3 -0
+     if (!datasync)
+             err = __exfat_write_inode(inode, 1);
+     write_inode_now(inode, !datasync);

This seems to have lost the i_state check to see if a sync is needed before __exfat_write_inode. Also doing write_inode_now plus the explicit inode sync seems duplicate. Last but not least I don't understand how this is related to iomap?

There is a deadlock issue when using iomap_dio_rw(). In exfat_write_iter(), the inode lock is held while calling iomap_dio_rw. generic_write_sync()->__generic_file_fsync() in iomap_dio_complete() can be triggered, which attempts to acquire the inode lock again. I will fix i_state check and introduced duplicate inode syncing problem.

Quoted 32 lines
Quoted 32 lines
fs/exfat/file.c +32 -0
+     ssize_t ret;
+
+     ret = iomap_dio_rw(iocb, from, &exfat_write_iomap_ops,
+                     &exfat_write_dio_ops, 0, NULL, 0);
+     if (ret == -ENOTBLK)
+             ret = 0;
+     else if (ret < 0)
+             goto out;
+
+     if (iov_iter_count(from)) {
+             loff_t offset, end;
+             ssize_t written;
+             int ret2;
+
+             offset = iocb->ki_pos;
+             iocb->ki_flags &= ~IOCB_DIRECT;
+             written = iomap_file_buffered_write(iocb, from,
+                             &exfat_write_iomap_ops, &exfat_iomap_folio_ops,
+                             NULL);
+             if (written < 0) {
+                     ret = written;
+                     goto out;
+             }
+
+             ret += written;
+             end = iocb->ki_pos + written - 1;
+             ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
+                             offset, end);
+             if (ret2) {
+                     ret = -EIO;
+                     goto out;
+             }
Quoted 8 lines

fs/exfat/file.c int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync) · line 714

Quoted 5 lines
fs/exfat/file.c +5 -0
+             if (!ret2)
+                     invalidate_mapping_pages(iocb->ki_filp->f_mapping,
+                                     offset >> PAGE_SHIFT,
+                                     end >> PAGE_SHIFT);
+     }

Eventually we should factor this code int oa common helper. Not needed for this submission, though.

Okay.

Quoted 17 lines
Quoted 6 lines

{

fs/exfat/file.c +0 -3
-     int err;
      struct inode *inode = file_inode(vmf->vma->vm_file);
-     struct exfat_inode_info *ei = EXFAT_I(inode);
-     loff_t new_valid_size;

fs/exfat/file.c static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) · line 775

Quoted 10 lines
fs/exfat/file.c +1 -5
+     vm_fault_t ret;
 
      if (!inode_trylock(inode))
              return VM_FAULT_RETRY;
 
-     new_valid_size = ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT;
-     new_valid_size = min(new_valid_size, i_size_read(inode));
-
-     if (ei->valid_size < new_valid_size) {
-             err = exfat_extend_valid_size(inode, new_valid_size, false);
Quoted 3 lines

Why is this moving out? I think the zeroing changes in this patch are really something that should be split out and explained in the commit log for better understanding and bisectability.

Okay, I will split it out and add detailed commit log.

Quoted 9 lines

fs/exfat/file.c static ssize_t exfat_splice_read(struct file *in, loff_t *ppos, · line 876

Quoted 7 lines
fs/exfat/file.c +6 -0
+static int exfat_file_open(struct inode *inode, struct file *filp)
+{
+     int err;
+
+     if (unlikely(exfat_forced_shutdown(inode->i_sb)))
+             return -EIO;

This doesn't look iomap-related.

Okay. I will split it out also.

Quoted 9 lines

fs/exfat/file.c static ssize_t exfat_splice_read(struct file *in, loff_t *ppos, · line 882

Quoted 7 lines
fs/exfat/file.c +6 -0
+
+     err = generic_file_open(inode, filp);
+     if (err)
+             return err;
+
+     filp->f_mode |= FMODE_CAN_ODIRECT;

This should go into the previous patch.

Right. I will fix it.
Thanks!

Quoted 1 line
#19 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 4 lines · Namjae Jeon

There is a deadlock issue when using iomap_dio_rw(). In exfat_write_iter(), the inode lock is held while calling iomap_dio_rw. generic_write_sync()->__generic_file_fsync() in iomap_dio_complete() can be triggered, which attempts to acquire the inode lock again.

Can you document this? Note that Jan has looked into __generic_file_fsync and decided i_rwsem isn't needed there. So maybe as a first step just switch exfat to generic_buffers_fsync_noflush, which doesn't need this (or way if/when Jan's series gets merged) and keep using a generic version? If not document in the commit log why this is changed.

author maintainer
#20 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Namjae Jeon

There is a deadlock issue when using iomap_dio_rw(). In exfat_write_iter(), the inode lock is held while calling iomap_dio_rw. generic_write_sync()->__generic_file_fsync() in iomap_dio_complete() can be triggered, which attempts to acquire the inode lock again.

Quoted 5 lines

Can you document this? Note that Jan has looked into __generic_file_fsync and decided i_rwsem isn't needed there. So maybe as a first step just switch exfat to generic_buffers_fsync_noflush, which doesn't need this (or way if/when Jan's series gets merged) and keep using a generic version? If not document in the commit log why this is changed.

Since Jan's series renames __generic_file_fsync() to simple_fsync_noflush(), switching to generic_buffers_fsync_noflush() now would cause a conflict with his patchset. I think that we can wait for Jan’s series to be merged first. So I will drop the exfat_file_fsync changes in the exfat iomap patchset. Thanks!

Quoted 1 line
#21 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 5 lines · Namjae Jeon

Since Jan's series renames __generic_file_fsync() to simple_fsync_noflush(), switching to generic_buffers_fsync_noflush() now would cause a conflict with his patchset. I think that we can wait for Jan’s series to be merged first. So I will drop the exfat_file_fsync changes in the exfat iomap patchset.

Or just open code it in exfat for now and switch beack to the generic one once that becomes suitable again. But we're pretty late for the 7.1 merge window anyway, so maybe not rushing this might be a better idea.

author maintainer
#22 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 8 lines · Christoph Hellwig
Quoted 5 lines · Namjae Jeon

Since Jan's series renames __generic_file_fsync() to simple_fsync_noflush(), switching to generic_buffers_fsync_noflush() now would cause a conflict with his patchset. I think that we can wait for Jan’s series to be merged first. So I will drop the exfat_file_fsync changes in the exfat iomap patchset.

Quoted 4 lines

Or just open code it in exfat for now and switch beack to the generic one once that becomes suitable again. But we're pretty late for the 7.1 merge window anyway, so maybe not rushing this might be a better idea.

There are still several things I need to check and improve, including possible changes to the core iomap layer.
So I agree that It looks difficult to get this series merged in the 7.1 window. But I'll submit v2 patch-set so it can receive further review.
Thanks!

Quoted 1 line
#23 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

In reply to Namjae Jeon
Quoted 19 lines · Namjae Jeon

Add full buffered I/O support using the iomap framework to the exfat filesystem. This replaces the old exfat_get_block(), exfat_write_begin(), exfat_write_end(), and exfat_block_truncate_page() functions with their iomap equivalents. Buffered writes now use iomap_file_buffered_write(), read uses iomap_bio_read_folio() and iomap_bio_readahead(), and writeback is handled through iomap_writepages().

---
 fs/exfat/exfat_fs.h |   3 +-
 fs/exfat/file.c     | 160 +++++++++++++++++++--------
 fs/exfat/inode.c    | 261 ++++----------------------------------------
 3 files changed, 142 insertions(+), 282 deletions(-)
fs/exfat/exfat_fs.h +0 -0
index 860f2e438b63..54da001a8f55 100644

fs/exfat/exfat_fs.h enum { · line 90

Quoted 5 lines
fs/exfat/exfat_fs.h +1 -1
@@ -87,7 +87,7 @@ enum {
 /*
  * helpers for cluster size to byte conversion.
  */
-#define EXFAT_CLU_TO_B(b, sbi)		((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)		((loff_t)(b) << (sbi)->cluster_size_bits)

I think type casting should be left to the users of the macro. These helper macros are quite dangerous in case of (ex)FAT because integer overflows are unchecked for shift op. The on-disk format is 32 bit but the kernel has switched over to "all-64-bit".

That particular macro is used in dirent update code and, in particular, in exfat_map_cluster():

    inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;

The type of i_blocks is blkcnt_t, which is unsigned. loff_t is signed. exFAT got away with it because Linux write calls expand the size of files no more than 0x7ffff000 bytes and the size of dir is limited to 256MB. However, someone could come in and decide to use exfat_map_cluster() to allocate more than this limit.

I don't think we can win because of the inherent discrepency between the on-disk format and the kernel interface. How the calculated value is used cannot be safely assumed.

Quoted 106 lines
fs/exfat/exfat_fs.h +0 -1
 #define EXFAT_B_TO_CLU(b, sbi)		((b) >> (sbi)->cluster_size_bits)
 #define EXFAT_B_TO_CLU_ROUND_UP(b, sbi)	\
 	(((b - 1) >> (sbi)->cluster_size_bits) + 1)
@@ -551,7 +551,6 @@ struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
 int __exfat_write_inode(struct inode *inode, int sync);
 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
 void exfat_evict_inode(struct inode *inode);
-int exfat_block_truncate_page(struct inode *inode, loff_t from);
 int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		unsigned int *clu, unsigned int *count, int create,
 		bool *balloc);
fs/exfat/file.c +54 -3
index 2a9263b4433b..5f85e2e0a71e 100644
@@ -337,7 +337,18 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 
 	if ((attr->ia_valid & ATTR_SIZE) &&
 	    attr->ia_size > i_size_read(inode)) {
+		loff_t old_size = i_size_read(inode);
+
 		error = exfat_cont_expand(inode, attr->ia_size);
+		if (!error && attr->ia_size > old_size &&
+		    old_size % PAGE_SIZE != 0) {
+			loff_t len = min_t(loff_t,
+					round_up(old_size, PAGE_SIZE) - old_size,
+					attr->ia_size - old_size);
+			error = iomap_zero_range(inode, old_size, len,
+					NULL, &exfat_read_iomap_ops,
+					&exfat_iomap_folio_ops, NULL);
+		}
 		if (error || attr->ia_valid == ATTR_SIZE)
 			return error;
 		attr->ia_valid &= ~ATTR_SIZE;
@@ -384,7 +395,10 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	exfat_truncate_inode_atime(inode);
 
 	if (attr->ia_valid & ATTR_SIZE) {
-		error = exfat_block_truncate_page(inode, attr->ia_size);
+		inode_dio_wait(inode);
+		error = iomap_truncate_page(inode, attr->ia_size, NULL,
+				&exfat_read_iomap_ops,
+				&exfat_iomap_folio_ops, NULL);
 		if (error)
 			goto out;
 
@@ -619,10 +633,14 @@ int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
 		return -EIO;
 
-	err = __generic_file_fsync(filp, start, end, datasync);
+	err = file_write_and_wait_range(filp, start, end);
 	if (err)
 		return err;
 
+	if (!datasync)
+		err = __exfat_write_inode(inode, 1);
+	write_inode_now(inode, !datasync);
+
 	err = sync_blockdev(inode->i_sb->s_bdev);
 	if (err)
 		return err;
@@ -648,12 +666,56 @@ int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync)
 				NULL);
 		if (!ret && bsync)
 			ret = filemap_write_and_wait_range(inode->i_mapping,
-					old_valid_size, off - 1);
+							   old_valid_size,
+							   off - 1);
 	}
 
 	return ret;
 }
 
+static ssize_t exfat_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
+{
+	ssize_t ret;
+
+	ret = iomap_dio_rw(iocb, from, &exfat_write_iomap_ops,
+			&exfat_write_dio_ops, 0, NULL, 0);
+	if (ret == -ENOTBLK)
+		ret = 0;
+	else if (ret < 0)
+		goto out;
+
+	if (iov_iter_count(from)) {
+		loff_t offset, end;
+		ssize_t written;
+		int ret2;
+
+		offset = iocb->ki_pos;
+		iocb->ki_flags &= ~IOCB_DIRECT;
+		written = iomap_file_buffered_write(iocb, from,
+				&exfat_write_iomap_ops, &exfat_iomap_folio_ops,
+				NULL);
+		if (written < 0) {
+			ret = written;
+			goto out;
+		}
+
+		ret += written;
+		end = iocb->ki_pos + written - 1;
+		ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
+				offset, end);
+		if (ret2) {
+			ret = -EIO;

fs/exfat/file.c int exfat_extend_valid_size(struct inode *inode, loff_t off, bool bsync) · line 717

Quoted 10 lines
fs/exfat/file.c +10 -0
+			goto out;
+		}
+		if (!ret2)
+			invalidate_mapping_pages(iocb->ki_filp->f_mapping,
+					offset >> PAGE_SHIFT,
+					end >> PAGE_SHIFT);
+	}
+out:
+	return ret;
+}

There's nothing to clean up upon error here. Suggest removing goto. I noticed that gotos are generally removed where appropriate in this serious of patches, but it looks like you've missed this bit.

#24 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 11 lines · David Timber

fs/exfat/exfat_fs.h enum { · line 90

Quoted 2 lines
fs/exfat/exfat_fs.h +1 -1
-#define EXFAT_CLU_TO_B(b, sbi)		((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)		((loff_t)(b) << (sbi)->cluster_size_bits)

I think type casting should be left to the users of the macro. These helper macros are quite dangerous in case of (ex)FAT because integer overflows are unchecked for shift op. The on-disk format is 32 bit but the kernel has switched over to "all-64-bit".

That particular macro is used in dirent update code and, in particular, in exfat_map_cluster():

Quoted 7 lines
    inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;

The type of i_blocks is blkcnt_t, which is unsigned. loff_t is signed. exFAT got away with it because Linux write calls expand the size of files no more than 0x7ffff000 bytes and the size of dir is limited to 256MB. However, someone could come in and decide to use exfat_map_cluster() to allocate more than this limit.

But that will blow up in some many other ways. The only sane thing to do here is to replace those macros with inline functions that include the proper casting.

author maintainer
#25 lore raw

Re: [PATCH 3/5] exfat: add iomap buffered I/O support

Quoted 21 lines · Christoph Hellwig
Quoted 11 lines · David Timber

fs/exfat/exfat_fs.h enum { · line 90

Quoted 2 lines
fs/exfat/exfat_fs.h +1 -1
-#define EXFAT_CLU_TO_B(b, sbi)             ((b) << (sbi)->cluster_size_bits)
+#define EXFAT_CLU_TO_B(b, sbi)             ((loff_t)(b) << (sbi)->cluster_size_bits)

I think type casting should be left to the users of the macro. These helper macros are quite dangerous in case of (ex)FAT because integer overflows are unchecked for shift op. The on-disk format is 32 bit but the kernel has switched over to "all-64-bit".

That particular macro is used in dirent update code and, in particular, in exfat_map_cluster():

Quoted 7 lines
    inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;

The type of i_blocks is blkcnt_t, which is unsigned. loff_t is signed. exFAT got away with it because Linux write calls expand the size of files no more than 0x7ffff000 bytes and the size of dir is limited to 256MB. However, someone could come in and decide to use exfat_map_cluster() to allocate more than this limit.

Quoted 3 lines

But that will blow up in some many other ways. The only sane thing to do here is to replace those macros with inline functions that include the proper casting.

Agreed. I'll replace them and include the changes in the v2 patch-set. Thanks!

Quoted 1 line
author maintainer
#26 lore raw

PATCH 4/5 exfat: add support for multi-cluster allocation

In reply to Namjae Jeon

Currently exfat_map_cluster() allocates and returns only one cluster at a time even when more clusters are needed. This causes multiple FAT walks and repeated allocation calls during large sequential writes or when using iomap for writes. This change exfat_map_cluster() and exfat_alloc_cluster() to be able to allocate multiple contiguous clusters.

---
 fs/exfat/dir.c      |  2 +-
 fs/exfat/exfat_fs.h |  2 +-
 fs/exfat/fatent.c   | 26 ++++++++++++++++----------
 fs/exfat/file.c     |  2 +-
 fs/exfat/inode.c    | 32 +++++---------------------------
 fs/exfat/namei.c    |  2 +-
 6 files changed, 25 insertions(+), 41 deletions(-)
fs/exfat/dir.c +1 -1
index a2c2b998808c..857b22e431cd 100644
@@ -308,7 +308,7 @@ int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
 
 	exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
 
-	ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
+	ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode), false);
 	if (ret)
 		return ret;
 
fs/exfat/exfat_fs.h +1 -1
index 54da001a8f55..5992755b5ab3 100644
@@ -448,7 +448,7 @@ int exfat_clear_volume_dirty(struct super_block *sb);
 #define exfat_get_next_cluster(sb, pclu) exfat_ent_get(sb, *(pclu), pclu, NULL)
 
 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
-		struct exfat_chain *p_chain, bool sync_bmap);
+		struct exfat_chain *p_chain, bool sync_bmap, bool contig);
 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain);
 int exfat_ent_get(struct super_block *sb, unsigned int loc,
 		unsigned int *content, struct buffer_head **last);
fs/exfat/fatent.c +16 -10
index f2e5d5dde393..758c2d971e73 100644
@@ -424,7 +424,7 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
 }
 
 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
-		struct exfat_chain *p_chain, bool sync_bmap)
+		struct exfat_chain *p_chain, bool sync_bmap, bool contig)
 {
 	int ret = -ENOSPC;
 	unsigned int total_cnt;
@@ -475,14 +475,20 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
 
 	while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
 	       EXFAT_EOF_CLUSTER) {
-		if (new_clu != hint_clu &&
-		    p_chain->flags == ALLOC_NO_FAT_CHAIN) {
-			if (exfat_chain_cont_cluster(sb, p_chain->dir,
-					p_chain->size)) {
-				ret = -EIO;
-				goto free_cluster;
+		if (new_clu != hint_clu) {
+			if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
+				if (exfat_chain_cont_cluster(sb, p_chain->dir,
+							     p_chain->size)) {
+					ret = -EIO;
+					goto free_cluster;
+				}
+				p_chain->flags = ALLOC_FAT_CHAIN;
+			}
+
+			if (contig && p_chain->size > 0) {
+				hint_clu--;
+				goto done;
 			}
-			p_chain->flags = ALLOC_FAT_CHAIN;
 		}
 
 		/* update allocation bitmap */
@@ -512,9 +518,9 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
 		last_clu = new_clu;
 
 		if (p_chain->size == num_alloc) {
+done:
 			sbi->clu_srch_ptr = hint_clu;
-			sbi->used_clusters += num_alloc;
-
+			sbi->used_clusters += p_chain->size;
 			mutex_unlock(&sbi->bitmap_lock);
 			return 0;
 		}
fs/exfat/file.c +1 -1
index 5f85e2e0a71e..d7857aec072b 100644
@@ -57,7 +57,7 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
 	clu.flags = ei->flags;
 
 	ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters,
-			&clu, inode_needs_sync(inode));
+			&clu, inode_needs_sync(inode), false);
 	if (ret)
 		return ret;
 
fs/exfat/inode.c +5 -27
index c53ae9293cfe..46dc98ef1afb 100644
@@ -128,14 +128,10 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 	struct exfat_inode_info *ei = EXFAT_I(inode);
 	unsigned int local_clu_offset = clu_offset;
-	unsigned int num_to_be_allocated = 0, num_clusters;
+	unsigned int num_to_be_allocated = *count, num_clusters;
 
 	num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
-
-	if (clu_offset >= num_clusters)
-		num_to_be_allocated = clu_offset - num_clusters + 1;
-
-	if (!create && (num_to_be_allocated > 0)) {
+	if (!create && clu_offset >= num_clusters) {
 		*clu = EXFAT_EOF_CLUSTER;
 		return 0;
 	}
@@ -176,7 +172,7 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		}
 
 		ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
-				inode_needs_sync(inode));
+				inode_needs_sync(inode), true);
 		if (ret)
 			return ret;
 
@@ -210,26 +206,8 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		}
 
 		*clu = new_clu.dir;
-
-		inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
-
-		/*
-		 * Move *clu pointer along FAT chains (hole care) because the
-		 * caller of this function expect *clu to be the last cluster.
-		 * This only works when num_to_be_allocated >= 2,
-		 * *clu = (the first cluster of the allocated chain) =>
-		 * (the last cluster of ...)
-		 */
-		if (ei->flags == ALLOC_NO_FAT_CHAIN) {
-			*clu += num_to_be_allocated - 1;
-		} else {
-			while (num_to_be_allocated > 1) {
-				if (exfat_get_next_cluster(sb, clu))
-					return -EIO;
-				num_to_be_allocated--;
-			}
-		}
-		*count = 1;
+		*count = new_clu.size;
+		inode->i_blocks += EXFAT_CLU_TO_B(new_clu.size, sbi) >> 9;
 		*balloc = true;
 	}
 
fs/exfat/namei.c +1 -1
index d0ea1ff81c09..6261cd994d1d 100644
@@ -347,7 +347,7 @@ int exfat_find_empty_entry(struct inode *inode,
 		}
 
 		/* allocate a cluster */
-		ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
+		ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode), false);
 		if (ret)
 			return ret;
 
Signature · 1 line
2.25.1
author maintainer
#27 lore raw

PATCH 5/5 exfat: add support for SEEK_HOLE and SEEK_DATA in llseek

In reply to Namjae Jeon

Adds exfat_file_llseek() that implements these whence values via the iomap layer (iomap_seek_hole() and iomap_seek_data()) using the existing exfat_read_iomap_ops.
Unlike many other modern filesystems, exFAT does not support sparse files with unallocated clusters (holes). In exFAT, clusters are always fully allocated once they are written or preallocated. In addition, exFAT maintains a separate "Valid Data Length" (valid_size) that is distinct from the logical file size. This affects how holes are reported during seeking. In exfat_read_iomap_begin(), ranges where the offset is greater than or equal to ei->valid_size are mapped as IOMAP_UNWRITTEN, while ranges below valid_size are mapped as IOMAP_MAPPED. This mapping behavior is used by the iomap seek functions to correctly report SEEK_HOLE and SEEK_DATA positions.

- Ranges with offset >= ei->valid_size are mapped as IOMAP_UNWRITTEN.
- Ranges with offset < ei->valid_size are mapped as IOMAP_MAPPED.

---
 fs/exfat/file.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
fs/exfat/file.c +26 -1
index d7857aec072b..9a30c32b3a05 100644
@@ -884,9 +884,34 @@ static int exfat_file_open(struct inode *inode, struct file *filp)
 	return 0;
 }
 
+static loff_t exfat_file_llseek(struct file *file, loff_t offset, int whence)
+{
+	struct inode *inode = file->f_mapping->host;
+
+	switch (whence) {
+	case SEEK_HOLE:
+		inode_lock_shared(inode);
+		offset = iomap_seek_hole(inode, offset, &exfat_read_iomap_ops);
+		inode_unlock_shared(inode);
+		break;
+	case SEEK_DATA:
+		inode_lock_shared(inode);
+		offset = iomap_seek_data(inode, offset, &exfat_read_iomap_ops);
+		inode_unlock_shared(inode);
+		break;
+	default:
+		return generic_file_llseek_size(file, offset, whence,
+						inode->i_sb->s_maxbytes,
+						i_size_read(inode));
+	}
+	if (offset < 0)
+		return offset;
+	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
+}
+
 const struct file_operations exfat_file_operations = {
 	.open		= exfat_file_open,
-	.llseek		= generic_file_llseek,
+	.llseek		= exfat_file_llseek,
 	.read_iter	= exfat_file_read_iter,
 	.write_iter	= exfat_file_write_iter,
 	.unlocked_ioctl = exfat_ioctl,
Signature · 1 line
2.25.1
#28 lore raw

Re: [PATCH 5/5] exfat: add support for SEEK_HOLE and SEEK_DATA in llseek

fs/exfat/file.c static int exfat_file_open(struct inode *inode, struct file *filp) · line 905

Quoted 4 lines
fs/exfat/file.c +4 -0
+	default:
+		return generic_file_llseek_size(file, offset, whence,
+						inode->i_sb->s_maxbytes,
+						i_size_read(inode));

Just use generic_file_llseek directly here.

author maintainer
#29 lore raw

Re: [PATCH 5/5] exfat: add support for SEEK_HOLE and SEEK_DATA in llseek

Quoted 7 lines · Christoph Hellwig

fs/exfat/file.c static int exfat_file_open(struct inode *inode, struct file *filp) · line 905

Quoted 5 lines
fs/exfat/file.c +4 -0
+     default:
+             return generic_file_llseek_size(file, offset, whence,
+                                             inode->i_sb->s_maxbytes,
+                                             i_size_read(inode));

Just use generic_file_llseek directly here.

Okay, I will update it like this.
Thanks for your review!

Quoted 1 line
#30 lore raw
In reply to Namjae Jeon
Quoted 2 lines · Namjae Jeon

This patch series converts the exfat filesystem to the iomap framework for buffered I/O, direct I/O, and llseek (SEEK_HOLE/SEEK_DATA) support.

What tree is this against? Patch fails to apply to current mainline. Or if you have a link to a git tree, that could be useful as well.

author maintainer
#31 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 2 lines · Namjae Jeon

This patch series converts the exfat filesystem to the iomap framework for buffered I/O, direct I/O, and llseek (SEEK_HOLE/SEEK_DATA) support.

What tree is this against? Patch fails to apply to current mainline.

This patch series was created against exfat #dev branch.

Quoted 1 line

Or if you have a link to a git tree, that could be useful as well.

You can find the git tree here:
git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat.git dev

Thanks!