Skip to contentSkip to conversation

exfat: mark straddling folio RO for 4K block size

PATCH In review Yuezhang Mo 2 participants 5 messages Sep 30 – Oct 9 exFAT file system

lore
Share & tools
lore

Yuezhang Mo

author reviewer
#0 lore raw

PATCH 1/1 exfat: mark straddling folio RO for 4K block size

The maximum file system block size supported by exFAT is 4096 bytes. When extending a file whose EOF is not page aligned, the straddling folio must be marked RO so that a subsequent mmap write can trigger ->page_mkwrite() and extend the valid data size.

This is normally handled by pagecache_isize_extended(), which marks the straddling folio RO when the filesystem block size is smaller than 4K. However, pagecache_isize_extended() returns early when the filesystem block size is PAGE_SIZE:

    if (from >= to || bsize >= PAGE_SIZE)
        return;

As a result, on an exFAT filesystem with a 4K block size, the PTE for the straddling folio remains writable after a file extension. A subsequent mmap write operation that modifies the straddling folio beyond the old EOF, will not trigger ->page_mkwrite(), leaving valid_size unchanged.

This was found by running xfstests generic/030 on a block device with a 4096-byte sector size: the test fails without this fix and passes with it.

Fix this by marking the straddling folio RO in exfat_cont_expand() when the filesystem block size is 4K and the old EOF is not page aligned. This mirrors what pagecache_isize_extended() does for smaller filesystem block sizes.

Fixes 82a81a7352bc ("exfat: add iomap buffered I/O support")
---
 fs/exfat/file.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/exfat/file.c +15 -0
index a2a9ee1a20048..390bfae3f3aa4 100644
@@ -17,6 +17,7 @@
 #include <linux/fileattr.h>
 #include <linux/iomap.h>
 #include <linux/pagemap.h>
+#include <linux/rmap.h>
 
 #include "exfat_raw.h"
 #include "exfat_fs.h"
@@ -91,6 +92,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
 	 */
 	pagecache_isize_extended(inode, oldsize, inode->i_size);
 
+	if (i_blocksize(inode) >= PAGE_SIZE && (oldsize & (PAGE_SIZE - 1))) {
+		struct folio *folio;
+
+		folio = filemap_lock_folio(inode->i_mapping,
+				oldsize >> PAGE_SHIFT);
+		if (!IS_ERR(folio)) {
+			if (folio_mkclean(folio))
+				folio_mark_dirty(folio);
+
+			folio_unlock(folio);
+			folio_put(folio);
+		}
+	}
+
 	inode->i_blocks = round_up(size, sbi->cluster_size) >> 9;
 	mark_inode_dirty(inode);
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 53 lines · Yuezhang Mo

The maximum file system block size supported by exFAT is 4096 bytes. When extending a file whose EOF is not page aligned, the straddling folio must be marked RO so that a subsequent mmap write can trigger ->page_mkwrite() and extend the valid data size.

This is normally handled by pagecache_isize_extended(), which marks the straddling folio RO when the filesystem block size is smaller than 4K. However, pagecache_isize_extended() returns early when the filesystem block size is PAGE_SIZE:

     if (from >= to || bsize >= PAGE_SIZE)
         return;

As a result, on an exFAT filesystem with a 4K block size, the PTE for the straddling folio remains writable after a file extension. A subsequent mmap write operation that modifies the straddling folio beyond the old EOF, will not trigger ->page_mkwrite(), leaving valid_size unchanged.

This was found by running xfstests generic/030 on a block device with a 4096-byte sector size: the test fails without this fix and passes with it.

Fix this by marking the straddling folio RO in exfat_cont_expand() when the filesystem block size is 4K and the old EOF is not page aligned. This mirrors what pagecache_isize_extended() does for smaller filesystem block sizes.

Fixes 82a81a7352bc ("exfat: add iomap buffered I/O support")
---
  fs/exfat/file.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
fs/exfat/file.c +4 -0
index a2a9ee1a20048..390bfae3f3aa4 100644
@@ -17,6 +17,7 @@
  #include <linux/fileattr.h>
  #include <linux/iomap.h>
  #include <linux/pagemap.h>
+#include <linux/rmap.h>
 
  #include "exfat_raw.h"
  #include "exfat_fs.h"
@@ -91,6 +92,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
  	 */
  	pagecache_isize_extended(inode, oldsize, inode->i_size);
 
+	if (i_blocksize(inode) >= PAGE_SIZE && (oldsize & (PAGE_SIZE - 1))) {
+		struct folio *folio;
+

fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 107

Quoted 10 lines
fs/exfat/file.c +10 -0
+		folio = filemap_lock_folio(inode->i_mapping,
+				oldsize >> PAGE_SHIFT);
+		if (!IS_ERR(folio)) {
+			if (folio_mkclean(folio))
+				folio_mark_dirty(folio);
+
+			folio_unlock(folio);
+			folio_put(folio);
+		}
+	}

I think fixing this issue specifically for the block size = 4K case may not be thorough enough, because pagecache_isize_extended() may also fail to work when the block size is smaller than 4K.

We should set the folio containing valid_size to RO whenever valid_size != i_size and valid_size is not page-aligned, regardless of the block size.

Quoted 4 lines
fs/exfat/file.c +1 -0
+
  	inode->i_blocks = round_up(size, sbi->cluster_size) >> 9;
  	mark_inode_dirty(inode);
 
author reviewer
#2 lore raw
Quoted 15 lines
Quoted 15 lines
  #include "exfat_raw.h"
  #include "exfat_fs.h"
fs/exfat/file.c +9 -0
@@ -91,6 +92,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
       */
      pagecache_isize_extended(inode, oldsize, inode->i_size);
 
+     if (i_blocksize(inode) >= PAGE_SIZE && (oldsize & (PAGE_SIZE - 1))) {
+             struct folio *folio;
+
+             folio = filemap_lock_folio(inode->i_mapping,
+                             oldsize >> PAGE_SHIFT);
+             if (!IS_ERR(folio)) {
+                     if (folio_mkclean(folio))
+                             folio_mark_dirty(folio);
+
Quoted 8 lines

fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 107

Quoted 4 lines
fs/exfat/file.c +4 -0
+                     folio_unlock(folio);
+                     folio_put(folio);
+             }
+     }

I think fixing this issue specifically for the block size = 4K case may not be thorough enough, because pagecache_isize_extended() may also fail to work when the block size is smaller than 4K.

What condition might pagecache_isize_extended() fail to work when the block size is smaller than 4K?

Quoted 3 lines

We should set the folio containing valid_size to RO whenever valid_size != i_size and valid_size is not page-aligned, regardless of the block size.

We should set the folio containing oldsize to RO, rather than valid_size. This is because, when the folio containing oldsize was first set to RW, oldsize constrained valid_size, after the file size is subsequently expanded, that constraint changes, necessitating a re-setting of valid_size.

#3 lore raw
Quoted 24 lines · On 10/9/26 10:46 AM, [email protected] wrote:
Quoted 15 lines
Quoted 15 lines
   #include "exfat_raw.h"
   #include "exfat_fs.h"
fs/exfat/file.c +9 -0
@@ -91,6 +92,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
        */
       pagecache_isize_extended(inode, oldsize, inode->i_size);
 
+     if (i_blocksize(inode) >= PAGE_SIZE && (oldsize & (PAGE_SIZE - 1))) {
+             struct folio *folio;
+
+             folio = filemap_lock_folio(inode->i_mapping,
+                             oldsize >> PAGE_SHIFT);
+             if (!IS_ERR(folio)) {
+                     if (folio_mkclean(folio))
+                             folio_mark_dirty(folio);
+
Quoted 8 lines

fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 107

Quoted 4 lines
fs/exfat/file.c +4 -0
+                     folio_unlock(folio);
+                     folio_put(folio);
+             }
+     }

I think fixing this issue specifically for the block size = 4K case may not be thorough enough, because pagecache_isize_extended() may also fail to work when the block size is smaller than 4K.

Quoted 2 lines

What condition might pagecache_isize_extended() fail to work when the block size is smaller than 4K?

I didn’t explain this clearly. What I meant is that the issue is not the block size, but the fact that VDL in exFAT is not block-aligned. pagecache_isize_extended is designed to work at block granularity, so it won’t work either if oldsize and newsize fall within the same block.

/* Page straddling @from will not have any hole block created? */ rounded_from = round_up(from, bsize);
if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))

        return;
Quoted 9 lines
Quoted 4 lines

We should set the folio containing valid_size to RO whenever valid_size != i_size and valid_size is not page-aligned, regardless of the block size.

We should set the folio containing oldsize to RO, rather than valid_size. This is because, when the folio containing oldsize was first set to RW, oldsize constrained valid_size, after the file size is subsequently expanded, that constraint changes, necessitating a re-setting of valid_size.

Yes, using oldsize is better.

author reviewer
#4 lore raw
Quoted 33 lines · On 10/9/26 10:46 AM, [email protected] wrote:
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
   #include "exfat_raw.h"
   #include "exfat_fs.h"
fs/exfat/file.c +9 -0
@@ -91,6 +92,20 @@ static int exfat_cont_expand(struct inode *inode, loff_t size)
        */
       pagecache_isize_extended(inode, oldsize, inode->i_size);
 
+     if (i_blocksize(inode) >= PAGE_SIZE && (oldsize & (PAGE_SIZE - 1))) {
+             struct folio *folio;
+
+             folio = filemap_lock_folio(inode->i_mapping,
+                             oldsize >> PAGE_SHIFT);
+             if (!IS_ERR(folio)) {
+                     if (folio_mkclean(folio))
+                             folio_mark_dirty(folio);
+
Quoted 8 lines

fs/exfat/file.c static int exfat_cont_expand(struct inode *inode, loff_t size) · line 107

Quoted 4 lines
fs/exfat/file.c +4 -0
+                     folio_unlock(folio);
+                     folio_put(folio);
+             }
+     }

I think fixing this issue specifically for the block size = 4K case may not be thorough enough, because pagecache_isize_extended() may also fail to work when the block size is smaller than 4K.

Quoted 2 lines

What condition might pagecache_isize_extended() fail to work when the block size is smaller than 4K?

I didn’t explain this clearly. What I meant is that the issue is not the block size, but the fact that VDL in exFAT is not block-aligned. pagecache_isize_extended is designed to work at block granularity, so it won’t work either if oldsize and newsize fall within the same block.

Quoted 4 lines

/* Page straddling @from will not have any hole block created? */ rounded_from = round_up(from, bsize);
if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))

         return;

I got it, thank you for the explanation.