[PATCH 2/2] ksmbd: round SetAllocation to the advertised allocation unit

From: DaeMyung Kang

Date: Fri Oct 16 2026 - 23:41:20 EST


SetAllocation rounds requests to 512-byte blocks, although
FS_SIZE_INFORMATION advertises the filesystem's f_bsize as its
allocation unit. For example, on a filesystem with a 4 KiB unit, a
1000-byte request can truncate an 8 KiB file to 1024 bytes instead of
4 KiB.

Round the request to the advertised unit and use the same result for
the block-count decision, fallocate, truncate, and the size stored on
the open handle. Validate the unit and rounded size before either
operation. Use DIV_ROUND_UP_ULL() so the calculation works on 32-bit
kernels.

This does not change the existing block-count decision for whether to
call fallocate. In particular, success does not yet guarantee
reservation of holes within the requested range on a sparse file.

Signed-off-by: DaeMyung Kang <charsyam@xxxxxxxxx>
---
fs/smb/server/smb2pdu.c | 35 ++++++++++++++++++++---------------
1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index d06bdc8b23db..6a91f939f3d6 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -8596,10 +8596,11 @@ static int set_file_allocation_info(struct ksmbd_work *work,
* properly with any smb.conf option
*/

- loff_t alloc_blks;
- u64 alloc_size;
+ u64 alloc_size, target, alloc_blks;
+ u32 alloc_unit;
struct inode *inode;
struct kstat stat;
+ struct kstatfs stfs;
int rc;

if (!(fp->daccess & FILE_WRITE_DATA_LE))
@@ -8612,44 +8613,48 @@ static int set_file_allocation_info(struct ksmbd_work *work,
AT_STATX_SYNC_AS_STAT);
if (rc)
return rc;
+ rc = vfs_statfs(&fp->filp->f_path, &stfs);
+ if (rc)
+ return rc;

/*
- * AllocationSize is fully client-controlled (the caller only
- * validates the fixed 8-byte buffer length). Reject values that
- * would overflow the "round up to 512-byte blocks" conversion
- * below instead of silently wrapping it to a tiny block count,
- * which would truncate the file to a size the client never
- * asked for.
+ * Use the allocation unit advertised in FS_SIZE_INFORMATION.
+ * Reject requests that overflow when rounded to a whole unit.
*/
alloc_size = le64_to_cpu(file_alloc_info->AllocationSize);
- if (alloc_size > MAX_LFS_FILESIZE - 511)
+ if (stfs.f_bsize <= 0 || (u64)stfs.f_bsize > U32_MAX)
+ return -EINVAL;
+ alloc_unit = stfs.f_bsize;
+ if (alloc_size > MAX_LFS_FILESIZE - (alloc_unit - 1))
return -EINVAL;

- alloc_blks = (alloc_size + 511) >> 9;
+ target = DIV_ROUND_UP_ULL(alloc_size, alloc_unit) * alloc_unit;
+ alloc_blks = DIV_ROUND_UP_ULL(target, 512);
inode = file_inode(fp->filp);
+ if (target > inode->i_sb->s_maxbytes)
+ return -EFBIG;

if (alloc_blks > stat.blocks) {
smb_break_all_levII_oplock(work, fp, 1);
- rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
- alloc_blks * 512);
+ rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, target);
if (rc && rc != -EOPNOTSUPP) {
pr_err("vfs_fallocate is failed : %d\n", rc);
return rc;
}
} else if (alloc_blks < stat.blocks &&
- alloc_blks * 512 <= i_size_read(inode)) {
+ target <= i_size_read(inode)) {
/*
* Truncate at or below EOF to discard excess allocation.
* Truncating above EOF would extend the file on disk.
*/
- rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
+ rc = ksmbd_vfs_truncate(work, fp, target);
if (rc) {
pr_err("truncate failed!, err %d\n", rc);
return rc;
}
}

- fp->allocation_size = le64_to_cpu(file_alloc_info->AllocationSize);
+ fp->allocation_size = target;
fp->allocation_size_set = true;
return 0;
}
--
2.43.0