Skip to contentSkip to conversation

exfat: convert to iomap

PATCH Applied Namjae Jeon 5 participants 22 messages May 18 – May 22

Subsystems (3)
lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

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

iozone benchmark results (4KB cluster size, -s1g -r64k, 1GB file, 64KB record size)

1 thread 4 threads
Write Read Write Read
(MB/s) (MB/s) (MB/s) (MB/s)
-----------------------------------------------------------------

exfat + iomap patch   332.7     418.1           78.6      82.4
Current exfat         278.4     415.1           42.1      38.0

-----------------------------------------------------------------
Improvement +19.5% +0.7% +86.7% +117.4%

Available in the Git repository at:
===================================
git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat.git iomap-work

v4:
- Declare initialized variables before uninitialized ones in __iomap_write_begin.
- Clean up the logic for handling ei->valid_size in the non-alloc iomap read path.
- Rename exfat_cluster_to_phys to exfat_cluster_to_phys_bytes.
- Use end variable in exfat_fallback_buffered_write.

v3:
- Remove extra >> 9 when adding to inode->i_blocks.
- Simplify exfat_file_open() by removing unnecessary err variable.
- Separate exfat_truncate() error return conversion from iomap changes.
- Fix xfstests failures with 512B cluster size.
- Make exfat_truncate() return error code.
- Use inode lock to protect valid_size in exfat_extend_valid_size().
- Unify num_clusters calculation regardless of may_alloc.
- Use min_t() to clamp iomap->length.
- Return early in exfat_write_iomap_end() if no data written.
- Remove s_lock in exfat_write_iomap_end().
- Split DIO fallback buffered write into helper function.
- Remove unnecessary goto in exfat_dio_write_iter().

v2:
- Replace macros with static inline functions.
- Remove noop_direct_IO.
- Consolidate read and write iomap_begin into __exfat_iomap_begin()
- Zero out stale data in straddle block beyond valid_size.
- Introduce IOMAP_F_ZERO_TAIL flag to remove exfat_iomap_put_folio().
- Select FS_IOMAP in Kconfig.
- Remove unnecessary alignment check in exfat_file_read_iter().
- Just use generic_file_llseek directly.
- Move exfat_extend_valid_size to exfat_file_write_iter().
- Use pagecache_isize_extended() to remove iomap_zero_range in exfat_setattr().
- fix mmap write data corruption with byte-by-byte fallocate.
- Remove exfat_mkwrite_iomap_begin().

Namjae Jeon (11):
iomap: introduce IOMAP_F_ZERO_TAIL flag
exfat: replace unsafe macros with static inline functions
exfat: add balloc parameter to exfat_map_cluster() for iomap support
exfat: add exfat_file_open()
exfat: add support for multi-cluster allocation
exfat: add data_start_bytes and exfat_cluster_to_phys_bytes() helper
exfat: fix implicit declaration of brelse()
exfat: add iomap buffered I/O support
exfat: add iomap direct I/O support
exfat: add support for SEEK_HOLE and SEEK_DATA in llseek
exfat: make exfat_truncate() return error code

 fs/exfat/Kconfig       |   2 +-
 fs/exfat/Makefile      |   2 +-
 fs/exfat/balloc.c      |   2 +-
 fs/exfat/dir.c         |  50 +++---
 fs/exfat/exfat_fs.h    | 140 ++++++++++++-----
 fs/exfat/fatent.c      |  30 ++--
 fs/exfat/file.c        | 263 +++++++++++++++++++++++--------
 fs/exfat/inode.c       | 342 ++++++-----------------------------------
 fs/exfat/iomap.c       | 263 +++++++++++++++++++++++++++++++
 fs/exfat/iomap.h       |  15 ++
 fs/exfat/namei.c       |  28 ++--
 fs/exfat/super.c       |   5 +-
 fs/iomap/buffered-io.c |   4 +
 include/linux/iomap.h  |   4 +
 14 files changed, 703 insertions(+), 447 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 v4 1/11 iomap: introduce IOMAP_F_ZERO_TAIL flag

In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten.

This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block.

Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region.

---
 fs/iomap/buffered-io.c | 4 ++++
 include/linux/iomap.h  | 4 ++++
 2 files changed, 8 insertions(+)
fs/iomap/buffered-io.c +4 -0
index d7b648421a70..44046c648df4 100644
@@ -836,6 +836,7 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
 				return -EIO;
 			folio_zero_segments(folio, poff, from, to, poff + plen);
 		} else {
+			const struct iomap *iomap = iomap_iter_srcmap(iter);
 			int status;
 
 			if (iter->flags & IOMAP_NOWAIT)
@@ -853,6 +854,9 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
 						  len, status, GFP_NOFS);
 			if (status)
 				return status;
+
+			if (iomap->flags & IOMAP_F_ZERO_TAIL)
+				folio_zero_segment(folio, to, poff + plen);
 		}
 		iomap_set_range_uptodate(folio, poff, plen);
 	} while ((block_start += plen) < block_end);
include/linux/iomap.h +4 -0
index 2c5685adf3a9..750602e18750 100644
@@ -67,6 +67,9 @@ struct vm_fault;
  * bio, i.e. set REQ_ATOMIC.
  *
  * IOMAP_F_INTEGRITY indicates that the filesystems handles integrity metadata.
+ *
+ * IOMAP_F_ZERO_TAIL indicates the remainder of the block after the data
+ * written should be zeroed.
  */
 #define IOMAP_F_NEW		(1U << 0)
 #define IOMAP_F_DIRTY		(1U << 1)
@@ -86,6 +89,7 @@ struct vm_fault;
 #else
 #define IOMAP_F_INTEGRITY	0
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
+#define IOMAP_F_ZERO_TAIL	(1U << 10)
 
 /*
  * Flag reserved for file system specific usage
Signature · 1 line
2.25.1
Reviewed-by on v4 1/11
#2 lore raw

Re: [PATCH v4 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag

Quoted 9 lines · Namjae Jeon

In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten.

This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block.

Quoted 6 lines

Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region.

AFAICT, the "valid size" means "all space between valid_size and i_size is unwritten", and that's why you need the tail of the block to be zeroed, right?

So if, say, the fsblock size is 4k and valid_size is 80k; and I initiate a pwrite of 300 bytes at 121k, exfat will do its own zeroing to bump valid_size up to 121k, right? Then the actual iomap_write call will copy the 300 bytes into the pagecache, and now it needs ZERO_TAIL to zero the rest of the pagecache from (121k + 300) to 124k, correct?

(What I'm probing for is, there's no need for a ZERO_HEAD at this time because exfat has to take care of that, right?)

--D

Quoted 52 lines
---
 fs/iomap/buffered-io.c | 4 ++++
 include/linux/iomap.h  | 4 ++++
 2 files changed, 8 insertions(+)
fs/iomap/buffered-io.c +4 -0
index d7b648421a70..44046c648df4 100644
@@ -836,6 +836,7 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
 				return -EIO;
 			folio_zero_segments(folio, poff, from, to, poff + plen);
 		} else {
+			const struct iomap *iomap = iomap_iter_srcmap(iter);
 			int status;
 
 			if (iter->flags & IOMAP_NOWAIT)
@@ -853,6 +854,9 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
 						  len, status, GFP_NOFS);
 			if (status)
 				return status;
+
+			if (iomap->flags & IOMAP_F_ZERO_TAIL)
+				folio_zero_segment(folio, to, poff + plen);
 		}
 		iomap_set_range_uptodate(folio, poff, plen);
 	} while ((block_start += plen) < block_end);
include/linux/iomap.h +4 -0
index 2c5685adf3a9..750602e18750 100644
@@ -67,6 +67,9 @@ struct vm_fault;
  * bio, i.e. set REQ_ATOMIC.
  *
  * IOMAP_F_INTEGRITY indicates that the filesystems handles integrity metadata.
+ *
+ * IOMAP_F_ZERO_TAIL indicates the remainder of the block after the data
+ * written should be zeroed.
  */
 #define IOMAP_F_NEW		(1U << 0)
 #define IOMAP_F_DIRTY		(1U << 1)
@@ -86,6 +89,7 @@ struct vm_fault;
 #else
 #define IOMAP_F_INTEGRITY	0
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
+#define IOMAP_F_ZERO_TAIL	(1U << 10)
 
 /*
  * Flag reserved for file system specific usage
Signature · 1 line
2.25.1
author maintainer
#3 lore raw

Re: [PATCH v4 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag

Quoted 15 lines · Darrick J. Wong
Quoted 13 lines · Namjae Jeon

In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten.

This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block.

Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region.

Quoted 6 lines
Quoted 2 lines

AFAICT, the "valid size" means "all space between valid_size and i_size is unwritten", and that's why you need the tail of the block to be zeroed, right?

Exactly.

Quoted 6 lines

So if, say, the fsblock size is 4k and valid_size is 80k; and I initiate a pwrite of 300 bytes at 121k, exfat will do its own zeroing to bump valid_size up to 121k, right? Then the actual iomap_write call will copy the 300 bytes into the pagecache, and now it needs ZERO_TAIL to zero the rest of the pagecache from (121k + 300) to 124k, correct?

Yes, that's correct.

Quoted 3 lines

(What I'm probing for is, there's no need for a ZERO_HEAD at this time because exfat has to take care of that, right?)

Right.

Quoted 2 lines

Thanks for the review!

Quoted 55 lines

--D

Quoted 52 lines
---
 fs/iomap/buffered-io.c | 4 ++++
 include/linux/iomap.h  | 4 ++++
 2 files changed, 8 insertions(+)
fs/iomap/buffered-io.c +4 -0
index d7b648421a70..44046c648df4 100644
@@ -836,6 +836,7 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
                              return -EIO;
                      folio_zero_segments(folio, poff, from, to, poff + plen);
              } else {
+                     const struct iomap *iomap = iomap_iter_srcmap(iter);
                      int status;
 
                      if (iter->flags & IOMAP_NOWAIT)
@@ -853,6 +854,9 @@ static int __iomap_write_begin(const struct iomap_iter *iter,
                                                len, status, GFP_NOFS);
                      if (status)
                              return status;
+
+                     if (iomap->flags & IOMAP_F_ZERO_TAIL)
+                             folio_zero_segment(folio, to, poff + plen);
              }
              iomap_set_range_uptodate(folio, poff, plen);
      } while ((block_start += plen) < block_end);
include/linux/iomap.h +4 -1
index 2c5685adf3a9..750602e18750 100644
@@ -67,6 +67,9 @@ struct vm_fault;
  * bio, i.e. set REQ_ATOMIC.
  *
  * IOMAP_F_INTEGRITY indicates that the filesystems handles integrity metadata.
+ *
+ * IOMAP_F_ZERO_TAIL indicates the remainder of the block after the data
+ * written should be zeroed.
  */
 #define IOMAP_F_NEW          (1U << 0)
 #define IOMAP_F_DIRTY                (1U << 1)
@@ -86,6 +89,7 @@ struct vm_fault;
 #else
 #define IOMAP_F_INTEGRITY    0
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
+#define IOMAP_F_ZERO_TAIL    (1U << 10)
 
 /*
  * Flag reserved for file system specific usage
--

2.25.1

#4 lore raw

Re: [PATCH v4 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag

In reply to Namjae Jeon
Quoted 9 lines · Namjae Jeon

In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten.

This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block.

Quoted 7 lines

Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region.

---

I've applied this. The branch is marked as shared with the exfat tree and will remain stable.

author maintainer
#5 lore raw

Re: [PATCH v4 01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag

Quoted 15 lines · Christian Brauner
Quoted 13 lines · Namjae Jeon

In filesystems that maintain a separate Valid Data Length, such as exFAT and NTFS, a partial write may start at or beyond the current valid_size and extend it. In this case, the region after the previous valid_size but within the same filesystem block is considered unwritten.

This patch introduces IOMAP_F_ZERO_TAIL. When this flag is set in iomap, __iomap_write_begin() will zero only the tail portion while preserving any valid data before it in the same block.

Without this tail zeroing, stale data in the unwritten portion of the block can remain in the page cache. Subsequent reads can then return incorrect contents from that region.

Quoted 6 lines
Quoted 3 lines

---

I've applied this. The branch is marked as shared with the exfat tree and will remain stable.

Thanks, Christian!

author maintainer
#6 lore raw

PATCH v4 2/11 exfat: replace unsafe macros with static inline functions

In reply to Namjae Jeon

The current exFAT driver relies on various macros for unit conversions between clusters, blocks, sectors, and directory entries. These macros are structurally unsafe as they lack type enforcement and are prone to potential integer overflows during bit-shift operations, especially on 64-bit architectures. Replace all arithmetic macros with static inline functions to provide strict type checking and explicit casting.

---
 fs/exfat/balloc.c   |   2 +-
 fs/exfat/dir.c      |  48 ++++++++---------
 fs/exfat/exfat_fs.h | 122 ++++++++++++++++++++++++++++++++------------
 fs/exfat/fatent.c   |   4 +-
 fs/exfat/file.c     |   8 +--
 fs/exfat/inode.c    |  19 +++----
 fs/exfat/namei.c    |  26 +++++-----
 fs/exfat/super.c    |   4 +-
 8 files changed, 147 insertions(+), 86 deletions(-)
fs/exfat/balloc.c +1 -1
index 625f2f14d4fe..e66ebf899778 100644
@@ -112,7 +112,7 @@ static int exfat_allocate_bitmap(struct super_block *sb,
 	}
 
 	if (exfat_test_bitmap_range(sb, sbi->map_clu,
-		EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
+		exfat_bytes_to_cluster_round_up(sbi, map_size)) == false)
 		goto err_out;
 
 	return 0;
fs/exfat/dir.c +25 -23

149 lines not on this page · raw message

fs/exfat/exfat_fs.h +89 -33

140 lines not on this page · raw message

fs/exfat/fatent.c +2 -2
index dce0955e689a..45b0b754a2e4 100644
@@ -412,8 +412,8 @@ int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
 
 	if (IS_DIRSYNC(dir))
 		return sync_blockdev_range(sb->s_bdev,
-				EXFAT_BLK_TO_B(blknr, sb),
-				EXFAT_BLK_TO_B(last_blknr, sb) - 1);
+				exfat_block_to_bytes(sb, blknr),
+				exfat_block_to_bytes(sb, last_blknr) - 1);
 
 	return 0;
 }
fs/exfat/file.c +4 -4
index 354bdcfe4abc..29a36a80e29b 100644
@@ -33,9 +33,9 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
 	if (ret)
 		return ret;
 
-	num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
+	num_clusters = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode));
 	/* integer overflow is already checked in inode_newsize_ok(). */
-	new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi);
+	new_num_clusters = exfat_bytes_to_cluster_round_up(sbi, size);
 
 	if (new_num_clusters == num_clusters)
 		goto out;
@@ -200,8 +200,8 @@ int __exfat_truncate(struct inode *inode)
 
 	exfat_set_volume_dirty(sb);
 
-	num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
-	num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
+	num_clusters_new = exfat_bytes_to_cluster_round_up(sbi, i_size_read(inode));
+	num_clusters_phys = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode));
 
 	exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
 
fs/exfat/inode.c +10 -9
index 1ea4c740fef9..249a35e2b4b2 100644
@@ -135,7 +135,7 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 	unsigned int local_clu_offset = clu_offset;
 	unsigned int num_to_be_allocated = 0, num_clusters;
 
-	num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
+	num_clusters = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode));
 
 	if (clu_offset >= num_clusters)
 		num_to_be_allocated = clu_offset - num_clusters + 1;
@@ -216,7 +216,8 @@ static 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;
+		inode->i_blocks +=
+			exfat_cluster_to_sectors(sbi, num_to_be_allocated);
 
 		/*
 		 * Move *clu pointer along FAT chains (hole care) because the
@@ -254,12 +255,12 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 
 	mutex_lock(&sbi->s_lock);
 	i_size = i_size_read(inode);
-	last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size, sb);
+	last_block = exfat_bytes_to_block_round_up(sb, i_size);
 	if (iblock >= last_block && !create)
 		goto done;
 
 	/* Is this block already allocated? */
-	count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
+	count = exfat_bytes_to_cluster_round_up(sbi, bh_result->b_size);
 	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
 			&cluster, &count, create);
 	if (err) {
@@ -296,9 +297,9 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	 * care the last nested block if valid_size is not equal to i_size.
 	 */
 	if (i_size == ei->valid_size || create || !bh_result->b_folio)
-		valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
+		valid_blks = exfat_bytes_to_block_round_up(sb, ei->valid_size);
 	else
-		valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
+		valid_blks = exfat_bytes_to_block(sb, ei->valid_size);
 
 	/* The range has been fully written, map it */
 	if (iblock + max_blocks < valid_blks)
@@ -313,7 +314,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	/* The area has not been written, map and mark as new for create case */
 	if (create) {
 		set_buffer_new(bh_result);
-		ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
+		ei->valid_size = exfat_block_to_bytes(sb, iblock + max_blocks);
 		mark_inode_dirty(inode);
 		goto done;
 	}
@@ -343,7 +344,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 			goto done;
 		}
 
-		pos = EXFAT_BLK_TO_B(iblock, sb);
+		pos = exfat_block_to_bytes(sb, iblock);
 		size = ei->valid_size - pos;
 		addr = folio_address(bh_result->b_folio) +
 			offset_in_folio(bh_result->b_folio, pos);
@@ -374,7 +375,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	 */
 	clear_buffer_mapped(bh_result);
 done:
-	bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
+	bh_result->b_size = exfat_block_to_bytes(sb, max_blocks);
 	if (err < 0)
 		clear_buffer_mapped(bh_result);
 unlock_ret:
fs/exfat/namei.c +14 -12
index 2c5636634b4a..752fbec9316b 100644
@@ -208,7 +208,7 @@ static int exfat_search_empty_slot(struct super_block *sb,
 	int dentries_per_clu;
 	struct exfat_chain clu;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
-	int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi);
+	unsigned int total_entries = exfat_cluster_to_dentries(sbi, p_dir->size);
 
 	dentries_per_clu = sbi->dentries_per_clu;
 
@@ -266,7 +266,7 @@ static int exfat_search_empty_slot(struct super_block *sb,
 
 static int exfat_check_max_dentries(struct inode *inode)
 {
-	if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
+	if (exfat_bytes_to_dentries(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
 		/*
 		 * exFAT spec allows a dir to grow up to 8388608(256MB)
 		 * dentries
@@ -314,7 +314,8 @@ int exfat_find_empty_entry(struct inode *inode,
 	}
 
 	exfat_chain_set(p_dir, ei->start_clu,
-			EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
+			exfat_bytes_to_cluster(sbi, i_size_read(inode)),
+			ei->flags);
 
 	while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
 					num_entries, es)) < 0) {
@@ -375,7 +376,7 @@ int exfat_find_empty_entry(struct inode *inode,
 
 		hint_femp.cur.size++;
 		p_dir->size++;
-		size = EXFAT_CLU_TO_B(p_dir->size, sbi);
+		size = exfat_cluster_to_bytes(sbi, p_dir->size);
 
 		/* directory inode should be updated in here */
 		i_size_write(inode, size);
@@ -604,7 +605,7 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 		return ret;
 
 	exfat_chain_set(&cdir, ei->start_clu,
-		EXFAT_B_TO_CLU(i_size_read(dir), sbi), ei->flags);
+		exfat_bytes_to_cluster(sbi, i_size_read(dir)), ei->flags);
 
 	/* check the validation of hint_stat and initialize it if required */
 	if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
@@ -681,7 +682,7 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 		return -EIO;
 	}
 
-	if (unlikely(EXFAT_B_TO_CLU_ROUND_UP(info->size, sbi) > sbi->used_clusters)) {
+	if (unlikely(exfat_bytes_to_cluster_round_up(sbi, info->size) > sbi->used_clusters)) {
 		exfat_fs_error(sb, "data size is invalid(%lld)", info->size);
 		return -EIO;
 	}
@@ -695,7 +696,8 @@ static int exfat_find(struct inode *dir, const struct qstr *qname,
 
 	if (info->type == TYPE_DIR) {
 		exfat_chain_set(&cdir, info->start_clu,
-				EXFAT_B_TO_CLU(info->size, sbi), info->flags);
+				exfat_bytes_to_cluster(sbi, info->size),
+				info->flags);
 		count = exfat_count_dir_entries(sb, &cdir);
 		if (count < 0)
 			return -EIO;
@@ -951,7 +953,7 @@ static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
 	}
 
 	exfat_chain_set(&clu_to_free, ei->start_clu,
-		EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
+		exfat_bytes_to_cluster_round_up(sbi, i_size_read(inode)), ei->flags);
 
 	err = exfat_check_dir_empty(sb, &clu_to_free);
 	if (err) {
@@ -1158,8 +1160,8 @@ static int __exfat_rename(struct inode *old_parent_inode,
 
 			new_clu.dir = new_ei->start_clu;
 			new_clu.size =
-				EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
-				sbi);
+				exfat_bytes_to_cluster_round_up(sbi,
+						i_size_read(new_inode));
 			new_clu.flags = new_ei->flags;
 
 			ret = exfat_check_dir_empty(sb, &new_clu);
@@ -1203,8 +1205,8 @@ static int __exfat_rename(struct inode *old_parent_inode,
 			struct exfat_chain new_clu_to_free;
 
 			exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
-				EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
-				sbi), new_ei->flags);
+				exfat_bytes_to_cluster_round_up(sbi, i_size_read(new_inode)),
+				new_ei->flags);
 
 			if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
 				/* just set I/O error only */
fs/exfat/super.c +2 -2
index 95d87e2d7717..cb2f8eefff99 100644
@@ -369,7 +369,7 @@ static int exfat_read_root(struct inode *inode, struct exfat_chain *root_clu)
 	ei->hint_stat.clu = sbi->root_dir;
 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
 
-	i_size_write(inode, EXFAT_CLU_TO_B(root_clu->size, sbi));
+	i_size_write(inode, exfat_cluster_to_bytes(sbi, root_clu->size));
 
 	num_subdirs = exfat_count_dir_entries(sb, root_clu);
 	if (num_subdirs < 0)
@@ -538,7 +538,7 @@ static int exfat_read_boot_sector(struct super_block *sb)
 	 * machines.
 	 */
 	sb->s_maxbytes = min(MAX_LFS_FILESIZE,
-			     EXFAT_CLU_TO_B((loff_t)EXFAT_MAX_NUM_CLUSTER, sbi));
+			     exfat_cluster_to_bytes(sbi, (loff_t)EXFAT_MAX_NUM_CLUSTER));
 
 	/* check logical sector size */
 	if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
Signature · 1 line
2.25.1
author maintainer
#7 lore raw

PATCH v4 3/11 exfat: add balloc parameter to exfat_map_cluster() for iomap support

In reply to Namjae Jeon

In preparation for supporting the iomap infrastructure, we need to know whether a new cluster was allocated or not in exfat_map_cluster().

Add an optional 'bool *balloc' output parameter. When a new cluster is allocated, *balloc is set to true. Pass NULL from exfat_get_block() to preserve the existing behavior.

---
 fs/exfat/inode.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/exfat/inode.c +5 -2
index 249a35e2b4b2..a10d4f3c66a1 100644
@@ -124,7 +124,8 @@ void exfat_sync_inode(struct inode *inode)
  * *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)
+		unsigned int *clu, unsigned int *count, int create,
+		bool *balloc)
 {
 	int ret;
 	unsigned int last_clu;
@@ -229,6 +230,8 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		if (exfat_cluster_walk(sb, clu, num_to_be_allocated - 1, ei->flags))
 			return -EIO;
 		*count = 1;
+		if (balloc)
+			*balloc = true;
 	}
 
 	/* hint information */
@@ -262,7 +265,7 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 	/* Is this block already allocated? */
 	count = exfat_bytes_to_cluster_round_up(sbi, bh_result->b_size);
 	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
-			&cluster, &count, create);
+			&cluster, &count, create, NULL);
 	if (err) {
 		if (err != -ENOSPC)
 			exfat_fs_error_ratelimit(sb,
Signature · 1 line
2.25.1
author maintainer
#8 lore raw

PATCH v4 4/11 exfat: add exfat_file_open()

In reply to Namjae Jeon

Add exfat_file_open() to handle file open operation for exFAT. This change is a preparation step before introducing iomap-based direct IO support.

---
 fs/exfat/file.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/exfat/file.c +9 -0
index 29a36a80e29b..d0ecdcb4bf7a 100644
@@ -800,7 +800,16 @@ static ssize_t exfat_splice_read(struct file *in, loff_t *ppos,
 	return filemap_splice_read(in, ppos, pipe, len, flags);
 }
 
+static int exfat_file_open(struct inode *inode, struct file *filp)
+{
+	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
+		return -EIO;
+
+	return generic_file_open(inode, filp);
+}
+
 const struct file_operations exfat_file_operations = {
+	.open		= exfat_file_open,
 	.llseek		= generic_file_llseek,
 	.read_iter	= exfat_file_read_iter,
 	.write_iter	= exfat_file_write_iter,
Signature · 1 line
2.25.1
author maintainer
#9 lore raw

PATCH v4 5/11 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    | 23 ++++++-----------------
 fs/exfat/namei.c    |  2 +-
 6 files changed, 26 insertions(+), 31 deletions(-)
fs/exfat/dir.c +1 -1
index ca9d707220df..37f324399b6b 100644
@@ -295,7 +295,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 9c8ab3df7a42..6e7fd6822b01 100644
@@ -497,7 +497,7 @@ int exfat_clear_volume_dirty(struct super_block *sb);
 	exfat_cluster_walk(sb, (pclu), 1, ALLOC_FAT_CHAIN)
 
 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 45b0b754a2e4..a917c954bd23 100644
@@ -419,7 +419,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;
@@ -470,14 +470,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 */
@@ -507,9 +513,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 d0ecdcb4bf7a..1effdf08ab69 100644
@@ -56,7 +56,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 +6 -17
index a10d4f3c66a1..7b09d94ac464 100644
@@ -137,9 +137,9 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 	unsigned int num_to_be_allocated = 0, num_clusters;
 
 	num_clusters = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode));
-
-	if (clu_offset >= num_clusters)
-		num_to_be_allocated = clu_offset - num_clusters + 1;
+	if (clu_offset > num_clusters ||
+	    *count > num_clusters - clu_offset)
+		num_to_be_allocated = clu_offset + *count - num_clusters;
 
 	if (!create && (num_to_be_allocated > 0)) {
 		*clu = EXFAT_EOF_CLUSTER;
@@ -182,7 +182,7 @@ static 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;
 
@@ -216,20 +216,9 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 		}
 
 		*clu = new_clu.dir;
+		*count = new_clu.size;
 
-		inode->i_blocks +=
-			exfat_cluster_to_sectors(sbi, num_to_be_allocated);
-
-		/*
-		 * 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 (exfat_cluster_walk(sb, clu, num_to_be_allocated - 1, ei->flags))
-			return -EIO;
-		*count = 1;
+		inode->i_blocks += exfat_cluster_to_sectors(sbi, new_clu.size);
 		if (balloc)
 			*balloc = true;
 	}
fs/exfat/namei.c +1 -1
index 752fbec9316b..87e55c5c1bf4 100644
@@ -341,7 +341,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
#10 lore raw

PATCH v4 6/11 exfat: add data_start_bytes and exfat_cluster_to_phys_bytes() helper

In reply to Namjae Jeon

This caches the data area start offset in bytes (data_start_bytes) and introduces a helper function exfat_cluster_to_phys_bytes() to compute the physical byte position of a given cluster.

---
 fs/exfat/exfat_fs.h | 8 ++++++++
 fs/exfat/super.c    | 1 +
 2 files changed, 9 insertions(+)
fs/exfat/exfat_fs.h +8 -0
index 6e7fd6822b01..cd38e71d3e8a 100644
@@ -227,6 +227,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 */
@@ -400,6 +401,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_bytes(struct exfat_sb_info *sbi,
+		unsigned int clus)
+{
+	return ((loff_t)(clus - EXFAT_RESERVED_CLUSTERS) << sbi->cluster_size_bits) +
+		sbi->data_start_bytes;
+}
+
 /*
  * helpers for cluster size to byte conversion.
  */
fs/exfat/super.c +1 -0
index cb2f8eefff99..388db271c6bf 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
author maintainer
#11 lore raw

PATCH v4 7/11 exfat: fix implicit declaration of brelse()

In reply to Namjae Jeon

exfat_cluster_walk() calls brelse(bh) without including the header that declares the function, causing the following build error:

fs/exfat/exfat_fs.h:542:9: error: implicit declaration of function ‘brelse’ [-Werror=implicit-function-declaration]

Fix this by adding the missing buffer_head.h in exfat_fs.h.

---
 fs/exfat/exfat_fs.h | 1 +
 1 file changed, 1 insertion(+)
fs/exfat/exfat_fs.h +1 -0
index cd38e71d3e8a..08d22d2581da 100644
@@ -12,6 +12,7 @@
 #include <linux/blkdev.h>
 #include <linux/backing-dev.h>
 #include <uapi/linux/exfat.h>
+#include <linux/buffer_head.h>
 
 #define EXFAT_ROOT_INO		1
 
Signature · 1 line
2.25.1
author maintainer
#12 lore raw

PATCH v4 8/11 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 will replaces the old exfat_get_block(), exfat_write_begin(), exfat_write_end(), and exfat_block_truncate_page() 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/Kconfig    |   1 +
 fs/exfat/Makefile   |   2 +-
 fs/exfat/exfat_fs.h |   6 +-
 fs/exfat/file.c     | 142 +++++++++++++++++++----------
 fs/exfat/inode.c    | 117 ++++++++----------------
 fs/exfat/iomap.c    | 213 ++++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/iomap.h    |  14 +++
 7 files changed, 365 insertions(+), 130 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
fs/exfat/Kconfig +1 -0
index cbeca8e44d9b..e0b200902253 100644
@@ -5,6 +5,7 @@ config EXFAT_FS
 	select BUFFER_HEAD
 	select NLS
 	select LEGACY_DIRECT_IO
+	select FS_IOMAP
 	help
 	  This allows you to mount devices formatted with the exFAT file system.
 	  exFAT is typically used on SD-Cards or USB sticks.
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 +5 -1
index 08d22d2581da..2d482858bfaf 100644
@@ -294,6 +294,8 @@ struct exfat_inode_info {
 	/* on-disk position of directory entry or 0 */
 	loff_t i_pos;
 	loff_t valid_size;
+	/* page-aligned size that has been zeroed out for mmap */
+	loff_t zeroed_size;
 	/* hash by i_location */
 	struct hlist_node i_hash_fat;
 	/* protect bmap against truncate */
@@ -649,7 +651,9 @@ 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);
 
 /* exfat/nls.c */
 unsigned short exfat_toupper(struct super_block *sb, unsigned short a);
fs/exfat/file.c +95 -47

230 lines not on this page · raw message

fs/exfat/inode.c +36 -81

187 lines not on this page · raw message

fs/exfat/iomap.c new file +213 -0

213 lines not on this page · raw message

fs/exfat/iomap.h new file +14 -0
new file mode 100644
index 000000000000..7f8dcbe20a17
@@ -0,0 +1,14 @@
+/* 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_ops exfat_iomap_ops;
+extern const struct iomap_ops exfat_write_iomap_ops;
+extern const struct iomap_writeback_ops exfat_writeback_ops;
+extern const struct iomap_read_ops exfat_iomap_bio_read_ops;
+
+#endif /* _LINUX_EXFAT_IOMAP_H */
Signature · 1 line
2.25.1
#13 lore raw

Re: [PATCH v4 08/11] exfat: add iomap buffered I/O support

Quoted 31 lines
fs/exfat/file.c +11 -8
@@ -749,28 +750,56 @@ static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
 
 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
 {
-       int err;
        struct inode *inode = file_inode(vmf->vma->vm_file);
        struct exfat_inode_info *ei = EXFAT_I(inode);
-       loff_t new_valid_size;
+       vm_fault_t ret;
+       loff_t new_valid_size, mmap_valid_size;
 
        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));
+       mmap_valid_size = ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT;
+       new_valid_size = min(mmap_valid_size, i_size_read(inode));
 
        if (ei->valid_size < new_valid_size) {
-               err = exfat_extend_valid_size(inode, new_valid_size);
-               if (err < 0) {
-                       inode_unlock(inode);
-                       return vmf_fs_error(err);
+               if (ei->zeroed_size < mmap_valid_size) {
+                       int err;
+
+                       /*
+                        * Only zero the range that hasn't been zeroed yet for
+                        * this mmap write path. zeroed_size tracks the largest
+                        * page-aligned offset that has already been zeroed.

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

Quoted 10 lines
fs/exfat/file.c +10 -0
+                        *
+                        * This prevents unnecessarily zeroing out the entire
+                        * tail page on every page fault when userspace writes
+                        * data byte-by-byte through mmap (after a small
+                        * fallocate). It fixes data corruption in the tail page
+                        * while preserving the existing valid_size semantics.
+                        */
+                       err = iomap_zero_range(inode, ei->zeroed_size,
+                                       mmap_valid_size - ei->zeroed_size, NULL,
+                                       &exfat_iomap_ops, NULL, NULL);

For volumes with clusters smaller than 4K, mmap_valid_size may be greater than exfat_on_disksize(), causing iomap_zero_range() to allocate new clusters for the file. We need to ensure that mmap_valid_size is not greater than exfat_on_disksize() to avoid this situation.

Quoted 5 lines
fs/exfat/file.c +5 -0
+                       if (err < 0) {
+                               inode_unlock(inode);
+                               return vmf_fs_error(err);
+                       }
+                       ei->zeroed_size = mmap_valid_size;
author maintainer
#14 lore raw

PATCH v4 9/11 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 iomap_dio_rw() interface.

---
 fs/exfat/Kconfig    |   1 -
 fs/exfat/exfat_fs.h |   1 -
 fs/exfat/file.c     |  89 ++++++++++++++++----
 fs/exfat/inode.c    | 200 --------------------------------------------
 fs/exfat/iomap.c    |  26 ++++++
 fs/exfat/iomap.h    |   1 +
 6 files changed, 102 insertions(+), 216 deletions(-)
fs/exfat/Kconfig +0 -1
index e0b200902253..1fcb10c8d7bc 100644
@@ -4,7 +4,6 @@ config EXFAT_FS
 	tristate "exFAT filesystem support"
 	select BUFFER_HEAD
 	select NLS
-	select LEGACY_DIRECT_IO
 	select FS_IOMAP
 	help
 	  This allows you to mount devices formatted with the exFAT file system.
fs/exfat/exfat_fs.h +0 -1
index 2d482858bfaf..174728904dc1 100644
@@ -557,7 +557,6 @@ int exfat_trim_fs(struct inode *inode, struct fstrim_range *range);
 /* file.c */
 extern const struct file_operations exfat_file_operations;
 int __exfat_truncate(struct inode *inode);
-void exfat_truncate(struct inode *inode);
 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		  struct iattr *attr);
 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
fs/exfat/file.c +75 -14

132 lines not on this page · raw message

fs/exfat/inode.c +0 -200

218 lines not on this page · raw message

fs/exfat/iomap.c +26 -0
index b360fadc9d58..c6841ad0b1cc 100644
@@ -12,6 +12,32 @@
 #include "exfat_fs.h"
 #include "iomap.h"
 
+/*
+ * 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,
+};
+
 static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		unsigned int flags, struct iomap *iomap, bool may_alloc)
 {
fs/exfat/iomap.h +1 -0
index 7f8dcbe20a17..830388f386f4 100644
@@ -6,6 +6,7 @@
 #ifndef _LINUX_EXFAT_IOMAP_H
 #define _LINUX_EXFAT_IOMAP_H
 
+extern const struct iomap_dio_ops exfat_write_dio_ops;
 extern const struct iomap_ops exfat_iomap_ops;
 extern const struct iomap_ops exfat_write_iomap_ops;
 extern const struct iomap_writeback_ops exfat_writeback_ops;
Signature · 1 line
2.25.1
author maintainer
#15 lore raw

PATCH v4 10/11 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_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_HOLE.
- Ranges with offset < ei->valid_size are mapped as IOMAP_MAPPED.

---
 fs/exfat/file.c  | 25 ++++++++++++++++++++++++-
 fs/exfat/iomap.c | 32 ++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 5 deletions(-)
fs/exfat/file.c +24 -1
index e5529d0698db..d9c4e8dd9fd8 100644
@@ -917,9 +917,32 @@ 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_iomap_ops);
+		inode_unlock_shared(inode);
+		break;
+	case SEEK_DATA:
+		inode_lock_shared(inode);
+		offset = iomap_seek_data(inode, offset, &exfat_iomap_ops);
+		inode_unlock_shared(inode);
+		break;
+	default:
+		return generic_file_llseek(file, offset, whence);
+	}
+	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,
fs/exfat/iomap.c +28 -4
index c6841ad0b1cc..61a1b6aba860 100644
@@ -98,12 +98,36 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length
 			iomap->flags = IOMAP_F_ZERO_TAIL;
 		}
 	} else {
+		/*
+		 * valid_size is tracked in byte granularity and
+		 * marks the exact boundary between valid data and
+		 * holes (or unwritten space).
+		 *
+		 * When IOMAP_REPORT is set (used by lseek(SEEK_HOLE)
+		 * and SEEK_DATA), we return IOMAP_HOLE. This allows
+		 * iomap_seek_hole_iter() to directly return the
+		 * precise byte position.
+		 *
+		 * For normal I/O paths (without IOMAP_REPORT) we
+		 * return IOMAP_UNWRITTEN so the write path can
+		 * distinguish it from a real hole.
+		 */
 		if (offset >= ei->valid_size) {
-			iomap->type = IOMAP_UNWRITTEN;
+			iomap->type = flags & IOMAP_REPORT ?
+				IOMAP_HOLE : IOMAP_UNWRITTEN;
 		} else if (offset + iomap->length > ei->valid_size) {
-			iomap->length = round_up(ei->valid_size,
-						 i_blocksize(inode)) -
-							iomap->offset;
+			if (flags & IOMAP_REPORT) {
+				/*
+				 * For SEEK_HOLE/SEEK_DATA, clip the length
+				 * to the exact byte boundary (valid_size).
+				 * This ensures the caller gets the precise
+				 * hole position in byte units.
+				 */
+				iomap->length = ei->valid_size - iomap->offset;
+			} else
+				iomap->length = round_up(ei->valid_size,
+							 i_blocksize(inode)) -
+								iomap->offset;
 		}
 	}
 
Signature · 1 line
2.25.1
author maintainer
#16 lore raw

PATCH v4 11/11 exfat: make exfat_truncate() return error code

In reply to Namjae Jeon

Convert exfat_truncate() to return int, and make it return the error code on failure. Update exfat_setattr() to handle the returned error appropriately.

---
 fs/exfat/file.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/exfat/file.c +4 -2
index d9c4e8dd9fd8..1271202b35ed 100644
@@ -292,7 +292,7 @@ int __exfat_truncate(struct inode *inode)
 	return 0;
 }
 
-static void exfat_truncate(struct inode *inode)
+static int exfat_truncate(struct inode *inode)
 {
 	struct super_block *sb = inode->i_sb;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
@@ -315,6 +315,8 @@ static void exfat_truncate(struct inode *inode)
 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
 write_size:
 	mutex_unlock(&sbi->s_lock);
+
+	return err;
 }
 
 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
@@ -400,7 +402,7 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 		 * __exfat_write_inode() is called from exfat_truncate(), inode
 		 * is already written by it, so mark_inode_dirty() is unneeded.
 		 */
-		exfat_truncate(inode);
+		error = exfat_truncate(inode);
 		up_write(&EXFAT_I(inode)->truncate_lock);
 	} else
 		mark_inode_dirty(inode);
Signature · 1 line
2.25.1
Acked-by on the v4 series
#17 lore raw
In reply to Namjae Jeon
Quoted 5 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.

iozone benchmark results (4KB cluster size, -s1g -r64k, 1GB file, 64KB record size)

Quoted 8 lines

1 thread 4 threads
Write Read Write Read
(MB/s) (MB/s) (MB/s) (MB/s)
-----------------------------------------------------------------

exfat + iomap patch   332.7     418.1           78.6      82.4
Current exfat         278.4     415.1           42.1      38.0

-----------------------------------------------------------------
Improvement +19.5% +0.7% +86.7% +117.4%

FWIW I scanned over the other patches in the series. Nothing stood out as scream-worthy, though as the maintainer I'm assuming you're watching them closely for the kinds of QA problems that only an exfat expert would know. ;)

--D

Quoted 77 lines

Available in the Git repository at:
===================================
git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat.git iomap-work

v4:
- Declare initialized variables before uninitialized ones in __iomap_write_begin.
- Clean up the logic for handling ei->valid_size in the non-alloc iomap read path.
- Rename exfat_cluster_to_phys to exfat_cluster_to_phys_bytes.
- Use end variable in exfat_fallback_buffered_write.

v3:
- Remove extra >> 9 when adding to inode->i_blocks.
- Simplify exfat_file_open() by removing unnecessary err variable.
- Separate exfat_truncate() error return conversion from iomap changes.
- Fix xfstests failures with 512B cluster size.
- Make exfat_truncate() return error code.
- Use inode lock to protect valid_size in exfat_extend_valid_size().
- Unify num_clusters calculation regardless of may_alloc.
- Use min_t() to clamp iomap->length.
- Return early in exfat_write_iomap_end() if no data written.
- Remove s_lock in exfat_write_iomap_end().
- Split DIO fallback buffered write into helper function.
- Remove unnecessary goto in exfat_dio_write_iter().

v2:
- Replace macros with static inline functions.
- Remove noop_direct_IO.
- Consolidate read and write iomap_begin into __exfat_iomap_begin()
- Zero out stale data in straddle block beyond valid_size.
- Introduce IOMAP_F_ZERO_TAIL flag to remove exfat_iomap_put_folio().
- Select FS_IOMAP in Kconfig.
- Remove unnecessary alignment check in exfat_file_read_iter().
- Just use generic_file_llseek directly.
- Move exfat_extend_valid_size to exfat_file_write_iter().
- Use pagecache_isize_extended() to remove iomap_zero_range in exfat_setattr().
- fix mmap write data corruption with byte-by-byte fallocate.
- Remove exfat_mkwrite_iomap_begin().

Namjae Jeon (11):
iomap: introduce IOMAP_F_ZERO_TAIL flag
exfat: replace unsafe macros with static inline functions
exfat: add balloc parameter to exfat_map_cluster() for iomap support
exfat: add exfat_file_open()
exfat: add support for multi-cluster allocation
exfat: add data_start_bytes and exfat_cluster_to_phys_bytes() helper
exfat: fix implicit declaration of brelse()
exfat: add iomap buffered I/O support
exfat: add iomap direct I/O support
exfat: add support for SEEK_HOLE and SEEK_DATA in llseek
exfat: make exfat_truncate() return error code

 fs/exfat/Kconfig       |   2 +-
 fs/exfat/Makefile      |   2 +-
 fs/exfat/balloc.c      |   2 +-
 fs/exfat/dir.c         |  50 +++---
 fs/exfat/exfat_fs.h    | 140 ++++++++++++-----
 fs/exfat/fatent.c      |  30 ++--
 fs/exfat/file.c        | 263 +++++++++++++++++++++++--------
 fs/exfat/inode.c       | 342 ++++++-----------------------------------
 fs/exfat/iomap.c       | 263 +++++++++++++++++++++++++++++++
 fs/exfat/iomap.h       |  15 ++
 fs/exfat/namei.c       |  28 ++--
 fs/exfat/super.c       |   5 +-
 fs/iomap/buffered-io.c |   4 +
 include/linux/iomap.h  |   4 +
 14 files changed, 703 insertions(+), 447 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
Signature · 1 line
2.25.1
author maintainer
#18 lore raw
Quoted 16 lines · Darrick J. Wong
Quoted 5 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.

iozone benchmark results (4KB cluster size, -s1g -r64k, 1GB file, 64KB record size)

Quoted 8 lines

1 thread 4 threads
Write Read Write Read
(MB/s) (MB/s) (MB/s) (MB/s)
-----------------------------------------------------------------

exfat + iomap patch   332.7     418.1           78.6      82.4
Current exfat         278.4     415.1           42.1      38.0

-----------------------------------------------------------------
Improvement +19.5% +0.7% +86.7% +117.4%

Quoted 4 lines

FWIW I scanned over the other patches in the series. Nothing stood out as scream-worthy, though as the maintainer I'm assuming you're watching them closely for the kinds of QA problems that only an exfat expert would know. ;)

I've checked it against all the potential issues I'm aware of and no issues in my test-sets either.
Once IOMAP_F_ZERO_TAIL lands, I plan to update NTFS to use it as well.

Quoted 2 lines

Thanks for reviewing the series and giving your Acked-by!

#19 lore raw
In reply to Namjae Jeon

So with all the reviews in we need to figure out how to merge this. Does merging the iomap part through the vfs tree and pulling that into the exfat tree work for you?

Quoted 78 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.

iozone benchmark results (4KB cluster size, -s1g -r64k, 1GB file, 64KB record size)

1 thread 4 threads
Write Read Write Read
(MB/s) (MB/s) (MB/s) (MB/s)
-----------------------------------------------------------------

exfat + iomap patch   332.7     418.1           78.6      82.4
Current exfat         278.4     415.1           42.1      38.0

-----------------------------------------------------------------
Improvement +19.5% +0.7% +86.7% +117.4%

Available in the Git repository at:
===================================
git://git.kernel.org/pub/scm/linux/kernel/git/linkinjeon/exfat.git iomap-work

v4:
- Declare initialized variables before uninitialized ones in __iomap_write_begin.
- Clean up the logic for handling ei->valid_size in the non-alloc iomap read path.
- Rename exfat_cluster_to_phys to exfat_cluster_to_phys_bytes.
- Use end variable in exfat_fallback_buffered_write.

v3:
- Remove extra >> 9 when adding to inode->i_blocks.
- Simplify exfat_file_open() by removing unnecessary err variable.
- Separate exfat_truncate() error return conversion from iomap changes.
- Fix xfstests failures with 512B cluster size.
- Make exfat_truncate() return error code.
- Use inode lock to protect valid_size in exfat_extend_valid_size().
- Unify num_clusters calculation regardless of may_alloc.
- Use min_t() to clamp iomap->length.
- Return early in exfat_write_iomap_end() if no data written.
- Remove s_lock in exfat_write_iomap_end().
- Split DIO fallback buffered write into helper function.
- Remove unnecessary goto in exfat_dio_write_iter().

v2:
- Replace macros with static inline functions.
- Remove noop_direct_IO.
- Consolidate read and write iomap_begin into __exfat_iomap_begin()
- Zero out stale data in straddle block beyond valid_size.
- Introduce IOMAP_F_ZERO_TAIL flag to remove exfat_iomap_put_folio().
- Select FS_IOMAP in Kconfig.
- Remove unnecessary alignment check in exfat_file_read_iter().
- Just use generic_file_llseek directly.
- Move exfat_extend_valid_size to exfat_file_write_iter().
- Use pagecache_isize_extended() to remove iomap_zero_range in exfat_setattr().
- fix mmap write data corruption with byte-by-byte fallocate.
- Remove exfat_mkwrite_iomap_begin().

Namjae Jeon (11):
iomap: introduce IOMAP_F_ZERO_TAIL flag
exfat: replace unsafe macros with static inline functions
exfat: add balloc parameter to exfat_map_cluster() for iomap support
exfat: add exfat_file_open()
exfat: add support for multi-cluster allocation
exfat: add data_start_bytes and exfat_cluster_to_phys_bytes() helper
exfat: fix implicit declaration of brelse()
exfat: add iomap buffered I/O support
exfat: add iomap direct I/O support
exfat: add support for SEEK_HOLE and SEEK_DATA in llseek
exfat: make exfat_truncate() return error code

 fs/exfat/Kconfig       |   2 +-
 fs/exfat/Makefile      |   2 +-
 fs/exfat/balloc.c      |   2 +-
 fs/exfat/dir.c         |  50 +++---
 fs/exfat/exfat_fs.h    | 140 ++++++++++++-----
 fs/exfat/fatent.c      |  30 ++--
 fs/exfat/file.c        | 263 +++++++++++++++++++++++--------
 fs/exfat/inode.c       | 342 ++++++-----------------------------------
 fs/exfat/iomap.c       | 263 +++++++++++++++++++++++++++++++
Quoted 11 lines
 fs/exfat/iomap.h       |  15 ++
 fs/exfat/namei.c       |  28 ++--
 fs/exfat/super.c       |   5 +-
 fs/iomap/buffered-io.c |   4 +
 include/linux/iomap.h  |   4 +
 14 files changed, 703 insertions(+), 447 deletions(-)
 create mode 100644 fs/exfat/iomap.c
 create mode 100644 fs/exfat/iomap.h
Signature · 1 line
2.25.1

---end quoted text---

author maintainer
#20 lore raw
Quoted 4 lines · Christoph Hellwig

So with all the reviews in we need to figure out how to merge this. Does merging the iomap part through the vfs tree and pulling that into the exfat tree work for you?

Yes. If Christian can take the 0001 iomap patch through the vfs tree, I will apply the remaining exfat patches to the exfat tree after pulling it in.
Thanks!

maintainer Applied
#21 lore raw

Re: (subset) [PATCH v4 00/11] exfat: convert to iomap

In reply to Namjae Jeon
Quoted 9 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.

iozone benchmark results (4KB cluster size, -s1g -r64k, 1GB file, 64KB record size)

1 thread 4 threads
Write Read Write Read
(MB/s) (MB/s) (MB/s) (MB/s)
-----------------------------------------------------------------

Quoted 6 lines
exfat + iomap patch   332.7     418.1           78.6      82.4
Current exfat         278.4     415.1           42.1      38.0

-----------------------------------------------------------------
Improvement +19.5% +0.7% +86.7% +117.4%

[...]

Applied to the vfs-7.2.iomap branch of the vfs/vfs.git tree. Patches in the vfs-7.2.iomap branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs-7.2.iomap

[01/11] iomap: introduce IOMAP_F_ZERO_TAIL flag

        https://git.kernel.org/vfs/vfs/c/f356603d90eb