All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] fat: allocate multiple clusters in fallocate
@ 2026-10-12 17:11 Matheus Alves de Almeida
  2026-10-12 17:11 ` [PATCH v2 1/3] fat: remove cluster allocation size limit Matheus Alves de Almeida
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-12 17:11 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel, Matheus Alves de Almeida

Hi,

This series adds support for allocating multiple clusters in a single
fat_add_clusters() call in fat_fallocate().

The multi-cluster TODO in inode.c is not implemented, as doing so would
require speculative preallocation or an iomap conversion.

Changes in v2:
- Keep the directory entry path unchanged.
- Add fat_alloc_chain() for callers that only need the first cluster,
  sharing the internal implementation with fat_alloc_clusters().
- Add benchmark results to the fallocate commit log.

Matheus Alves de Almeida (3):
  fat: remove cluster allocation size limit
  fat: support adding multiple clusters
  fat: allocate multiple clusters in fallocate

 fs/fat/fat.h    |  3 ++-
 fs/fat/fatent.c | 62 +++++++++++++++++++++++++++++++++++++------------
 fs/fat/file.c   | 10 ++++----
 fs/fat/inode.c  |  8 +++----
 4 files changed, 57 insertions(+), 26 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/3] fat: remove cluster allocation size limit
  2026-10-12 17:11 [PATCH v2 0/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
@ 2026-10-12 17:11 ` Matheus Alves de Almeida
  2026-10-13 13:17   ` OGAWA Hirofumi
  2026-10-12 17:11 ` [PATCH v2 2/3] fat: support adding multiple clusters Matheus Alves de Almeida
  2026-10-12 17:11 ` [PATCH v2 3/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
  2 siblings, 1 reply; 7+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-12 17:11 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel, Matheus Alves de Almeida

fat_alloc_clusters() currently BUG()s when asked to allocate more than
MAX_BUF_PER_PAGE / 2 clusters. This limit comes from the buffer_head
array used to defer syncing and mirroring.

Remove the allocation size limit by doing buffer_head batching like
fat_free_clusters() does.

Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
---
 fs/fat/fatent.c | 44 +++++++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c
index f0801d99d..bfab8eab5 100644
--- a/fs/fat/fatent.c
+++ b/fs/fat/fatent.c
@@ -468,6 +468,26 @@ static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
 	}
 }
 
+static int fat_flush_bhs(struct super_block *sb, struct buffer_head **bhs,
+			 int *nr_bhs, bool sync)
+{
+	int err, i;
+
+	if (sync) {
+		err = fat_sync_bhs(bhs, *nr_bhs);
+		if (err)
+			return err;
+	}
+	err = fat_mirror_bhs(sb, bhs, *nr_bhs);
+	if (err)
+		return err;
+	for (i = 0; i < *nr_bhs; i++)
+		brelse(bhs[i]);
+	*nr_bhs = 0;
+
+	return 0;
+}
+
 int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 {
 	struct super_block *sb = inode->i_sb;
@@ -477,8 +497,6 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
 	int i, count, err, nr_bhs, idx_clus;
 
-	BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2));	/* fixed limit */
-
 	lock_fat(sbi);
 	if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
 	    sbi->free_clusters < nr_cluster) {
@@ -509,14 +527,21 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 				if (prev_ent.nr_bhs)
 					ops->ent_put(&prev_ent, entry);
 
-				fat_collect_bhs(bhs, &nr_bhs, &fatent);
-
 				sbi->prev_free = entry;
 				if (sbi->free_clusters != -1)
 					sbi->free_clusters--;
 
 				cluster[idx_clus] = entry;
 				idx_clus++;
+
+				if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
+					err = fat_flush_bhs(sb, bhs, &nr_bhs,
+							    inode_needs_sync(inode));
+					if (err)
+						goto out;
+				}
+				fat_collect_bhs(bhs, &nr_bhs, &fatent);
+
 				if (idx_clus == nr_cluster)
 					goto out;
 
@@ -606,17 +631,10 @@ int fat_free_clusters(struct inode *inode, int cluster)
 		}
 
 		if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
-			if (sb->s_flags & SB_SYNCHRONOUS) {
-				err = fat_sync_bhs(bhs, nr_bhs);
-				if (err)
-					goto error;
-			}
-			err = fat_mirror_bhs(sb, bhs, nr_bhs);
+			err = fat_flush_bhs(sb, bhs, &nr_bhs,
+					    sb->s_flags & SB_SYNCHRONOUS);
 			if (err)
 				goto error;
-			for (i = 0; i < nr_bhs; i++)
-				brelse(bhs[i]);
-			nr_bhs = 0;
 		}
 		fat_collect_bhs(bhs, &nr_bhs, &fatent);
 	} while (cluster != FAT_ENT_EOF);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/3] fat: support adding multiple clusters
  2026-10-12 17:11 [PATCH v2 0/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
  2026-10-12 17:11 ` [PATCH v2 1/3] fat: remove cluster allocation size limit Matheus Alves de Almeida
@ 2026-10-12 17:11 ` Matheus Alves de Almeida
  2026-10-13 13:17   ` OGAWA Hirofumi
  2026-10-12 17:11 ` [PATCH v2 3/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
  2 siblings, 1 reply; 7+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-12 17:11 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel, Matheus Alves de Almeida

fat_add_cluster() currently adds only one cluster to a file's chain,
requiring multiple calls when adding more clusters.

Add a fat_alloc_chain() helper that allocates a cluster chain
but only returns the first cluster. Extend fat_add_cluster()
to add multiple clusters in a single call and rename it to
fat_add_clusters() to reflect the new behavior.

Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
---
 fs/fat/fat.h    |  3 ++-
 fs/fat/fatent.c | 18 ++++++++++++++++--
 fs/fat/file.c   |  2 +-
 fs/fat/inode.c  |  8 ++++----
 4 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/fs/fat/fat.h b/fs/fat/fat.h
index 61338413d..b18385461 100644
--- a/fs/fat/fat.h
+++ b/fs/fat/fat.h
@@ -395,6 +395,7 @@ extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
 			 int new, int wait);
 extern int fat_alloc_clusters(struct inode *inode, int *cluster,
 			      int nr_cluster);
+int fat_alloc_chain(struct inode *inode, int *cluster, int nr_cluster);
 extern int fat_free_clusters(struct inode *inode, int cluster);
 extern int fat_count_free_clusters(struct super_block *sb);
 extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
@@ -440,7 +441,7 @@ static inline unsigned long fat_dir_hash(int logstart)
 {
 	return hash_32(logstart, FAT_HASH_BITS);
 }
-extern int fat_add_cluster(struct inode *inode);
+extern int fat_add_clusters(struct inode *inode, int nr_cluster);
 
 /* fat/misc.c */
 extern __printf(3, 4) __cold
diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c
index bfab8eab5..8897a5dd5 100644
--- a/fs/fat/fatent.c
+++ b/fs/fat/fatent.c
@@ -488,7 +488,8 @@ static int fat_flush_bhs(struct super_block *sb, struct buffer_head **bhs,
 	return 0;
 }
 
-int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
+static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster,
+				bool return_all)
 {
 	struct super_block *sb = inode->i_sb;
 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
@@ -531,7 +532,10 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 				if (sbi->free_clusters != -1)
 					sbi->free_clusters--;
 
-				cluster[idx_clus] = entry;
+				if (return_all)
+					cluster[idx_clus] = entry;
+				else if (!idx_clus)
+					*cluster = entry;
 				idx_clus++;
 
 				if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
@@ -581,6 +585,16 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
 	return err;
 }
 
+int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
+{
+	return __fat_alloc_clusters(inode, cluster, nr_cluster, true);
+}
+
+int fat_alloc_chain(struct inode *inode, int *cluster, int nr_cluster)
+{
+	return __fat_alloc_clusters(inode, cluster, nr_cluster, false);
+}
+
 int fat_free_clusters(struct inode *inode, int cluster)
 {
 	struct super_block *sb = inode->i_sb;
diff --git a/fs/fat/file.c b/fs/fat/file.c
index 1c835ca5f..41d49e9de 100644
--- a/fs/fat/file.c
+++ b/fs/fat/file.c
@@ -292,7 +292,7 @@ static long fat_fallocate(struct file *file, int mode,
 
 		/* Start the allocation.We are not zeroing out the clusters */
 		while (nr_cluster-- > 0) {
-			err = fat_add_cluster(inode);
+			err = fat_add_clusters(inode, 1);
 			if (err)
 				goto error;
 		}
diff --git a/fs/fat/inode.c b/fs/fat/inode.c
index f775a004c..141276fcf 100644
--- a/fs/fat/inode.c
+++ b/fs/fat/inode.c
@@ -101,16 +101,16 @@ static struct fat_floppy_defaults {
 },
 };
 
-int fat_add_cluster(struct inode *inode)
+int fat_add_clusters(struct inode *inode, int nr_cluster)
 {
 	int err, cluster;
 
-	err = fat_alloc_clusters(inode, &cluster, 1);
+	err = fat_alloc_chain(inode, &cluster, nr_cluster);
 	if (err)
 		return err;
 	/* FIXME: this cluster should be added after data of this
 	 * cluster is writed */
-	err = fat_chain_add(inode, cluster, 1);
+	err = fat_chain_add(inode, cluster, nr_cluster);
 	if (err)
 		fat_free_clusters(inode, cluster);
 	return err;
@@ -152,7 +152,7 @@ static inline int __fat_get_block(struct inode *inode, sector_t iblock,
 	 */
 	if (!offset && !(iblock < last_block)) {
 		/* TODO: multiple cluster allocation would be desirable. */
-		err = fat_add_cluster(inode);
+		err = fat_add_clusters(inode, 1);
 		if (err)
 			return err;
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/3] fat: allocate multiple clusters in fallocate
  2026-10-12 17:11 [PATCH v2 0/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
  2026-10-12 17:11 ` [PATCH v2 1/3] fat: remove cluster allocation size limit Matheus Alves de Almeida
  2026-10-12 17:11 ` [PATCH v2 2/3] fat: support adding multiple clusters Matheus Alves de Almeida
@ 2026-10-12 17:11 ` Matheus Alves de Almeida
  2026-10-13 13:17   ` OGAWA Hirofumi
  2 siblings, 1 reply; 7+ messages in thread
From: Matheus Alves de Almeida @ 2026-10-12 17:11 UTC (permalink / raw)
  To: OGAWA Hirofumi; +Cc: linux-fsdevel, linux-kernel, Matheus Alves de Almeida

The fat_fallocate() code currently does many 1-cluster allocations when
growing a file in the FALLOC_FL_KEEP_SIZE path. Make it allocate
everything with one call instead.

Benchmarking was done under QEMU/KVM with a virtio-blk device using
cache=none. Each result is the arithmetic mean of the listed samples.

start = now_ns();
for (unsigned int i = 0; i < count; i++)
     fallocate(fds[i], FALLOC_FL_KEEP_SIZE, 0, bytes);
elapsed = now_ns() - start;

FAT32 with 512-byte clusters using count=16 files:
length    mounted sync?    before    after    improvement    samples
256MiB    no               2.328s    0.175s   13.2x          10
16MiB     no               149.659ms 14.036ms 10.7x          10
16MiB     yes              747.993s  3.445s   217.1x         3
1MiB      yes              46.63s    0.333s   140.1x         10

FAT32 with 512-byte clusters using count=128 files:
length    mounted sync?    before    after    improvement    samples
1MiB      no               90.873ms  4.686ms  19.4x          10
16KiB     no               1.287ms   0.241ms  5.3x           10
128KiB    yes              47.284s   0.350s   135x           10
4KiB      yes              1.98s     0.144s   13.7x          10

FAT32 with 4096-byte clusters using count=16 files:
length    mounted sync?    before    after    improvement    samples
256MiB    no               295.392ms 24.733ms 11.9x          10
16MiB     no               20.509ms  1.404ms  14.6x          10
16MiB     yes              92.489s   0.348s   265.4x         10
1MiB      yes              5.837s    0.048s   121.3x         10

The improvement is especially large on synchronous mounts, where the
new code can batch FAT updates instead of synchronously handling each
cluster allocation separately.

Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>
---
 fs/fat/file.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/fs/fat/file.c b/fs/fat/file.c
index 41d49e9de..3aa541c6b 100644
--- a/fs/fat/file.c
+++ b/fs/fat/file.c
@@ -290,12 +290,10 @@ static long fat_fallocate(struct file *file, int mode,
 		nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
 			sbi->cluster_bits;
 
-		/* Start the allocation.We are not zeroing out the clusters */
-		while (nr_cluster-- > 0) {
-			err = fat_add_clusters(inode, 1);
-			if (err)
-				goto error;
-		}
+		/* Allocate the clusters. We are not zeroing them out */
+		err = fat_add_clusters(inode, nr_cluster);
+		if (err)
+			goto error;
 	} else {
 		if ((offset + len) <= i_size_read(inode))
 			goto error;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/3] fat: remove cluster allocation size limit
  2026-10-12 17:11 ` [PATCH v2 1/3] fat: remove cluster allocation size limit Matheus Alves de Almeida
@ 2026-10-13 13:17   ` OGAWA Hirofumi
  0 siblings, 0 replies; 7+ messages in thread
From: OGAWA Hirofumi @ 2026-10-13 13:17 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Matheus Alves de Almeida, linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> fat_alloc_clusters() currently BUG()s when asked to allocate more than
> MAX_BUF_PER_PAGE / 2 clusters. This limit comes from the buffer_head
> array used to defer syncing and mirroring.
>
> Remove the allocation size limit by doing buffer_head batching like
> fat_free_clusters() does.
>
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

Looks good. Thanks.

> ---
>  fs/fat/fatent.c | 44 +++++++++++++++++++++++++++++++-------------
>  1 file changed, 31 insertions(+), 13 deletions(-)
>
> diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c
> index f0801d99d..bfab8eab5 100644
> --- a/fs/fat/fatent.c
> +++ b/fs/fat/fatent.c
> @@ -468,6 +468,26 @@ static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
>  	}
>  }
>  
> +static int fat_flush_bhs(struct super_block *sb, struct buffer_head **bhs,
> +			 int *nr_bhs, bool sync)
> +{
> +	int err, i;
> +
> +	if (sync) {
> +		err = fat_sync_bhs(bhs, *nr_bhs);
> +		if (err)
> +			return err;
> +	}
> +	err = fat_mirror_bhs(sb, bhs, *nr_bhs);
> +	if (err)
> +		return err;
> +	for (i = 0; i < *nr_bhs; i++)
> +		brelse(bhs[i]);
> +	*nr_bhs = 0;
> +
> +	return 0;
> +}
> +
>  int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
>  {
>  	struct super_block *sb = inode->i_sb;
> @@ -477,8 +497,6 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
>  	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
>  	int i, count, err, nr_bhs, idx_clus;
>  
> -	BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2));	/* fixed limit */
> -
>  	lock_fat(sbi);
>  	if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
>  	    sbi->free_clusters < nr_cluster) {
> @@ -509,14 +527,21 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
>  				if (prev_ent.nr_bhs)
>  					ops->ent_put(&prev_ent, entry);
>  
> -				fat_collect_bhs(bhs, &nr_bhs, &fatent);
> -
>  				sbi->prev_free = entry;
>  				if (sbi->free_clusters != -1)
>  					sbi->free_clusters--;
>  
>  				cluster[idx_clus] = entry;
>  				idx_clus++;
> +
> +				if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
> +					err = fat_flush_bhs(sb, bhs, &nr_bhs,
> +							    inode_needs_sync(inode));
> +					if (err)
> +						goto out;
> +				}
> +				fat_collect_bhs(bhs, &nr_bhs, &fatent);
> +
>  				if (idx_clus == nr_cluster)
>  					goto out;
>  
> @@ -606,17 +631,10 @@ int fat_free_clusters(struct inode *inode, int cluster)
>  		}
>  
>  		if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
> -			if (sb->s_flags & SB_SYNCHRONOUS) {
> -				err = fat_sync_bhs(bhs, nr_bhs);
> -				if (err)
> -					goto error;
> -			}
> -			err = fat_mirror_bhs(sb, bhs, nr_bhs);
> +			err = fat_flush_bhs(sb, bhs, &nr_bhs,
> +					    sb->s_flags & SB_SYNCHRONOUS);
>  			if (err)
>  				goto error;
> -			for (i = 0; i < nr_bhs; i++)
> -				brelse(bhs[i]);
> -			nr_bhs = 0;
>  		}
>  		fat_collect_bhs(bhs, &nr_bhs, &fatent);
>  	} while (cluster != FAT_ENT_EOF);

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/3] fat: support adding multiple clusters
  2026-10-12 17:11 ` [PATCH v2 2/3] fat: support adding multiple clusters Matheus Alves de Almeida
@ 2026-10-13 13:17   ` OGAWA Hirofumi
  0 siblings, 0 replies; 7+ messages in thread
From: OGAWA Hirofumi @ 2026-10-13 13:17 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Matheus Alves de Almeida, linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> fat_add_cluster() currently adds only one cluster to a file's chain,
> requiring multiple calls when adding more clusters.
>
> Add a fat_alloc_chain() helper that allocates a cluster chain
> but only returns the first cluster. Extend fat_add_cluster()
> to add multiple clusters in a single call and rename it to
> fat_add_clusters() to reflect the new behavior.
>
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

Looks good. Thanks.

> ---
>  fs/fat/fat.h    |  3 ++-
>  fs/fat/fatent.c | 18 ++++++++++++++++--
>  fs/fat/file.c   |  2 +-
>  fs/fat/inode.c  |  8 ++++----
>  4 files changed, 23 insertions(+), 8 deletions(-)
>
> diff --git a/fs/fat/fat.h b/fs/fat/fat.h
> index 61338413d..b18385461 100644
> --- a/fs/fat/fat.h
> +++ b/fs/fat/fat.h
> @@ -395,6 +395,7 @@ extern int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
>  			 int new, int wait);
>  extern int fat_alloc_clusters(struct inode *inode, int *cluster,
>  			      int nr_cluster);
> +int fat_alloc_chain(struct inode *inode, int *cluster, int nr_cluster);
>  extern int fat_free_clusters(struct inode *inode, int cluster);
>  extern int fat_count_free_clusters(struct super_block *sb);
>  extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);
> @@ -440,7 +441,7 @@ static inline unsigned long fat_dir_hash(int logstart)
>  {
>  	return hash_32(logstart, FAT_HASH_BITS);
>  }
> -extern int fat_add_cluster(struct inode *inode);
> +extern int fat_add_clusters(struct inode *inode, int nr_cluster);
>  
>  /* fat/misc.c */
>  extern __printf(3, 4) __cold
> diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c
> index bfab8eab5..8897a5dd5 100644
> --- a/fs/fat/fatent.c
> +++ b/fs/fat/fatent.c
> @@ -488,7 +488,8 @@ static int fat_flush_bhs(struct super_block *sb, struct buffer_head **bhs,
>  	return 0;
>  }
>  
> -int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
> +static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster,
> +				bool return_all)
>  {
>  	struct super_block *sb = inode->i_sb;
>  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
> @@ -531,7 +532,10 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
>  				if (sbi->free_clusters != -1)
>  					sbi->free_clusters--;
>  
> -				cluster[idx_clus] = entry;
> +				if (return_all)
> +					cluster[idx_clus] = entry;
> +				else if (!idx_clus)
> +					*cluster = entry;
>  				idx_clus++;
>  
>  				if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
> @@ -581,6 +585,16 @@ int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
>  	return err;
>  }
>  
> +int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
> +{
> +	return __fat_alloc_clusters(inode, cluster, nr_cluster, true);
> +}
> +
> +int fat_alloc_chain(struct inode *inode, int *cluster, int nr_cluster)
> +{
> +	return __fat_alloc_clusters(inode, cluster, nr_cluster, false);
> +}
> +
>  int fat_free_clusters(struct inode *inode, int cluster)
>  {
>  	struct super_block *sb = inode->i_sb;
> diff --git a/fs/fat/file.c b/fs/fat/file.c
> index 1c835ca5f..41d49e9de 100644
> --- a/fs/fat/file.c
> +++ b/fs/fat/file.c
> @@ -292,7 +292,7 @@ static long fat_fallocate(struct file *file, int mode,
>  
>  		/* Start the allocation.We are not zeroing out the clusters */
>  		while (nr_cluster-- > 0) {
> -			err = fat_add_cluster(inode);
> +			err = fat_add_clusters(inode, 1);
>  			if (err)
>  				goto error;
>  		}
> diff --git a/fs/fat/inode.c b/fs/fat/inode.c
> index f775a004c..141276fcf 100644
> --- a/fs/fat/inode.c
> +++ b/fs/fat/inode.c
> @@ -101,16 +101,16 @@ static struct fat_floppy_defaults {
>  },
>  };
>  
> -int fat_add_cluster(struct inode *inode)
> +int fat_add_clusters(struct inode *inode, int nr_cluster)
>  {
>  	int err, cluster;
>  
> -	err = fat_alloc_clusters(inode, &cluster, 1);
> +	err = fat_alloc_chain(inode, &cluster, nr_cluster);
>  	if (err)
>  		return err;
>  	/* FIXME: this cluster should be added after data of this
>  	 * cluster is writed */
> -	err = fat_chain_add(inode, cluster, 1);
> +	err = fat_chain_add(inode, cluster, nr_cluster);
>  	if (err)
>  		fat_free_clusters(inode, cluster);
>  	return err;
> @@ -152,7 +152,7 @@ static inline int __fat_get_block(struct inode *inode, sector_t iblock,
>  	 */
>  	if (!offset && !(iblock < last_block)) {
>  		/* TODO: multiple cluster allocation would be desirable. */
> -		err = fat_add_cluster(inode);
> +		err = fat_add_clusters(inode, 1);
>  		if (err)
>  			return err;
>  	}

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 3/3] fat: allocate multiple clusters in fallocate
  2026-10-12 17:11 ` [PATCH v2 3/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
@ 2026-10-13 13:17   ` OGAWA Hirofumi
  0 siblings, 0 replies; 7+ messages in thread
From: OGAWA Hirofumi @ 2026-10-13 13:17 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Matheus Alves de Almeida, linux-fsdevel, linux-kernel

Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br> writes:

> The fat_fallocate() code currently does many 1-cluster allocations when
> growing a file in the FALLOC_FL_KEEP_SIZE path. Make it allocate
> everything with one call instead.
>
> Benchmarking was done under QEMU/KVM with a virtio-blk device using
> cache=none. Each result is the arithmetic mean of the listed samples.
>
> start = now_ns();
> for (unsigned int i = 0; i < count; i++)
>      fallocate(fds[i], FALLOC_FL_KEEP_SIZE, 0, bytes);
> elapsed = now_ns() - start;
>
> FAT32 with 512-byte clusters using count=16 files:
> length    mounted sync?    before    after    improvement    samples
> 256MiB    no               2.328s    0.175s   13.2x          10
> 16MiB     no               149.659ms 14.036ms 10.7x          10
> 16MiB     yes              747.993s  3.445s   217.1x         3
> 1MiB      yes              46.63s    0.333s   140.1x         10
>
> FAT32 with 512-byte clusters using count=128 files:
> length    mounted sync?    before    after    improvement    samples
> 1MiB      no               90.873ms  4.686ms  19.4x          10
> 16KiB     no               1.287ms   0.241ms  5.3x           10
> 128KiB    yes              47.284s   0.350s   135x           10
> 4KiB      yes              1.98s     0.144s   13.7x          10
>
> FAT32 with 4096-byte clusters using count=16 files:
> length    mounted sync?    before    after    improvement    samples
> 256MiB    no               295.392ms 24.733ms 11.9x          10
> 16MiB     no               20.509ms  1.404ms  14.6x          10
> 16MiB     yes              92.489s   0.348s   265.4x         10
> 1MiB      yes              5.837s    0.048s   121.3x         10
>
> The improvement is especially large on synchronous mounts, where the
> new code can batch FAT updates instead of synchronously handling each
> cluster allocation separately.
>
> Signed-off-by: Matheus Alves de Almeida <matheus.aalmeida@inf.ufrgs.br>

Acked-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

Looks good. Thanks.

> ---
>  fs/fat/file.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/fs/fat/file.c b/fs/fat/file.c
> index 41d49e9de..3aa541c6b 100644
> --- a/fs/fat/file.c
> +++ b/fs/fat/file.c
> @@ -290,12 +290,10 @@ static long fat_fallocate(struct file *file, int mode,
>  		nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
>  			sbi->cluster_bits;
>  
> -		/* Start the allocation.We are not zeroing out the clusters */
> -		while (nr_cluster-- > 0) {
> -			err = fat_add_clusters(inode, 1);
> -			if (err)
> -				goto error;
> -		}
> +		/* Allocate the clusters. We are not zeroing them out */
> +		err = fat_add_clusters(inode, nr_cluster);
> +		if (err)
> +			goto error;
>  	} else {
>  		if ((offset + len) <= i_size_read(inode))
>  			goto error;

-- 
OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-10-13 13:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-12 17:11 [PATCH v2 0/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
2026-10-12 17:11 ` [PATCH v2 1/3] fat: remove cluster allocation size limit Matheus Alves de Almeida
2026-10-13 13:17   ` OGAWA Hirofumi
2026-10-12 17:11 ` [PATCH v2 2/3] fat: support adding multiple clusters Matheus Alves de Almeida
2026-10-13 13:17   ` OGAWA Hirofumi
2026-10-12 17:11 ` [PATCH v2 3/3] fat: allocate multiple clusters in fallocate Matheus Alves de Almeida
2026-10-13 13:17   ` OGAWA Hirofumi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.