[PATCH 1/4] fat: remove cluster allocation size limit
From: Matheus Alves de Almeida
Date: Wed Oct 07 2026 - 22:11:24 EST
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@xxxxxxxxxxxx>
---
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