[PATCH 2/2] hfs: set s_maxbytes from the volume capacity
From: Viacheslav Dubeyko
Date: Thu Nov 05 2026 - 19:49:53 EST
Likewise HFS+, HFS returned -ENOSPC instead of -EFBIG when
a write targets an offset beyond the capacity of the volume.
hfs_fill_super() does not set s_maxbytes, so it keeps the
VFS default of MAX_NON_LFS (2GiB - 1). On a volume smaller
than that, the VFS limit checks consider the position valid
and the request reaches the file system. Because HFS does
not support holes, cont_write_begin() then tries to zero-fill
the whole gap up to the target offset, and the operation
fails with -ENOSPC.
This was previously worked around by a local check against
the volume capacity in hfs_write_begin(). That check only
covered the write path and only the start offset: truncate
up, copy_file_range() and writes that straddle the limit
were not handled.
Every byte of an HFS file up to end-of-file must be backed
by an allocated block, so a file can never be larger than
the volume. Set s_maxbytes to the volume capacity
(fs_ablocks * alloc_blksz, capped by MAX_NON_LFS). The VFS
then returns -EFBIG for write(), copy_file_range(), truncate
and fallocate beyond that limit, and trims writes that
straddle it. Drop the now redundant local check from
hfs_write_begin().
Fixes: c6decf13bf90 ("hfs: fix error code when writing beyond volume capacity")
Signed-off-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
cc: John Paul Adrian Glaubitz <glaubitz@xxxxxxxxxxxxxxxxxxx>
cc: Yangtao Li <frank.li@xxxxxxxx>
cc: linux-fsdevel@xxxxxxxxxxxxxxx
---
fs/hfs/inode.c | 7 -------
fs/hfs/super.c | 8 ++++++++
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/fs/hfs/inode.c b/fs/hfs/inode.c
index e7a8a5350216..ef1275bbbcbb 100644
--- a/fs/hfs/inode.c
+++ b/fs/hfs/inode.c
@@ -50,15 +50,8 @@ int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,
void **fsdata)
{
struct inode *inode = mapping->host;
- struct hfs_sb_info *sbi = HFS_SB(inode->i_sb);
- loff_t total_capacity;
int ret;
- total_capacity = (loff_t)sbi->fs_ablocks * sbi->alloc_blksz;
-
- if (pos >= total_capacity)
- return -EFBIG;
-
ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata,
hfs_get_block,
&HFS_I(inode)->phys_size);
diff --git a/fs/hfs/super.c b/fs/hfs/super.c
index d20150277eb3..fb8c6c0ba212 100644
--- a/fs/hfs/super.c
+++ b/fs/hfs/super.c
@@ -359,6 +359,14 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
goto bail;
}
+ /*
+ * HFS does not support sparse files, so every byte up to
+ * the end-of-file has to be backed by an allocated block.
+ * Therefore, a file can never be larger than the volume.
+ */
+ sb->s_maxbytes = min_t(loff_t, MAX_NON_LFS,
+ (loff_t)sbi->fs_ablocks * sbi->alloc_blksz);
+
/* try to get the root inode */
res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
if (res)
--
2.43.0